Skip to content

Commit

Permalink
Small refactoring on ignored_key_prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHoste committed Jun 10, 2024
1 parent 793590b commit 4b83db9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 75 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,11 @@ end
### Ignored YAML keys

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

For example, this can be a mix of strings and regular expressions:
You can use the `ignored_key_prefixes` array with a mix or strings and
regular expressions.

For example:

~~~ruby
TranslationIO.configure do |config|
Expand Down
23 changes: 14 additions & 9 deletions lib/translation_io/yaml_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def from_locale?(key, locale)
end

def ignored?(key)
key.present? && ignored_key_prefixes.any? { |prefix| ignore_using_string(key, prefix) || ignore_using_regex(key, prefix) }
key.present? && ignored_key_prefixes.any? { |prefix| ignored_prefix?(key, prefix) || ignored_regex?(key, prefix) }
end

def localization?(key, value)
Expand All @@ -44,16 +44,21 @@ def localization_prefix?(key)

private

def ignore_using_string(key, prefix)
return unless prefix.is_a?(String)

key_without_locale(key).match(/^#{Regexp.escape(prefix)}\b/) != nil
def ignored_prefix?(key, prefix)
if prefix.is_a?(String)
regex = /^#{Regexp.escape(prefix)}\b/
key_without_locale(key).match(regex) != nil
else
false
end
end

def ignore_using_regex(key, prefix)
return unless prefix.is_a?(Regexp)

key_without_locale(key).scan(prefix).flatten.compact.uniq.count > 0
def ignored_regex?(key, regex)
if regex.is_a?(Regexp)
key_without_locale(key).match(regex) != nil
else
false
end
end

def localization_key_prefixes
Expand Down
117 changes: 53 additions & 64 deletions spec/translation/yaml_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,74 +24,63 @@
end

describe '#ignored?' do
context 'when using a string' do
it do
TranslationIO::YamlEntry.ignored?('en.faker.yo').should be true
TranslationIO::YamlEntry.ignored?('en.faker').should be true
TranslationIO::YamlEntry.ignored?('en.faker.aa.aa.bb').should be true
TranslationIO::YamlEntry.ignored?('en.yo').should be false
TranslationIO::YamlEntry.ignored?('en.fakeryo').should be false
TranslationIO::YamlEntry.ignored?('fr.faker').should be true

TranslationIO.config.ignored_key_prefixes = ['world']

TranslationIO::YamlEntry.ignored?('en.world').should be true
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true

TranslationIO.config.ignored_key_prefixes = ['world.']

TranslationIO::YamlEntry.ignored?('en.world').should be false
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true

# check "." on ignored key prefix is not used as special char in the regexp
TranslationIO::YamlEntry.ignored?('fr.worlda').should be false
end
it 'works with default ignored keys' do
TranslationIO::YamlEntry.ignored?('en.faker.yo' ).should be true
TranslationIO::YamlEntry.ignored?('en.faker' ).should be true
TranslationIO::YamlEntry.ignored?('en.faker.aa.aa.bb').should be true
TranslationIO::YamlEntry.ignored?('en.yo' ).should be false
TranslationIO::YamlEntry.ignored?('en.fakeryo' ).should be false
TranslationIO::YamlEntry.ignored?('fr.faker' ).should be true
end

context 'when using a regular expression' do
it do
TranslationIO::YamlEntry.ignored?('en.faker.yo').should be true
TranslationIO::YamlEntry.ignored?('en.faker').should be true
TranslationIO::YamlEntry.ignored?('en.faker.aa.aa.bb').should be true
TranslationIO::YamlEntry.ignored?('en.yo').should be false
TranslationIO::YamlEntry.ignored?('en.fakeryo').should be false
TranslationIO::YamlEntry.ignored?('fr.faker').should be true

TranslationIO.config.ignored_key_prefixes = [
/\.do_not_translate$/,
/^world$|^world\..+$/,
]

TranslationIO::YamlEntry.ignored?('en.world').should be true
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello').should be false
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
end
it 'works with ignored prefix strings' do
TranslationIO.config.ignored_key_prefixes = ['world']

TranslationIO::YamlEntry.ignored?('en.world' ).should be true
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true

TranslationIO.config.ignored_key_prefixes = ['world.']

TranslationIO::YamlEntry.ignored?('en.world' ).should be false
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true

# check "." on ignored key prefix is not used as special char in the regexp
TranslationIO::YamlEntry.ignored?('fr.worlda').should be false
end

it 'works with ignored regular expression' do
TranslationIO.config.ignored_key_prefixes = [
/\.do_not_translate$/,
/^world$|^world\..+$/,
]

TranslationIO::YamlEntry.ignored?('en.world' ).should be true
TranslationIO::YamlEntry.ignored?('en.world.hello' ).should be true
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello' ).should be true
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello' ).should be false
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
end

context 'when using a mix of regular expression and strings' do
it do
TranslationIO.config.ignored_key_prefixes = [
/\.do_not_translate$/,
/^world$|^world\..+$/,
"mars"
]

TranslationIO::YamlEntry.ignored?('en.world').should be true
TranslationIO::YamlEntry.ignored?('en.world.hello').should be true
TranslationIO::YamlEntry.ignored?('en.worldbla').should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello').should be true
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello').should be false
TranslationIO::YamlEntry.ignored?('fr.mars.hello').should be true
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
TranslationIO::YamlEntry.ignored?('fr.mars_attacks.world').should be false
end
it 'works when using a mix of ignored prefix strings and regular expressions' do
TranslationIO.config.ignored_key_prefixes = [
/\.do_not_translate$/,
/^world$|^world\..+$/,
"mars"
]

TranslationIO::YamlEntry.ignored?('en.world' ).should be true
TranslationIO::YamlEntry.ignored?('en.world.hello' ).should be true
TranslationIO::YamlEntry.ignored?('en.worldbla' ).should be false
TranslationIO::YamlEntry.ignored?('fr.world.hello' ).should be true
TranslationIO::YamlEntry.ignored?('fr.yet.another.world.hello' ).should be false
TranslationIO::YamlEntry.ignored?('fr.mars.hello' ).should be true
TranslationIO::YamlEntry.ignored?('fr.mars.hello.do_not_translate').should be true
TranslationIO::YamlEntry.ignored?('fr.mars_attacks.world' ).should be false
end
end

Expand Down

0 comments on commit 4b83db9

Please sign in to comment.