From ef1e684824865249dce98e00da0978db993c5dca Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Mon, 24 Aug 2015 09:07:22 +0200 Subject: [PATCH] Fixed --include-untranslated handling. Before even untranslated keys were included if a formatter didn't return nil als fallback language (which would have caused a crash). --- lib/twine/formatters/abstract.rb | 8 ++++---- lib/twine/formatters/android.rb | 4 ++-- lib/twine/stringsfile.rb | 10 ++++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/twine/formatters/abstract.rb b/lib/twine/formatters/abstract.rb index e273f5f..2207cdb 100644 --- a/lib/twine/formatters/abstract.rb +++ b/lib/twine/formatters/abstract.rb @@ -117,8 +117,8 @@ module Twine @options[:developer_language] || @strings.language_codes[0] end - def fallback_language(lang) - default_language + def fallback_languages(lang) + [default_language] end def format_file(lang) @@ -156,9 +156,9 @@ module Twine end def format_row(row, lang) - value = row.translated_string_for_lang(lang, fallback_language(lang)) + value = row.translated_string_for_lang(lang) if value.nil? && @options[:include_untranslated] - value = row.translated_string_for_lang(default_language) + value = row.translated_string_for_lang(fallback_languages(lang)) end return nil unless value diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb index 6dad302..a643a4f 100644 --- a/lib/twine/formatters/android.rb +++ b/lib/twine/formatters/android.rb @@ -91,8 +91,8 @@ module Twine end end - def fallback_language(lang) - DEFAULT_LANG_CODES[lang] || super + def fallback_languages(lang) + [DEFAULT_LANG_CODES[lang], super].flatten.compact end def format_header(lang) diff --git a/lib/twine/stringsfile.rb b/lib/twine/stringsfile.rb index 9de932c..26987ab 100644 --- a/lib/twine/stringsfile.rb +++ b/lib/twine/stringsfile.rb @@ -41,16 +41,18 @@ module Twine end def translated_string_for_lang(lang, default_lang=nil) - if @translations[lang] - return @translations[lang] - elsif default_lang.respond_to?("each") + translation = [lang].flatten.map { |l| @translations[l] }.first + return translation if translation + + # TODO: get rid of all this and the default_lang parameter once all formatters are converted to the new style + if default_lang.respond_to?("each") default_lang.each do |def_lang| if @translations[def_lang] return @translations[def_lang] end end return nil - else + elsif default_lang return @translations[default_lang] end end