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 e243cd3
Show file tree
Hide file tree
Showing 3 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
24 changes: 24 additions & 0 deletions test/unit/test_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ def test_create_missing_translations_in_top_level_file
assert_nil translation.text
end

def test_create_missing_translations_in_suffix_named_file
@store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/something.foo.da.yml")
@store.add_locale("en")

@store.create_missing_keys

assert(translation = @store.translations["en.app_name"])
assert_equal "en.app_name", translation.name
assert_equal "/tmp/something.foo.en.yml", translation.file
assert_nil translation.text
end

def test_create_missing_translations_in_prefix_named_file
@store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/da.something.foo.yml")
@store.add_locale("en")

@store.create_missing_keys

assert(translation = @store.translations["en.app_name"])
assert_equal "en.app_name", translation.name
assert_equal "/tmp/en.something.foo.yml", translation.file
assert_nil translation.text
end

def test_from_yaml
input = {
da: {
Expand Down

0 comments on commit e243cd3

Please sign in to comment.