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
  • Loading branch information
Christoph committed Nov 23, 2015
1 parent 241fb27 commit dd4cf0c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/i18n_yaml_editor/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ def create_missing_keys
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
# this just replaces the locale part of the file name
path = translation.file
.sub(/(\/|\.)#{translation.locale}\.yml$/, "\\1#{locale}.yml")
.sub(/\/#{translation.locale}([^\/]+)\.yml$/, "/#{locale}\\1.yml")

new_translation = Translation.new(name: "#{locale}.#{key.name}", file: path)
add_translation(new_translation)
Expand Down
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 dd4cf0c

Please sign in to comment.