From 793590be4d4aac11d946f81ae8ceac9f9f5d5029 Mon Sep 17 00:00:00 2001 From: Michael Harrison Date: Mon, 10 Jun 2024 21:12:18 +1000 Subject: [PATCH] Feature: Ignore YAML keys using RegEx (#56) * Added support for regular expressions in the ignored_key_prefixes * Updated documentation --------- Co-authored-by: Michael Harrison --- README.md | 5 +- lib/translation_io/yaml_entry.rb | 14 +++- spec/translation/yaml_entry_spec.rb | 100 ++++++++++++++++++++-------- 3 files changed, 88 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index fe6bd10..2357fa8 100644 --- a/README.md +++ b/README.md @@ -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| @@ -423,7 +423,8 @@ TranslationIO.configure do |config| 'will_paginate', 'helpers.page_entries_info', 'views.pagination', - 'enumerize.visibility' + 'enumerize.visibility', + /\.code$/ ] ... end diff --git a/lib/translation_io/yaml_entry.rb b/lib/translation_io/yaml_entry.rb index 873b239..5aebb81 100644 --- a/lib/translation_io/yaml_entry.rb +++ b/lib/translation_io/yaml_entry.rb @@ -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) @@ -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 diff --git a/spec/translation/yaml_entry_spec.rb b/spec/translation/yaml_entry_spec.rb index 9aaf807..f46413e 100644 --- a/spec/translation/yaml_entry_spec.rb +++ b/spec/translation/yaml_entry_spec.rb @@ -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 @@ -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