Extracted processing of StringsFile from formatters into OutputProcessor.

This commit is contained in:
Sebastian Ludwig 2015-11-20 20:04:43 +01:00
parent e8312d0f95
commit fc57ce0019
5 changed files with 71 additions and 30 deletions

View file

@ -5,6 +5,7 @@ module Twine
require 'twine/plugin'
require 'twine/cli'
require 'twine/encoding'
require 'twine/output_processor'
require 'twine/formatters'
require 'twine/runner'
require 'twine/stringsfile'

View file

@ -13,6 +13,7 @@ module Twine
def initialize(strings, options)
@strings = strings
@options = options
@output_processor = Processors::OutputProcessor.new @strings, @options
end
def iosify_substitutions(str)
@ -119,25 +120,17 @@ module Twine
raise NotImplementedError.new("You must implement read_file in your formatter class.")
end
def default_language
@options[:developer_language] || @strings.language_codes[0]
end
def fallback_languages(lang)
[default_language]
end
def format_file(lang)
def format_file(strings, lang)
result = format_header(lang) + "\n"
result += format_sections(lang)
result += format_sections(strings, lang)
end
def format_header(lang)
raise NotImplementedError.new("You must implement format_header in your formatter class.")
end
def format_sections(lang)
sections = @strings.sections.map { |section| format_section(section, lang) }
def format_sections(strings, lang)
sections = strings.sections.map { |section| format_section(section, lang) }
sections.join("\n")
end
@ -145,7 +138,7 @@ module Twine
end
def format_section(section, lang)
rows = section.rows.select { |row| row.matches_tags?(@options[:tags], @options[:untagged]) }
rows = section.rows.dup
result = ""
unless rows.empty?
@ -164,12 +157,6 @@ module Twine
def format_row(row, lang)
value = row.translated_string_for_lang(lang)
return if value && @options[:include] == 'untranslated'
if value.nil? && @options[:include] != 'translated'
value = row.translated_string_for_lang(fallback_languages(lang))
end
return nil unless value
result = ""
@ -199,8 +186,10 @@ module Twine
def write_file(path, lang)
encoding = @options[:output_encoding] || 'UTF-8'
processed_strings = @output_processor.process(lang)
File.open(path, "w:#{encoding}") do |f|
f.puts format_file(lang)
f.puts format_file(processed_strings, lang)
end
end

View file

@ -17,9 +17,6 @@ module Twine
'nb' => 'no'
# TODO: spanish
]
DEFAULT_LANG_CODES = Hash[
'zh-TW' => 'zh-Hant' # if we don't have a zh-TW translation, try zh-Hant before en
]
def self.can_handle_directory?(path)
Dir.entries(path).any? { |item| /^values.*$/.match(item) }
@ -91,18 +88,14 @@ module Twine
end
end
def fallback_languages(lang)
[DEFAULT_LANG_CODES[lang], super].flatten.compact
end
def format_header(lang)
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Android Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
end
def format_sections(lang)
def format_sections(strings, lang)
result = '<resources>'
result += super(lang) + "\n"
result += super + "\n"
result += '</resources>'
end
@ -120,6 +113,7 @@ module Twine
end
def format_value(value)
value = value.dup
# Android enforces the following rules on the values
# 1) apostrophes and quotes must be escaped with a backslash
value.gsub!("'", "\\\\'")

View file

@ -35,7 +35,7 @@ module Twine
open(path) do |io|
json = JSON.load(io)
json.each do |key, value|
value.gsub!("\n","\\n")
value = value.gsub("\n","\\n")
set_translation_for_key(key, lang, value)
end
end

View file

@ -0,0 +1,57 @@
module Twine
module Processors
class OutputProcessor
def initialize(strings, options)
@strings = strings
@options = options
end
def default_language
@options[:developer_language] || @strings.language_codes[0]
end
def fallback_languages(language)
fallback_mapping = {
'zh-TW' => 'zh-Hant' # if we don't have a zh-TW translation, try zh-Hant before en
}
[fallback_mapping[language], default_language].flatten.compact
end
def process(language)
result = StringsFile.new
@strings.language_codes.each { |lang| result.language_codes << lang }
@strings.sections.each do |section|
new_section = StringsSection.new section.name
section.rows.each do |row|
next unless row.matches_tags?(@options[:tags], @options[:untagged])
value = row.translated_string_for_lang(language)
next if value && @options[:include] == 'untranslated'
if value.nil? && @options[:include] != 'translated'
value = row.translated_string_for_lang(fallback_languages(language))
end
next unless value
new_row = row.dup
new_row.translations[language] = value
new_section.rows << new_row
result.strings_map[new_row.key] = new_row
end
result.sections << new_section
end
return result
end
end
end
end