Skip to content

Commit 21c1eaa

Browse files
authored
Merge pull request #82 from aergonaut/pseudolocalize-translations-hash
Add support for pseudolocalized `translations`
2 parents 8fae663 + c444790 commit 21c1eaa

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/pseudolocalization.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ def translate(locale, key, options)
3131
::Pseudolocalization::I18n::Pseudolocalizer.pseudolocalize(original_backend.translate(locale, key, options))
3232
end
3333

34+
def translations
35+
original = original_backend.translations
36+
original.transform_values do |locale_translations|
37+
pseudolocalize_node(locale_translations)
38+
end
39+
end
40+
3441
private
3542

3643
def key_ignored?(key)
@@ -48,6 +55,22 @@ def key_ignored?(key)
4855
end
4956
end
5057
end
58+
59+
def pseudolocalize_node(node, scope = [])
60+
if node.is_a?(Hash)
61+
node.each_with_object({}) do |(key, value), memo|
62+
memo[key] = pseudolocalize_node(value, scope + [key])
63+
end
64+
elsif (node.is_a?(String) || node.is_a?(Array)) && !key_ignored?(scope.join('.'))
65+
::Pseudolocalization::I18n::Pseudolocalizer.pseudolocalize(node)
66+
else
67+
node
68+
end
69+
end
70+
71+
def init_translations
72+
original_backend.send(:init_translations)
73+
end
5174
end
5275
end
5376
end

test/pseudolocalization_test.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
class PseudolocalizationTest < Minitest::Test
44
class DummyBackend
5+
attr_accessor :translations
6+
7+
def initialize(translations = {})
8+
@translations = translations
9+
end
10+
511
def translate(_locale, string, _options)
612
string
713
end
@@ -67,4 +73,52 @@ def test_it_allows_ignoring_cetain_keys
6773
assert_equal('Ignore me, as well Clifford!', @backend.translate(:en, 'Ignore me, as well Clifford!', {}))
6874
assert_equal(['Ḥḛḛḽḽṓṓ, ẁṓṓṛḽḍ!'], @backend.translate(:en, ['Hello, world!'], {}))
6975
end
76+
77+
def test_it_exposes_pseudo_localized_translations
78+
translations = {
79+
en: {
80+
date: {
81+
day_names: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
82+
},
83+
hello: 'Hello, world!',
84+
ignored: {
85+
foobar: "Foobar"
86+
}
87+
},
88+
es: {
89+
date: {
90+
day_names: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes'],
91+
},
92+
hello: 'Hola, mundo!',
93+
ignored: {
94+
foobar: "Foobar"
95+
}
96+
}
97+
}
98+
expected = {
99+
en: {
100+
date: {
101+
day_names: ["Ṣṵṵṇḍααẏẏ", "Ṁṓṓṇḍααẏẏ", "Ṫṵṵḛḛṡḍααẏẏ", "Ŵḛḛḍṇḛḛṡḍααẏẏ", "Ṫḥṵṵṛṡḍααẏẏ", "Ḟṛḭḭḍααẏẏ"],
102+
},
103+
hello: "Ḥḛḛḽḽṓṓ, ẁṓṓṛḽḍ!",
104+
ignored: {
105+
foobar: "Foobar"
106+
}
107+
},
108+
es: {
109+
date: {
110+
day_names: ["Ḍṓṓṃḭḭṇḡṓṓ", "Ḻṵṵṇḛḛṡ", "Ṁααṛṭḛḛṡ", "Ṁḭḭéṛͼṓṓḽḛḛṡ", "Ĵṵṵḛḛṽḛḛṡ", "Ṿḭḭḛḛṛṇḛḛṡ"],
111+
},
112+
hello: "Ḥṓṓḽαα, ṃṵṵṇḍṓṓ!",
113+
ignored: {
114+
foobar: "Foobar"
115+
}
116+
}
117+
}
118+
119+
@backend = Pseudolocalization::I18n::Backend.new(DummyBackend.new(translations))
120+
@backend.ignores = ["ignored*"]
121+
122+
assert_equal(expected, @backend.translations)
123+
end
70124
end

0 commit comments

Comments
 (0)