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

Commit af0e334

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 af0e334

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-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

+20
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,25 @@ def nest_hash hash
3737
result
3838
end
3939
module_function :nest_hash
40+
41+
##
42+
# Replaces from_locale with to_locale in filename of path
43+
# Supports filenames with locale prefix or suffix
44+
#
45+
# @param [String] from_locale
46+
# @param [String] to_locale
47+
# @param [String] path
48+
#
49+
# @example Get path for en locale path from existing dk locale path
50+
# Transformation.replace_locale_in_path('dk', 'en', '/tmp/dk.foo.yml') #=> "/tmp/en.foo.yml"
51+
# Transformation.replace_locale_in_path('dk', 'en', '/tmp/foo.dk.yml') #=> "/tmp/foo.en.yml"
52+
#
53+
# @return [String]
54+
def replace_locale_in_path(from_locale, to_locale, path)
55+
parts = File.basename(path).split('.')
56+
parts[parts.index(from_locale)] = to_locale
57+
File.join(File.dirname(path), parts.join('.'))
58+
end
59+
module_function :replace_locale_in_path
4060
end
4161
end

test/unit/test_store.rb

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

105+
def test_create_missing_keys_uses_replace_locale_in_path_transformation
106+
calls = []
107+
@store.define_singleton_method(:replace_locale_in_path) do |*args|
108+
calls << args
109+
"/tmp/special_path/en.yml"
110+
end
111+
112+
@store.add_translation Translation.new(name: "da.app_name", text: "Oversætter", file: "/tmp/da.yml")
113+
@store.add_locale("en")
114+
@store.create_missing_keys
115+
116+
assert_equal(1, calls.count)
117+
assert_equal(['da', 'en', '/tmp/da.yml'], calls.first)
118+
assert_equal "/tmp/special_path/en.yml", @store.translations["en.app_name"].file
119+
end
120+
105121
def test_from_yaml
106122
input = {
107123
da: {

test/unit/test_transformation.rb

+8
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ def test_nest_hash
4141

4242
assert_equal expected, Transformation.nest_hash(input)
4343
end
44+
45+
def test_replace_locale_in_path_suffixed
46+
assert_equal '/tmp/en.foo.yml', Transformation.replace_locale_in_path('dk', 'en', '/tmp/dk.foo.yml')
47+
end
48+
49+
def test_create_missing_translations_in_prefix_named_file
50+
assert_equal '/tmp/foo.en.yml', Transformation.replace_locale_in_path('dk', 'en', '/tmp/foo.dk.yml')
51+
end
4452
end

0 commit comments

Comments
 (0)