[twine][tools] Added missing files

This commit is contained in:
Alex Zolotarev 2013-03-25 18:22:55 +03:00 committed by Alex Zolotarev
parent 4753f646ef
commit 75dc74ae2f
6 changed files with 254 additions and 0 deletions

View file

@ -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

View file

@ -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

16
tools/twine/test/fixtures/en-1.po vendored Normal file
View file

@ -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"

View file

@ -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.

View file

@ -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"

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine <%= Twine::VERSION %> -->
<!-- Language: en -->
<resources>
<!-- SECTION: My Strings -->
<string name="parameterized_string">The %1$s brown fox jumps over the %2$s dog %3$d times.</string>
<string name="percentage_string">This product is %d%% off.</string>
</resources>