Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement glob patterns for lint translations #724

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 8 additions & 5 deletions lib/i18n-js/cli/lint_translations_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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"
Expand Down