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

Commit af4ab30

Browse files
author
Christoph
committed
Add support for yml files with format locale.something.yml
Extract path transformation helper #replace_locale_in_path
1 parent 241fb27 commit af4ab30

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

lib/i18n_yaml_editor/store.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,7 @@ def create_missing_keys
7575
missing_locales = self.locales - key.translations.map(&:locale)
7676
missing_locales.each {|locale|
7777
translation = key.translations.first
78-
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
87-
78+
path = replace_locale_in_path(translation.locale, locale, translation.file)
8879
new_translation = Translation.new(name: "#{locale}.#{key.name}", file: path)
8980
add_translation(new_translation)
9081
}

lib/i18n_yaml_editor/transformation.rb

+7
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,12 @@ def nest_hash hash
3737
result
3838
end
3939
module_function :nest_hash
40+
41+
def replace_locale_in_path(from_locale, to_locale, path)
42+
parts = File.basename(path).split('.')
43+
parts[parts.index(from_locale)] = to_locale
44+
File.join(File.dirname(path), parts.join('.'))
45+
end
46+
module_function :replace_locale_in_path
4047
end
4148
end

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)