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

Commit dd4cf0c

Browse files
author
Christoph
committed
Add support for yml files with format locale.something.yml
1 parent 241fb27 commit dd4cf0c

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/i18n_yaml_editor/store.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,10 @@ def create_missing_keys
7676
missing_locales.each {|locale|
7777
translation = key.translations.first
7878

79-
# this just replaces the locale part of the file name. should
80-
# be possible to do in a simpler way. gsub, baby.
81-
path = Pathname.new(translation.file)
82-
dirs, file = path.split
83-
file = file.to_s.split(".")
84-
file[-2] = locale
85-
file = file.join(".")
86-
path = dirs.join(file).to_s
79+
# this just replaces the locale part of the file name
80+
path = translation.file
81+
.sub(/(\/|\.)#{translation.locale}\.yml$/, "\\1#{locale}.yml")
82+
.sub(/\/#{translation.locale}([^\/]+)\.yml$/, "/#{locale}\\1.yml")
8783

8884
new_translation = Translation.new(name: "#{locale}.#{key.name}", file: path)
8985
add_translation(new_translation)

test/unit/test_store.rb

+24
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ def test_create_missing_translations_in_top_level_file
102102
assert_nil translation.text
103103
end
104104

105+
def test_create_missing_translations_in_suffix_named_file
106+
@store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/something.foo.da.yml")
107+
@store.add_locale("en")
108+
109+
@store.create_missing_keys
110+
111+
assert(translation = @store.translations["en.app_name"])
112+
assert_equal "en.app_name", translation.name
113+
assert_equal "/tmp/something.foo.en.yml", translation.file
114+
assert_nil translation.text
115+
end
116+
117+
def test_create_missing_translations_in_prefix_named_file
118+
@store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/da.something.foo.yml")
119+
@store.add_locale("en")
120+
121+
@store.create_missing_keys
122+
123+
assert(translation = @store.translations["en.app_name"])
124+
assert_equal "en.app_name", translation.name
125+
assert_equal "/tmp/en.something.foo.yml", translation.file
126+
assert_nil translation.text
127+
end
128+
105129
def test_from_yaml
106130
input = {
107131
da: {

0 commit comments

Comments
 (0)