diff --git a/Gemfile b/Gemfile index 8bd5b4f5..49de3e44 100644 --- a/Gemfile +++ b/Gemfile @@ -69,6 +69,7 @@ end group :development, :staging do gem 'better_errors' gem 'binding_of_caller' + gem 'i18n-tasks' gem 'letter_opener' gem 'listen' gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 571b7b55..991072b0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -184,6 +184,16 @@ GEM i18n-js (4.2.3) glob (>= 0.4.0) i18n + i18n-tasks (1.0.14) + activesupport (>= 4.0.2) + ast (>= 2.1.0) + erubi + highline (>= 2.0.0) + i18n + parser (>= 3.2.2.1) + rails-i18n + rainbow (>= 2.2.2, < 4.0) + terminal-table (>= 1.5.1) image_processing (1.12.2) mini_magick (>= 4.9.5, < 5) ruby-vips (>= 2.0.17, < 3) @@ -524,6 +534,8 @@ GEM strscan (3.1.0) telegraf (3.0.0) temple (0.10.3) + terminal-table (3.0.2) + unicode-display_width (>= 1.1.1, < 3) terser (1.2.2) execjs (>= 0.3.0, < 3) thor (1.3.1) @@ -595,6 +607,7 @@ DEPENDENCIES highline http_accept_language i18n-js + i18n-tasks ims-lti (< 2.0.0) jbuilder js-routes diff --git a/config/application.rb b/config/application.rb index 20ab6981..acdbc7b2 100644 --- a/config/application.rb +++ b/config/application.rb @@ -19,7 +19,7 @@ module CodeOcean # Please, add to the `ignore` list any other `lib` subdirectories that do # not contain `.rb` files, or that should not be reloaded or eager loaded. # Common ones are `templates`, `generators`, or `middleware`, for example. - config.autoload_lib(ignore: %w[assets tasks templates generators middleware]) + config.autoload_lib(ignore: %w[assets tasks templates generators middleware i18n_tasks]) # Configuration for the application, engines, and railties goes here. # diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml new file mode 100644 index 00000000..834a3e5d --- /dev/null +++ b/config/i18n-tasks.yml @@ -0,0 +1,165 @@ +<% require './lib/i18n_tasks/js_erb_locale_matcher.rb' %> +<% require './lib/i18n_tasks/slim_row_locale_matcher.rb' %> + +# i18n_tasks finds and manages missing and unused translations: https://github.com/glebm/i18n-tasks + +# The "main" locale. +base_locale: en +## All available locales are inferred from the data by default. Alternatively, specify them explicitly: +# locales: [es, fr] +## Reporting locale, default: en. Available: en, ru. +# internal_locale: en + +# Read and write translations. +data: + ## Translations are read from the file system. Supported format: YAML, JSON. + ## Provide a custom adapter: + # adapter: I18n::Tasks::Data::FileSystem + + # Locale files or `Find.find` patterns where translations are read from: + read: + ## Default: + # - config/locales/%{locale}.yml + ## More files: + # - config/locales/**/*.%{locale}.yml + - config/locales/%{locale}/*.yml + - config/locales/%{locale}/**/*.yml + + # Locale files to write new keys to, based on a list of key pattern => file rules. Matched from top to bottom: + # `i18n_tasks normalize -p` will force move the keys according to these rules + write: + - ['{application,breadcrumbs,locales,navigation,shared,will_paginate,activerecord.errors}.*', 'config/locales/%{locale}/meta/\1.yml'] + - ['activerecord.:.{admin,code_ocean}.{:}.*', 'config/locales/%{locale}/\1/\2.yml'] + - ['*.{*:}/:.*', 'config/locales/%{locale}/\1.yml'] + - ['activerecord.:.{*:}s?.*', 'config/locales/%{locale}/\1.yml'] + - ['{admin,code_ocean,linter,mailers}.{:}.*', 'config/locales/%{locale}/\1/\2.yml'] + - ['{*:}s?.*', 'config/locales/%{locale}/\1.yml'] + ## Catch-all default: + # - config/locales/%{locale}/unsorted.yml + + # External locale data (e.g. gems). + # This data is not considered unused and is never written to. + external: + ## Example (replace %#= with %=): + # - "<%#= %x[bundle info vagrant --path].chomp %>/templates/locales/%{locale}.yml" + + ## Specify the router (see Readme for details). Valid values: conservative_router, pattern_router, or a custom class. + # router: conservative_router + + yaml: + write: + # do not wrap lines at 80 characters + line_width: -1 + + ## Pretty-print JSON: + # json: + # write: + # indent: ' ' + # space: ' ' + # object_nl: "\n" + # array_nl: "\n" + +# Find translate calls +search: + ## Paths or `Find.find` patterns to search in: + paths: + - app/ + - spec/ + - app/assets/javascripts/ + + ## Root directories for relative keys resolution. + # relative_roots: + # - app/controllers + # - app/helpers + # - app/mailers + # - app/presenters + # - app/views + + ## Directories where method names which should not be part of a relative key resolution. + # By default, if a relative translation is used inside a method, the name of the method will be considered part of the resolved key. + # Directories listed here will not consider the name of the method part of the resolved key + # + # relative_exclude_method_name_paths: + # - + + ## Files or `File.fnmatch` patterns to exclude from search. Some files are always excluded regardless of this setting: + ## *.jpg *.jpeg *.png *.gif *.svg *.ico *.eot *.otf *.ttf *.woff *.woff2 *.pdf *.css *.sass *.scss *.less + ## *.yml *.json *.zip *.tar.gz *.swf *.flv *.mp3 *.wav *.flac *.webm *.mp4 *.ogg *.opus *.webp *.map *.xlsx + exclude: + - app/assets/images + - app/assets/fonts + - app/assets/videos + - app/assets/builds + - spec/fixtures + + ## Alternatively, the only files or `File.fnmatch patterns` to search in `paths`: + ## If specified, this settings takes priority over `exclude`, but `exclude` still applies. + # only: ["*.rb", "*.html.slim"] + + ## If `strict` is `false`, guess usages such as t("categories.#{category}.title"). The default is `true`. + # strict: true + + ## Allows adding ast_matchers for finding translations using the AST-scanners + ## The available matchers are: + ## - RailsModelMatcher + ## Matches ActiveRecord translations like + ## User.human_attribute_name(:email) and User.model_name.human + ## + ## To implement your own, please see `I18n::Tasks::Scanners::AstMatchers::BaseMatcher`. + # <%# I18n::Tasks.add_ast_matcher('I18n::Tasks::Scanners::AstMatchers::RailsModelMatcher') %> + + ## Multiple scanners can be used. Their results are merged. + ## The options specified above are passed down to each scanner. Per-scanner options can be specified as well. + ## See this example of a custom scanner: https://github.com/glebm/i18n-tasks/wiki/A-custom-scanner-example + +## Translation Services +# translation: +# # Google Translate +# # Get an API key and set billing info at https://code.google.com/apis/console to use Google Translate +# google_translate_api_key: "AbC-dEf5" +# # DeepL Pro Translate +# # Get an API key and subscription at https://www.deepl.com/pro to use DeepL Pro +# deepl_api_key: "48E92789-57A3-466A-9959-1A1A1A1A1A1A" +# # deepl_host: "https://api.deepl.com" +# # deepl_version: "v2" +# # add additional options to the DeepL.translate call: https://www.deepl.com/docs-api/translate-text/translate-text/ +# deepl_options: +# formality: prefer_less +## Do not consider these keys missing: +ignore_missing: + - 'linter.*' + +## Consider these keys used: +ignore_unused: + - 'activerecord.{attributes,errors,models}.*' + - 'linter.*' + - 'will_paginate.*' + +## Exclude these keys from the `i18n_tasks eq-base' report: +# ignore_eq_base: +# all: +# - common.ok +# fr,es: +# - common.brand + +## Exclude these keys from the `i18n_tasks check-consistent-interpolations` report: +ignore_inconsistent_interpolations: + - 'shared.errors_one' + - 'shared.errors_other' + +## Ignore these keys completely: +# ignore: + + +## Sometimes, it isn't possible for i18n_tasks to match the key correctly, +## e.g. in case of a relative key defined in a helper method. +## In these cases you can use the built-in PatternMapper to map patterns to keys, e.g.: +# +# <%# I18n::Tasks.add_scanner 'I18n::Tasks::Scanners::PatternMapper', +# only: %w(*.html.haml *.html.slim), +# patterns: [['= title\b', '.page_title']] %> +# +# The PatternMapper can also match key literals via a special %{key} interpolation, e.g.: +# +# <%# I18n::Tasks.add_scanner 'I18n::Tasks::Scanners::PatternMapper', +# patterns: [['\bSpree\.t[( ]\s*%{key}', 'spree.%{key}']] %> diff --git a/lib/i18n_tasks/js_erb_locale_matcher.rb b/lib/i18n_tasks/js_erb_locale_matcher.rb new file mode 100644 index 00000000..a0186a4d --- /dev/null +++ b/lib/i18n_tasks/js_erb_locale_matcher.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'i18n/tasks/scanners/file_scanner' + +module I18nTasks + class JsErbLocaleMatcher < I18n::Tasks::Scanners::FileScanner + include I18n::Tasks::Scanners::RelativeKeys + include I18n::Tasks::Scanners::OccurrenceFromPosition + + # @return [Array<[absolute key, Results::Occurrence]>] + def scan_file(path) + text = read_file(path) + text.scan(/I18n.t\(['"]([\.\w]*)["'].*\)/).map do |match| + occurrence = occurrence_from_position( + path, text, Regexp.last_match.offset(0).first + ) + [match.first, occurrence] + end + end + end +end + +I18n::Tasks.add_scanner 'I18nTasks::JsErbLocaleMatcher', only: %w[*.js.erb] diff --git a/lib/i18n_tasks/slim_row_locale_matcher.rb b/lib/i18n_tasks/slim_row_locale_matcher.rb new file mode 100644 index 00000000..c30bf14e --- /dev/null +++ b/lib/i18n_tasks/slim_row_locale_matcher.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'i18n' +require 'i18n/tasks/scanners/file_scanner' + +module I18nTasks + class SlimRowLocaleMatcher < I18n::Tasks::Scanners::FileScanner + include I18n::Tasks::Scanners::RelativeKeys + include I18n::Tasks::Scanners::OccurrenceFromPosition + + AppI18n = I18n.dup + Dir[File.join(File.expand_path('config/locales'), '**/*.yml')].each do |locale_file| + AppI18n.config.load_path << locale_file + end + + # @return [Array<[absolute key, Results::Occurrence]>] + def scan_file(path) + text = read_file(path) + text.scan(/row\(.*label:\s*['"]([\.\w]*)["'].*\)/).map do |match| + occurrence = occurrence_from_position( + path, text, Regexp.last_match.offset(0).first + ) + # This lookup is based on `ApplicationHelper#label_column` + label = AppI18n.exists?("activerecord.attributes.#{match.first}") ? "activerecord.attributes.#{match.first}" : match.first + [absolute_key(label, path), occurrence] + end + end + end +end + +I18n::Tasks.add_scanner 'I18nTasks::SlimRowLocaleMatcher', only: %w[*.html.slim] diff --git a/spec/i18n_spec.rb b/spec/i18n_spec.rb new file mode 100644 index 00000000..8f72eac7 --- /dev/null +++ b/spec/i18n_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'i18n/tasks' + +RSpec.describe I18n do + let(:i18n) { I18n::Tasks::BaseTask.new } + let(:missing_keys) { i18n.missing_keys } + let(:unused_keys) { i18n.unused_keys } + let(:inconsistent_interpolations) { i18n.inconsistent_interpolations } + + it 'does not have missing keys' do + expect(missing_keys).to be_empty, + "Missing #{missing_keys.leaves.count} I18n keys, " \ + 'run `i18n-tasks missing` to show them. ' \ + 'If this is a false positive, you can add exceptions for translation keys ' \ + "in `config/i18n_tasks.yml` under 'ignore'. See https://github.com/glebm/i18n-tasks#configuration " \ + 'for more details.' + end + + it 'does not have unused keys' do + expect(unused_keys).to be_empty, + "#{unused_keys.leaves.count} unused I18n keys, run `i18n-tasks unused` to show them. " \ + 'If this is a false positive, you can add exceptions for translation keys ' \ + "in `config/i18n_tasks.yml` under 'ignore_unused'. See https://github.com/glebm/i18n-tasks#configuration " \ + 'for more details.' + end + + it 'files are normalized' do + non_normalized = i18n.non_normalized_paths + error_message = "The following files need to be normalized:\n" \ + "#{non_normalized.map {|path| " #{path}" }.join("\n")}\n" \ + 'Please run `i18n-tasks normalize` to fix these.' + expect(non_normalized).to be_empty, error_message + end + + it 'does not have inconsistent interpolations' do + error_message = "#{inconsistent_interpolations.leaves.count} i18n keys have inconsistent interpolations.\n" \ + 'Run `i18n-tasks check-consistent-interpolations` to show them.' + expect(inconsistent_interpolations).to be_empty, error_message + end +end