diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a6a4cee..686ad3c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ Prefix your message with one of the following: - [Security] in case of vulnerabilities. --> +## Unreleased + +- [Changed] Allow glob patterns in lint:translations. + ## v4.2.3 - Mar 29, 2023 - [Fixed] Load plugins when running `i18n lint:*` commands. diff --git a/README.md b/README.md index 60f7b84e..445b1ed3 100644 --- a/README.md +++ b/README.md @@ -351,13 +351,14 @@ lint_translations: ignore: - en.mailer.login.subject - en.mailer.login.body + - en.faker.* ``` > **Note**: > -> In order to avoid mistakenly ignoring keys, this configuration option only -> accepts the full translation scope, rather than accepting a pattern like -> `pt.ignored.scope.*`. +> In order to avoid mistakenly ignoring keys, prefer using a full list of translations +> instead of using a pattern like `pt.ignored.scope.*`. We recommend only using +> patterns for excluding scopes coming from modules, such as `en.faker.*`. ### Linting your JavaScript/TypeScript files diff --git a/lib/i18n-js/cli/lint_translations_command.rb b/lib/i18n-js/cli/lint_translations_command.rb index fd142439..45047913 100644 --- a/lib/i18n-js/cli/lint_translations_command.rb +++ b/lib/i18n-js/cli/lint_translations_command.rb @@ -69,7 +69,7 @@ class LintTranslationsCommand < Command config = load_config_file(config_file) I18nJS.load_plugins! - I18nJS.initialize_plugins!(config:) + I18nJS.initialize_plugins!(config: config) Schema.validate!(config) load_require_file!(require_file) if require_file @@ -103,20 +103,20 @@ class LintTranslationsCommand < Command filtered_keys = partial_keys.reject do |key| key = "#{locale}.#{key}" - ignored = ignored_keys.include?(key) + ignored = key_matches?(key, ignored_keys) ignored_count += 1 if ignored ignored end extraneous = (partial_keys - default_locale_keys).reject do |key| key = "#{locale}.#{key}" - ignored = ignored_keys.include?(key) + ignored = key_matches?(key, ignored_keys) ignored_count += 1 if ignored ignored end missing = (default_locale_keys - (filtered_keys - extraneous)) - .reject {|key| ignored_keys.include?("#{locale}.#{key}") } + .reject {|key| key_matches?("#{locale}.#{key}", ignored_keys) } ignored_count += extraneous.size total_missing_count += missing.size @@ -128,7 +128,7 @@ class LintTranslationsCommand < Command all_keys = (default_locale_keys + extraneous + missing).uniq.sort all_keys.each do |key| - next if ignored_keys.include?("#{locale}.#{key}") + next if key_matches?("#{locale}.#{key}", ignored_keys) label = if extraneous.include?(key) ui.yellow("extraneous") @@ -145,6 +145,9 @@ class LintTranslationsCommand < Command exit(1) if total_missing_count.nonzero? end + private def key_matches?(key, ignored_keys) + ignored_keys.include?(key) || ignored_keys.any? { |ignored_key| File.fnmatch?(ignored_key, key) } + end private def set_defaults! config_file = "./config/i18n.yml" require_file = "./config/environment.rb"