Made androids concept of a fallback language available to all formatters.

This commit is contained in:
Sebastian Ludwig 2015-08-24 09:06:56 +02:00
parent 4a40e5d09e
commit a5dff1d711
3 changed files with 25 additions and 14 deletions

View file

@ -67,7 +67,7 @@ module Twine
opts.on('-n', '--file-name FILE_NAME', 'When running the generate-all-string-files command, this flag may be used to overwrite the default file name of the format.') do |n|
@options[:file_name] = n
end
opts.on('-d', '--developer-language LANG', 'When writing the strings data file, set the specified language as the "developer language". In practice, this just means that this language will appear first in the strings data file.') do |d|
opts.on('-d', '--developer-language LANG', 'When writing the strings data file, set the specified language as the "developer language". In practice, this just means that this language will appear first in the strings data file. When generating files this language will be used as default language and its translations will be used if a key is not localized for the output language.') do |d|
@options[:developer_language] = d
end
opts.on('-c', '--consume-comments', 'Normally, when consuming a string file, Twine will ignore all comments in the file. With this flag set, any comments encountered will be read and parsed into the strings data file. This is especially useful when creating your first strings data file from an existing project.') do |c|

View file

@ -113,24 +113,32 @@ module Twine
raise NotImplementedError.new("You must implement read_file in your formatter class.")
end
def format_file(lang, default_lang)
def default_language
@options[:developer_language] || @strings.language_codes[0]
end
def fallback_language(lang)
default_language
end
def format_file(lang)
result = format_header(lang) + "\n"
result += format_sections(lang, default_lang)
result += format_sections(lang)
end
def format_header(lang)
raise NotImplementedError.new("You must implement format_header in your formatter class.")
end
def format_sections(lang, default_lang)
sections = @strings.sections.map { |section| format_section(section, lang, default_lang) }
def format_sections(lang)
sections = @strings.sections.map { |section| format_section(section, lang) }
sections.join("\n")
end
def format_section_header(section)
end
def format_section(section, lang, default_lang)
def format_section(section, lang)
rows = section.rows.select { |row| row.matches_tags?(@options[:tags], @options[:untagged]) }
result = ""
@ -141,16 +149,16 @@ module Twine
end
end
rows.map! { |row| format_row(row, lang, default_lang) }
rows.map! { |row| format_row(row, lang) }
rows.compact! # remove nil entries
rows.map! { |row| "\n#{row}" } # prepend newline
result += rows.join
end
def format_row(row, lang, default_lang)
value = row.translated_string_for_lang(lang, default_lang)
def format_row(row, lang)
value = row.translated_string_for_lang(lang, fallback_language(lang))
if value.nil? && @options[:include_untranslated]
value = row.translated_string_for_lang(@strings.language_codes[0])
value = row.translated_string_for_lang(default_language)
end
return nil unless value
@ -179,11 +187,10 @@ module Twine
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 format_file(lang, default_lang)
f.puts format_file(lang)
end
end

View file

@ -91,14 +91,18 @@ module Twine
end
end
def fallback_language(lang)
DEFAULT_LANG_CODES[lang] || super
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, default_lang)
def format_sections(lang)
result = '<resources>'
result += super(lang, default_lang) + "\n"
result += super(lang) + "\n"
result += '</resources>'
end