Skip to content

Commit

Permalink
Feature: Ignore YAML keys using RegEx (#56)
Browse files Browse the repository at this point in the history
* Added support for regular expressions in the ignored_key_prefixes

* Updated documentation

---------

Co-authored-by: Michael Harrison <michael@ntechmedia.com>
  • Loading branch information
michael-harrison and Michael Harrison committed Jun 10, 2024
1 parent c699ae6 commit 793590b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 31 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ end
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:
For example, this can be a mix of strings and regular expressions:

~~~ruby
TranslationIO.configure do |config|
Expand All @@ -423,7 +423,8 @@ TranslationIO.configure do |config|
'will_paginate',
'helpers.page_entries_info',
'views.pagination',
'enumerize.visibility'
'enumerize.visibility',
/\.code$/
]
...
end
Expand Down
14 changes: 13 additions & 1 deletion 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| key_without_locale(key).match(/^#{Regexp.escape(prefix)}\b/) != nil }
key.present? && ignored_key_prefixes.any? { |prefix| ignore_using_string(key, prefix) || ignore_using_regex(key, prefix) }
end

def localization?(key, value)
Expand All @@ -44,6 +44,18 @@ 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
end

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

key_without_locale(key).scan(prefix).flatten.compact.uniq.count > 0
end

def localization_key_prefixes
if TranslationIO.config
LOCALIZATION_KEY_PREFIXES + TranslationIO.config.localization_key_prefixes
Expand Down
100 changes: 72 additions & 28 deletions spec/translation/yaml_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,74 @@
end

describe '#ignored?' 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.']
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
end

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
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
end

# check "." on ignored key prefix is not used as special char in the regexp
TranslationIO::YamlEntry.ignored?('fr.worlda').should be false
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
end
end

Expand Down Expand Up @@ -79,14 +123,14 @@

describe '#localization_prefix?' do
it do
TranslationIO::YamlEntry.localization_prefix?('en.date.formats.default' ).should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.formats.default').should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.formatsss.default').should be false
TranslationIO::YamlEntry.localization_prefix?('en.date.order[0]' ).should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.order[1]' ).should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.order[2]' ).should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.orders[2]' ).should be false
TranslationIO::YamlEntry.localization_prefix?('en.date.order[0]').should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.order[1]').should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.order[2]').should be true
TranslationIO::YamlEntry.localization_prefix?('en.date.orders[2]').should be false

TranslationIO::YamlEntry.localization_prefix?('en.yo' ).should be false
TranslationIO::YamlEntry.localization_prefix?('en.yo').should be false
TranslationIO::YamlEntry.localization_prefix?('en.number.human.decimal_units.units.thousand').should be false
end
end
Expand Down

0 comments on commit 793590b

Please sign in to comment.