Skip to content

Commit 4b83db9

Browse files
committed
Small refactoring on ignored_key_prefixes
1 parent 793590b commit 4b83db9

File tree

3 files changed

+71
-75
lines changed

3 files changed

+71
-75
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,11 @@ end
408408
### Ignored YAML keys
409409

410410
Sometimes you would like to ignore some YAML keys coming from gems or so.
411-
You can use the `ignored_key_prefixes` for that.
412411

413-
For example, this can be a mix of strings and regular expressions:
412+
You can use the `ignored_key_prefixes` array with a mix or strings and
413+
regular expressions.
414+
415+
For example:
414416

415417
~~~ruby
416418
TranslationIO.configure do |config|

lib/translation_io/yaml_entry.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def from_locale?(key, locale)
3131
end
3232

3333
def ignored?(key)
34-
key.present? && ignored_key_prefixes.any? { |prefix| ignore_using_string(key, prefix) || ignore_using_regex(key, prefix) }
34+
key.present? && ignored_key_prefixes.any? { |prefix| ignored_prefix?(key, prefix) || ignored_regex?(key, prefix) }
3535
end
3636

3737
def localization?(key, value)
@@ -44,16 +44,21 @@ def localization_prefix?(key)
4444

4545
private
4646

47-
def ignore_using_string(key, prefix)
48-
return unless prefix.is_a?(String)
49-
50-
key_without_locale(key).match(/^#{Regexp.escape(prefix)}\b/) != nil
47+
def ignored_prefix?(key, prefix)
48+
if prefix.is_a?(String)
49+
regex = /^#{Regexp.escape(prefix)}\b/
50+
key_without_locale(key).match(regex) != nil
51+
else
52+
false
53+
end
5154
end
5255

53-
def ignore_using_regex(key, prefix)
54-
return unless prefix.is_a?(Regexp)
55-
56-
key_without_locale(key).scan(prefix).flatten.compact.uniq.count > 0
56+
def ignored_regex?(key, regex)
57+
if regex.is_a?(Regexp)
58+
key_without_locale(key).match(regex) != nil
59+
else
60+
false
61+
end
5762
end
5863

5964
def localization_key_prefixes

spec/translation/yaml_entry_spec.rb

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,63 @@
2424
end
2525

2626
describe '#ignored?' do
27-
context 'when using a string' do
28-
it do
29-
TranslationIO::YamlEntry.ignored?('en.faker.yo').should be true
30-
TranslationIO::YamlEntry.ignored?('en.faker').should be true
31-
TranslationIO::YamlEntry.ignored?('en.faker.aa.aa.bb').should be true
32-
TranslationIO::YamlEntry.ignored?('en.yo').should be false
33-
TranslationIO::YamlEntry.ignored?('en.fakeryo').should be false
34-
TranslationIO::YamlEntry.ignored?('fr.faker').should be true
35-
36-
TranslationIO.config.ignored_key_prefixes = ['world']
37-
38-
TranslationIO::YamlEntry.ignored?('en.world').should be true
39-
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
40-
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
41-
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
42-
43-
TranslationIO.config.ignored_key_prefixes = ['world.']
44-
45-
TranslationIO::YamlEntry.ignored?('en.world').should be false
46-
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
47-
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
48-
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
49-
50-
# check "." on ignored key prefix is not used as special char in the regexp
51-
TranslationIO::YamlEntry.ignored?('fr.worlda').should be false
52-
end
27+
it 'works with default ignored keys' do
28+
TranslationIO::YamlEntry.ignored?('en.faker.yo' ).should be true
29+
TranslationIO::YamlEntry.ignored?('en.faker' ).should be true
30+
TranslationIO::YamlEntry.ignored?('en.faker.aa.aa.bb').should be true
31+
TranslationIO::YamlEntry.ignored?('en.yo' ).should be false
32+
TranslationIO::YamlEntry.ignored?('en.fakeryo' ).should be false
33+
TranslationIO::YamlEntry.ignored?('fr.faker' ).should be true
5334
end
5435

55-
context 'when using a regular expression' do
56-
it do
57-
TranslationIO::YamlEntry.ignored?('en.faker.yo').should be true
58-
TranslationIO::YamlEntry.ignored?('en.faker').should be true
59-
TranslationIO::YamlEntry.ignored?('en.faker.aa.aa.bb').should be true
60-
TranslationIO::YamlEntry.ignored?('en.yo').should be false
61-
TranslationIO::YamlEntry.ignored?('en.fakeryo').should be false
62-
TranslationIO::YamlEntry.ignored?('fr.faker').should be true
63-
64-
TranslationIO.config.ignored_key_prefixes = [
65-
/\.do_not_translate$/,
66-
/^world$|^world\..+$/,
67-
]
68-
69-
TranslationIO::YamlEntry.ignored?('en.world').should be true
70-
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
71-
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
72-
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
73-
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello').should be false
74-
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
75-
end
36+
it 'works with ignored prefix strings' do
37+
TranslationIO.config.ignored_key_prefixes = ['world']
38+
39+
TranslationIO::YamlEntry.ignored?('en.world' ).should be true
40+
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
41+
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
42+
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
43+
44+
TranslationIO.config.ignored_key_prefixes = ['world.']
45+
46+
TranslationIO::YamlEntry.ignored?('en.world' ).should be false
47+
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
48+
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
49+
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
50+
51+
# check "." on ignored key prefix is not used as special char in the regexp
52+
TranslationIO::YamlEntry.ignored?('fr.worlda').should be false
53+
end
54+
55+
it 'works with ignored regular expression' do
56+
TranslationIO.config.ignored_key_prefixes = [
57+
/\.do_not_translate$/,
58+
/^world$|^world\..+$/,
59+
]
60+
61+
TranslationIO::YamlEntry.ignored?('en.world' ).should be true
62+
TranslationIO::YamlEntry.ignored?('en.world.hello' ).should be true
63+
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
64+
TranslationIO::YamlEntry.ignored?('fr.world.hello' ).should be true
65+
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello' ).should be false
66+
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
7667
end
7768

78-
context 'when using a mix of regular expression and strings' do
79-
it do
80-
TranslationIO.config.ignored_key_prefixes = [
81-
/\.do_not_translate$/,
82-
/^world$|^world\..+$/,
83-
"mars"
84-
]
85-
86-
TranslationIO::YamlEntry.ignored?('en.world').should be true
87-
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
88-
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
89-
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
90-
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello').should be false
91-
TranslationIO::YamlEntry.ignored?('fr.mars.hello').should be true
92-
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
93-
TranslationIO::YamlEntry.ignored?('fr.mars_attacks.world').should be false
94-
end
69+
it 'works when using a mix of ignored prefix strings and regular expressions' do
70+
TranslationIO.config.ignored_key_prefixes = [
71+
/\.do_not_translate$/,
72+
/^world$|^world\..+$/,
73+
"mars"
74+
]
75+
76+
TranslationIO::YamlEntry.ignored?('en.world' ).should be true
77+
TranslationIO::YamlEntry.ignored?('en.world.hello' ).should be true
78+
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
79+
TranslationIO::YamlEntry.ignored?('fr.world.hello' ).should be true
80+
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello' ).should be false
81+
TranslationIO::YamlEntry.ignored?('fr.mars.hello' ).should be true
82+
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
83+
TranslationIO::YamlEntry.ignored?('fr.mars_attacks.world' ).should be false
9584
end
9685
end
9786

0 commit comments

Comments
 (0)