From 75dc74ae2ff2960828c0aa9226df1243aea5158c Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Mon, 25 Mar 2013 18:22:55 +0300 Subject: [PATCH] [twine][tools] Added missing files --- tools/twine/lib/twine/formatters/flash.rb | 110 ++++++++++++++++++++ tools/twine/lib/twine/formatters/gettext.rb | 100 ++++++++++++++++++ tools/twine/test/fixtures/en-1.po | 16 +++ tools/twine/test/fixtures/strings-3.txt | 5 + tools/twine/test/fixtures/test-output-7.txt | 14 +++ tools/twine/test/fixtures/test-output-8.txt | 9 ++ 6 files changed, 254 insertions(+) create mode 100644 tools/twine/lib/twine/formatters/flash.rb create mode 100644 tools/twine/lib/twine/formatters/gettext.rb create mode 100644 tools/twine/test/fixtures/en-1.po create mode 100644 tools/twine/test/fixtures/strings-3.txt create mode 100644 tools/twine/test/fixtures/test-output-7.txt create mode 100644 tools/twine/test/fixtures/test-output-8.txt diff --git a/tools/twine/lib/twine/formatters/flash.rb b/tools/twine/lib/twine/formatters/flash.rb new file mode 100644 index 0000000000..e36c40386b --- /dev/null +++ b/tools/twine/lib/twine/formatters/flash.rb @@ -0,0 +1,110 @@ +module Twine + module Formatters + class Flash < Abstract + FORMAT_NAME = 'flash' + EXTENSION = '.properties' + DEFAULT_FILE_NAME = 'resources.properties' + + def self.can_handle_directory?(path) + return false + end + + def default_file_name + return DEFAULT_FILE_NAME + end + + def determine_language_given_path(path) + return + end + + def read_file(path, lang) + encoding = Twine::Encoding.encoding_for_path(path) + sep = nil + if !encoding.respond_to?(:encode) + # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7. + if encoding.end_with? 'LE' + sep = "\x0a\x00" + elsif encoding.end_with? 'BE' + sep = "\x00\x0a" + else + sep = "\n" + end + end + + if encoding.index('UTF-16') + mode = "rb:#{encoding}" + else + mode = "r:#{encoding}" + end + + File.open(path, mode) do |f| + last_comment = nil + while line = (sep) ? f.gets(sep) : f.gets + if encoding.index('UTF-16') + if line.respond_to? :encode! + line.encode!('UTF-8') + else + require 'iconv' + line = Iconv.iconv('UTF-8', encoding, line).join + end + end + match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line) + if match + key = match[1] + value = match[2] + value.gsub!(/\{[0-9]\}/, '%@') + set_translation_for_key(key, lang, value) + if last_comment + set_comment_for_key(key, last_comment) + end + end + if @options[:consume_comments] + match = /#(.*)/.match(line) + if match + last_comment = match[1] + else + last_comment = nil + end + end + end + end + end + + def write_file(path, lang) + default_lang = @strings.language_codes[0] + encoding = @options[:output_encoding] || 'UTF-8' + File.open(path, "w:#{encoding}") do |f| + f.puts "## Flash Strings File\n## Generated by Twine #{Twine::VERSION}\n## Language: #{lang}\n" + @strings.sections.each do |section| + printed_section = false + section.rows.each do |row| + if row.matches_tags?(@options[:tags], @options[:untagged]) + f.puts '' + if !printed_section + if section.name && section.name.length > 0 + f.print "## #{section.name} ##\n\n" + end + printed_section = true + end + + key = row.key + value = row.translated_string_for_lang(lang, default_lang) + if value + placeHolderNumber = -1 + value = value.gsub(/%[d@]/) { placeHolderNumber += 1; '{%d}' % placeHolderNumber } + + comment = row.comment + if comment && comment.length > 0 + f.print "# #{comment}\n" + end + + f.print "#{key}=#{value}" + end + end + end + end + end + end + end + end +end diff --git a/tools/twine/lib/twine/formatters/gettext.rb b/tools/twine/lib/twine/formatters/gettext.rb new file mode 100644 index 0000000000..8dab8db91e --- /dev/null +++ b/tools/twine/lib/twine/formatters/gettext.rb @@ -0,0 +1,100 @@ +module Twine + module Formatters + class Gettext < Abstract + FORMAT_NAME = 'gettext' + EXTENSION = '.po' + DEFAULT_FILE_NAME = 'strings.po' + + def self.can_handle_directory?(path) + Dir.entries(path).any? { |item| /^.+\.po$/.match(item) } + end + + def default_file_name + return DEFAULT_FILE_NAME + end + + def determine_language_given_path(path) + path_arr = path.split(File::SEPARATOR) + path_arr.each do |segment| + match = /(..)\.po$/.match(segment) + if match + return match[1] + end + end + + return + end + + def read_file(path, lang) + comment_regex = /#.? *"(.*)"$/ + key_regex = /msgctxt *"(.*)"$/ + value_regex = /msgstr *"(.*)"$/m + File.open(path, 'r:UTF-8') do |f| + while item = f.gets("\n\n") + key = nil + value = nil + comment = nil + + for line in item.split(/\r?\n/) + comment_match = comment_regex.match(line) + if comment_match + comment = comment_match[1] + end + key_match = key_regex.match(line) + if key_match + key = key_match[1].gsub('\\"', '"') + end + value_match = value_regex.match(line) + if value_match + value = value_match[1].gsub('\\"', '"') + end + end + if key and key.length > 0 and value and value.length > 0 + set_translation_for_key(key, lang, value) + if comment and comment.length > 0 + set_comment_for_key(key, comment) + end + end + end + end + end + + def write_file(path, lang) + default_lang = @strings.language_codes[0] + encoding = @options[:output_encoding] || 'UTF-8' + File.open(path, "w:#{encoding}") do |f| + f.puts "msgid \"\"\nmsgstr \"\"\n\"Language: #{lang}\\n\"\n\"X-Generator: Twine #{Twine::VERSION}\\n\"\n\n" + @strings.sections.each do |section| + printed_section = false + section.rows.each do |row| + if row.matches_tags?(@options[:tags], @options[:untagged]) + basetrans = row.translated_string_for_lang(default_lang) + + if basetrans + key = row.key + key = key.gsub('"', '\\\\"') + + comment = row.comment + if comment + comment = comment.gsub('"', '\\\\"') + end + + if comment && comment.length > 0 + f.print "#. \"#{comment}\"\n" + end + + f.print "msgctxt \"#{key}\"\nmsgid \"#{basetrans}\"\n" + value = row.translated_string_for_lang(lang) + if value + value = value.gsub('"', '\\\\"') + end + f.print "msgstr \"#{value}\"\n\n" + end + end + end + end + end + end + end + end +end diff --git a/tools/twine/test/fixtures/en-1.po b/tools/twine/test/fixtures/en-1.po new file mode 100644 index 0000000000..4d153d4117 --- /dev/null +++ b/tools/twine/test/fixtures/en-1.po @@ -0,0 +1,16 @@ +msgid "" +msgstr "" +"Language: en\n" +"X-Generator: Twine\n" + +msgctxt "key1" +msgid "key1-english" +msgstr "key1-english" + +msgctxt "key3" +msgid "key3-english" +msgstr "" + +msgctxt "key5" +msgid "A new string" +msgstr "A new string" diff --git a/tools/twine/test/fixtures/strings-3.txt b/tools/twine/test/fixtures/strings-3.txt new file mode 100644 index 0000000000..6dc0103a27 --- /dev/null +++ b/tools/twine/test/fixtures/strings-3.txt @@ -0,0 +1,5 @@ +[[My Strings]] + [parameterized_string] + en = The %@ brown fox jumps over the %@ dog %d times. + [percentage_string] + en = This product is %d%% off. diff --git a/tools/twine/test/fixtures/test-output-7.txt b/tools/twine/test/fixtures/test-output-7.txt new file mode 100644 index 0000000000..999be8be51 --- /dev/null +++ b/tools/twine/test/fixtures/test-output-7.txt @@ -0,0 +1,14 @@ +msgid "" +msgstr "" +"Language: en\n" +"X-Generator: Twine <%= Twine::VERSION %>\n" + +#. "This is a comment" +msgctxt "key1" +msgid "key1-english" +msgstr "key1-english" + +msgctxt "key3" +msgid "key3-english" +msgstr "key3-english" + diff --git a/tools/twine/test/fixtures/test-output-8.txt b/tools/twine/test/fixtures/test-output-8.txt new file mode 100644 index 0000000000..1c630a2eb1 --- /dev/null +++ b/tools/twine/test/fixtures/test-output-8.txt @@ -0,0 +1,9 @@ + + + + + + + The %1$s brown fox jumps over the %2$s dog %3$d times. + This product is %d%% off. +