Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Add support for yml files with format locale.something.yml
Browse files Browse the repository at this point in the history
Extract path transformation helper #replace_locale_in_path
  • Loading branch information
Christoph committed Nov 26, 2015
1 parent 241fb27 commit af0e334
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
11 changes: 1 addition & 10 deletions lib/i18n_yaml_editor/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions lib/i18n_yaml_editor/transformation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions test/unit/test_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@1st8

1st8 Nov 26, 2015

Contributor

Couldn't find a way to express rspecs expect(@store).to receive(: replace_locale_in_path).with(args).and_return(path) with minitest. Is there an idiomatic way to do this?

"/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: {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test_transformation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit af0e334

Please sign in to comment.