From af0e3349fd4f25e05ec330314e38e2010cb18a0e Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 21 Oct 2015 14:09:43 +0200 Subject: [PATCH] Add support for yml files with format locale.something.yml Extract path transformation helper #replace_locale_in_path --- lib/i18n_yaml_editor/store.rb | 11 +---------- lib/i18n_yaml_editor/transformation.rb | 20 ++++++++++++++++++++ test/unit/test_store.rb | 16 ++++++++++++++++ test/unit/test_transformation.rb | 8 ++++++++ 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/i18n_yaml_editor/store.rb b/lib/i18n_yaml_editor/store.rb index ffc4d33..2e090fd 100644 --- a/lib/i18n_yaml_editor/store.rb +++ b/lib/i18n_yaml_editor/store.rb @@ -75,16 +75,7 @@ def create_missing_keys missing_locales = self.locales - key.translations.map(&:locale) missing_locales.each {|locale| translation = key.translations.first - - # this just replaces the locale part of the file name. should - # be possible to do in a simpler way. gsub, baby. - path = Pathname.new(translation.file) - dirs, file = path.split - file = file.to_s.split(".") - file[-2] = locale - file = file.join(".") - path = dirs.join(file).to_s - + path = replace_locale_in_path(translation.locale, locale, translation.file) new_translation = Translation.new(name: "#{locale}.#{key.name}", file: path) add_translation(new_translation) } diff --git a/lib/i18n_yaml_editor/transformation.rb b/lib/i18n_yaml_editor/transformation.rb index c0c7cab..f5eff3a 100644 --- a/lib/i18n_yaml_editor/transformation.rb +++ b/lib/i18n_yaml_editor/transformation.rb @@ -37,5 +37,25 @@ def nest_hash hash result end module_function :nest_hash + + ## + # Replaces from_locale with to_locale in filename of path + # Supports filenames with locale prefix or suffix + # + # @param [String] from_locale + # @param [String] to_locale + # @param [String] path + # + # @example Get path for en locale path from existing dk locale path + # Transformation.replace_locale_in_path('dk', 'en', '/tmp/dk.foo.yml') #=> "/tmp/en.foo.yml" + # Transformation.replace_locale_in_path('dk', 'en', '/tmp/foo.dk.yml') #=> "/tmp/foo.en.yml" + # + # @return [String] + def replace_locale_in_path(from_locale, to_locale, path) + parts = File.basename(path).split('.') + parts[parts.index(from_locale)] = to_locale + File.join(File.dirname(path), parts.join('.')) + end + module_function :replace_locale_in_path end end diff --git a/test/unit/test_store.rb b/test/unit/test_store.rb index 05fdbb3..14a6bab 100644 --- a/test/unit/test_store.rb +++ b/test/unit/test_store.rb @@ -102,6 +102,22 @@ def test_create_missing_translations_in_top_level_file assert_nil translation.text end + def test_create_missing_keys_uses_replace_locale_in_path_transformation + calls = [] + @store.define_singleton_method(:replace_locale_in_path) do |*args| + calls << args + "/tmp/special_path/en.yml" + end + + @store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/da.yml") + @store.add_locale("en") + @store.create_missing_keys + + assert_equal(1, calls.count) + assert_equal(['da', 'en', '/tmp/da.yml'], calls.first) + assert_equal "/tmp/special_path/en.yml", @store.translations["en.app_name"].file + end + def test_from_yaml input = { da: { diff --git a/test/unit/test_transformation.rb b/test/unit/test_transformation.rb index 0266837..aa9f07e 100644 --- a/test/unit/test_transformation.rb +++ b/test/unit/test_transformation.rb @@ -41,4 +41,12 @@ def test_nest_hash assert_equal expected, Transformation.nest_hash(input) end + + def test_replace_locale_in_path_suffixed + assert_equal '/tmp/en.foo.yml', Transformation.replace_locale_in_path('dk', 'en', '/tmp/dk.foo.yml') + end + + def test_create_missing_translations_in_prefix_named_file + assert_equal '/tmp/foo.en.yml', Transformation.replace_locale_in_path('dk', 'en', '/tmp/foo.dk.yml') + end end