This repository was archived by the owner on Jan 29, 2024. It is now read-only.
File tree 3 files changed +32
-10
lines changed
3 files changed +32
-10
lines changed Original file line number Diff line number Diff line change @@ -75,16 +75,7 @@ def create_missing_keys
75
75
missing_locales = self . locales - key . translations . map ( &:locale )
76
76
missing_locales . each { |locale |
77
77
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 )
88
79
new_translation = Translation . new ( name : "#{ locale } .#{ key . name } " , file : path )
89
80
add_translation ( new_translation )
90
81
}
Original file line number Diff line number Diff line change @@ -37,5 +37,12 @@ def nest_hash hash
37
37
result
38
38
end
39
39
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
40
47
end
41
48
end
Original file line number Diff line number Diff line change @@ -102,6 +102,30 @@ def test_create_missing_translations_in_top_level_file
102
102
assert_nil translation . text
103
103
end
104
104
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
+
105
129
def test_from_yaml
106
130
input = {
107
131
da : {
You can’t perform that action at this time.
0 commit comments