From 0f80393e6eef559db1149ff6c7cc1d3ca2509043 Mon Sep 17 00:00:00 2001 From: echarp Date: Tue, 30 Apr 2019 15:16:53 +0200 Subject: [PATCH] A new mechanism is added, i18n-tasks, to check on translations --- Gemfile | 1 + Gemfile.lock | 16 ++++- config/i18n-tasks.yml | 134 ++++++++++++++++++++++++++++++++++++++++++ test/i18n_test.rb | 36 ++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 config/i18n-tasks.yml create mode 100644 test/i18n_test.rb diff --git a/Gemfile b/Gemfile index a9d776ac..475ac8a5 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,7 @@ gem 'http_accept_language' gem 'rails-i18n' # Store some specific content, like application name and static pages contents gem 'i18n-active_record', require: 'i18n/active_record' +gem 'i18n-tasks' # Remove leading and trailing white spaces gem 'strip_attributes' # Track changes to models' data diff --git a/Gemfile.lock b/Gemfile.lock index 9eb6c187..e6f90f51 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -181,6 +181,7 @@ GEM actionpack (>= 4.1) activesupport (>= 4.1) high_voltage (3.1.0) + highline (2.0.2) html2haml (2.2.0) erubis (~> 2.7.0) haml (>= 4.0, < 6) @@ -203,6 +204,16 @@ GEM i18n-country-translations (1.3.1) i18n (>= 0.9.3, < 2) railties (>= 5.0, < 5.3) + i18n-tasks (0.9.29) + activesupport (>= 4.0.2) + ast (>= 2.1.0) + erubi + highline (>= 2.0.0) + i18n + parser (>= 2.2.3.0) + rails-i18n + rainbow (>= 2.2.2, < 4.0) + terminal-table (>= 1.5.1) icalendar (2.5.3) ice_cube (~> 0.16) inherited_resources (1.10.0) @@ -396,13 +407,15 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.4.0) + sqlite3 (1.4.1) strip_attributes (1.9.0) activemodel (>= 3.0, < 7.0) sysexits (1.2.0) tagsinput-rails (1.3.5.1) railties (>= 3.1) temple (0.8.1) + terminal-table (1.8.0) + unicode-display_width (~> 1.1, >= 1.1.1) therubyracer (0.12.3) libv8 (~> 3.16.14.15) ref @@ -476,6 +489,7 @@ DEPENDENCIES http_accept_language i18n-active_record i18n-country-translations + i18n-tasks icalendar ice_cube! jbuilder (~> 2.5) diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml new file mode 100644 index 00000000..6726c92b --- /dev/null +++ b/config/i18n-tasks.yml @@ -0,0 +1,134 @@ +# 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 `File.find` patterns where translations are read from: + read: + ## Default: + - config/locales/%{locale}.yml + ## More files: + - 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: + ## For example, write devise and simple form keys to their respective files: + - ['{devise, gflash, ice_cube, simple_form}.*', 'config/locales/\1.%{locale}.yml'] + ## Catch-all default: + - config/locales/%{locale}.yml + + # External locale data (e.g. gems). + # This data is not considered unused and is never written to. + external: + ## Example (replace %#= with %=): + # - "<%#= %x[bundle show vagrant].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 `File.find` patterns to search in: + # paths: + # - app/ + + ## Root directories for relative keys resolution. + # relative_roots: + # - app/controllers + # - app/helpers + # - app/mailers + # - app/presenters + # - app/views + + ## Files or `File.fnmatch` patterns to exclude from search. Some files are always excluded regardless of this setting: + ## %w(*.jpg *.png *.gif *.svg *.ico *.eot *.otf *.ttf *.woff *.woff2 *.pdf *.css *.sass *.scss *.less *.yml *.json) + exclude: + - app/assets/images + - app/assets/fonts + - app/assets/videos + + ## 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 + + ## 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" + +## Do not consider these keys missing: +# ignore_missing: +# - 'errors.messages.{accepted,blank,invalid,too_short,too_long}' +# - '{devise,simple_form}.*' + +## Consider these keys used: +ignore_unused: +- 'activerecord.*' +- '{devise,kaminari,gflash,ice_cube}.*' +# - 'simple_form.{yes,no}' +# - 'simple_form.{placeholders,hints,labels}.*' +# - 'simple_form.{error_notification,required}.:' + +## 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: +# - 'activerecord.attributes.*' + +## Ignore these keys completely: +ignore: +- kaminari.* + +## 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/test/i18n_test.rb b/test/i18n_test.rb new file mode 100644 index 00000000..a521dabf --- /dev/null +++ b/test/i18n_test.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'i18n/tasks' + +class I18nTest < ActiveSupport::TestCase + def setup + @i18n = I18n::Tasks::BaseTask.new + @missing_keys = @i18n.missing_keys + @unused_keys = @i18n.unused_keys + end + + def test_no_missing_keys + assert_empty @missing_keys, + "Missing #{@missing_keys.leaves.count} i18n keys, run `i18n-tasks missing' to show them" + end + + def test_no_unused_keys + assert_empty @unused_keys, + "#{@unused_keys.leaves.count} unused i18n keys, run `i18n-tasks unused' to show them" + end + + def test_files_are_normalized + 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" + assert_empty non_normalized, error_message + end + + def test_no_inconsistent_interpolations + inconsistent_interpolations = @i18n.inconsistent_interpolations + error_message = "#{inconsistent_interpolations.leaves.count} i18n keys have inconsistent interpolations.\n" \ + "Please run `i18n-tasks check-consistent-interpolations' to show them" + assert_empty inconsistent_interpolations, error_message + end +end