Skip to content

Commit

Permalink
Merge pull request #867 from Floppy/i18n-tasks
Browse files Browse the repository at this point in the history
Add tasks and checks for translations
  • Loading branch information
Floppy committed Dec 12, 2022
2 parents b60c2d5 + 3b8ab9d commit 67d6974
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 45 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require:
- standard
- rubocop-rails
- rubocop-rspec
- rubocop-i18n

inherit_gem:
standard: config/base.yml
Expand All @@ -17,3 +18,10 @@ AllCops:
- 'tmp/**/*'
- 'vendor/**/*'
- '.git/**/*'

I18n/GetText:
Enabled: false
I18n/RailsI18n:
Enabled: true
Exclude:
- 'spec/**/*'
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ group :development, :test do
gem "database_cleaner-active_record", "~> 2.0"
gem "rubocop-rails", require: false
gem "rubocop-rspec", require: false
gem "rubocop-i18n", require: false
gem "i18n-tasks", "~> 1.0"
end

group :development do
Expand Down
29 changes: 29 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ GEM
ruby2_keywords (>= 0.0.2, < 1.0)
ast (2.4.2)
bcrypt (3.1.18)
better_html (2.0.1)
actionview (>= 6.0)
activesupport (>= 6.0)
ast (~> 2.0)
erubi (~> 1.4)
parser (>= 2.4)
smart_properties
bindex (0.8.1)
bootsnap (1.15.0)
msgpack (~> 1.2)
Expand Down Expand Up @@ -149,8 +156,20 @@ GEM
has_scope (0.8.0)
actionpack (>= 5.2)
activesupport (>= 5.2)
highline (2.0.3)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
i18n-tasks (1.0.12)
activesupport (>= 4.0.2)
ast (>= 2.1.0)
better_html (>= 1.0, < 3.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)
inherited_resources (1.13.1)
actionpack (>= 5.2, < 7.1)
has_scope (~> 0.6)
Expand Down Expand Up @@ -262,6 +281,9 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.3)
loofah (~> 2.3)
rails-i18n (7.0.6)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 8)
rails-settings-cached (2.8.2)
rails (>= 5.0.0)
railties (7.0.4)
Expand Down Expand Up @@ -324,6 +346,8 @@ GEM
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.24.0)
parser (>= 3.1.1.0)
rubocop-i18n (3.0.0)
rubocop (~> 1.0)
rubocop-performance (1.15.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
Expand All @@ -346,6 +370,7 @@ GEM
sprockets-rails
tilt
shellany (0.0.1)
smart_properties (1.17.0)
spring (4.1.0)
sprockets (4.0.3)
concurrent-ruby (~> 1.0)
Expand All @@ -364,6 +389,8 @@ GEM
rubocop-performance (= 1.15.1)
stopwords-filter (0.7.0)
strscan (3.0.4)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.2.1)
tilt (2.0.10)
timeout (0.3.0)
Expand Down Expand Up @@ -404,6 +431,7 @@ DEPENDENCIES
faker (~> 3.0)
guard (~> 2.18)
guard-rspec (~> 4.7)
i18n-tasks (~> 1.0)
jbuilder (~> 2.11)
jsbundling-rails
kaminari (~> 1.2)
Expand All @@ -418,6 +446,7 @@ DEPENDENCIES
rails-settings-cached (~> 2.8)
redis (~> 5.0)
rspec-rails
rubocop-i18n
rubocop-rails
rubocop-rspec
sass-rails (>= 6)
Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_links_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= card :secondary, "Links" do %>
<ul class='links'>
<% links.each do |link| %>
<li><%= link_to t(link.site), link.url %></li>
<li><%= link_to t("sites.#{link.site}"), link.url %></li>
<% end %>
</ul>
<% end %>
Expand Down
153 changes: 153 additions & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# 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

# 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, 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 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 `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

## 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

## 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"

## 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.attributes.*'
# - '{devise,kaminari,will_paginate}.*'
# - '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}']] %>
61 changes: 17 additions & 44 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at https://guides.rubyonrails.org/i18n.html.

---
en:
# Site names
thingiverse: Thingiverse
myminifactory: MyMiniFactory
cults3d: Cults3D
cgtrader: CGTrader
theminiindex: The Mini Index
comicsgamesandthings: Comics, Games, and Things
# ActiveRecord things
activerecord:
models:
attributes:
link:
url: "URL"
active_admin:
dashboard: Dashboard
sites:
cgtrader: CGTrader
comicsgamesandthings: Comics, Games, and Things
cults3d: Cults3D
myminifactory: MyMiniFactory
theminiindex: The Mini Index
thingiverse: Thingiverse
views:
pagination:
first: "« First"
last: Last »
next: Next ›
previous: "‹ Prev"
truncate: ""
34 changes: 34 additions & 0 deletions spec/i18n_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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"
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"
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"
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

0 comments on commit 67d6974

Please sign in to comment.