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

Basic i18n-tasks support with custom parser for Spree.t #640

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ spec/dummy
public/spree
.ruby-version
.ruby-gemset
config/locales/en.yml
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ Remove all occurrences of `SpreeI18n::Config.supported_locales` from your code.

---

## Managing Translations

We use i18n-tasks to keep locales normalized with spree upstream.

### Updating Keys with Spree Master

** Beta ** this currently requires a version of i18n-tasks that conflicts with
spree_cmd, over highline versions, i18n-tasks 0.9.0-rc2 or higher must be
installed for the custom Spree.t parser to work. Otherwise i18n-tasks will
mangle translations.

``` bash
be rake spree_i18n:update_en # Pulls en.yml from spree/spree master
i18n-tasks add-missing-nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n-tasks add-missing --nil-value.

```

### Normalizing Locales
``` bash
i18n-tasks normalize
```

See i18n-tasks for the rest of the magic


## Contributing

[See corresponding guidelines][7]
Expand Down
59 changes: 6 additions & 53 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,14 @@ task :test_app do
end

namespace :spree_i18n do
desc 'Update by retrieving the latest Spree locale files'
task :update_default do
puts "Fetching latest Spree locale file to #{locales_dir}"
require 'uri'
require 'net/https'

desc 'Update by retrieving the latest Spree locale files for i18n-tasks'
task :update_en do
puts "Fetching latest English Spree locale"
require 'open-uri'
location = 'https://raw.github.com/spree/spree/master/core/config/locales/en.yml'
begin
uri = URI.parse(location)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
puts "Getting from #{uri}"
request = Net::HTTP::Get.new(uri.request_uri)
case response = http.request(request)
when Net::HTTPRedirection then location = response['location']
when Net::HTTPClientError, Net::HTTPServerError then response.error!
end
end until Net::HTTPSuccess == response

FileUtils.mkdir_p(default_dir) unless File.directory?(default_dir)

File.open("#{default_dir}/spree_core.yml", 'w') { |file| file << response.body }
end

desc 'Syncronize translation files with latest en (adds comments with fallback en value)'
task :sync do
puts 'Starting syncronization...'
words = translation_keys

Dir["#{locales_dir}/*.yml"].each do |filename|
basename = File.basename(filename, '.yml')
(comments, other) = Spree::I18nUtils.read_file(filename, basename)
# Initializing hash variable as en fallback if it does not exist
words.each { |k, _v| other[k] ||= "#{words[k]}" }
# Remove if not defined in en locale
other.delete_if { |k, _v| !words[k] }
Spree::I18nUtils.write_file(filename, basename, comments, other, false)
open("config/locales/en.yml", 'wb') do |file|
file << open(location).read
end
end

def translation_keys
(dummy_comments, words) = Spree::I18nUtils.read_file(File.dirname(__FILE__) + '/default/spree_core.yml', 'en')
words
end

def locales_dir
File.join File.dirname(__FILE__), 'config/locales'
end

def default_dir
File.join File.dirname(__FILE__), 'default'
end

def env_locale
ENV['LOCALE'].presence
end
end
151 changes: 151 additions & 0 deletions config/i18n-tasks.yml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# 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
## Another gem (replace %#= with %=):
# - "<%#= %x[bundle show vagrant].chomp %>/templates/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

## Specify the router (see Readme for details). Valid values: conservative_router, pattern_router, or a custom class.
# router: convervative_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:
# This currently cannot see into spree.t calls will be easier to support
# in 0.9
<% spree_path = %x[bundle show spree].chomp %>
- "<%=spree_path%>/core/app"
- "<%=spree_path%>/frontend/app"
- "<%=spree_path%>/backend/app"
- "<%=spree_path%>/cmd/app"
- "<%=spree_path%>/api/app"
- "<%=spree_path%>/core/lib"
- "<%=spree_path%>/frontend/lib"
- "<%=spree_path%>/backend/lib"
- "<%=spree_path%>/cmd/lib"
- "<%=spree_path%>/api/lib"

## Root directories for relative keys resolution.
relative_roots:
- app

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

## Alternatively, the only files or `File.fnmatch patterns` to search in `paths`:
## If specified, this settings takes priority over `exclude`, but `exclude` still applies.
# include: ["*.rb", "*.html.slim"]

## Default scanner finds t() and I18n.t() calls.
# scanner: I18nTasks::Scanners::SpreeTScanner

## Google Translate
# translation:
# # Get an API key and set billing info at https://code.google.com/apis/console to use Google Translate
# api_key: "AbC-dEf5"

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

## Ignore these keys completely:
# ignore:
# - kaminari.*

# Hackity Hack
# See https://github.com/glebm/i18n-tasks/issues/170
<% module AddMissingNil
include ::I18n::Tasks::Command::Collection
cmd :add_missing_nil,
desc: 'Add missing keys with nil values',
pos: '[locale ...]',
args: [:locales]
def add_missing_nil(opt = {})
add_missing opt.merge(value: nil)
end
end
I18n::Tasks::Commands.send :include, AddMissingNil

%>

# Add in spree custom scanner in 0.9
<%
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this inlined here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As i18n_tasks runs outside the scope of the parent application. We could add a require but it would still need to be inlined.

Basically that's what the i18n-tasks creator recommended. See glebm/i18n-tasks#170

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n-tasks add-missing supports a --nil-value argument since v0.9.5, so this is no longer necessary.

require './lib/i18n/spree_t_scanner.rb'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.rb extension not necessary

I18n::Tasks::Configuration::DEFAULTS[:search][:scanners] = [
[
'I18n::Scanners::SpreeTScanner',
{ ignore_lines: { 'opal' => '^\\s*#(?!\\si18n-tasks-use)',
'haml' => '^\\s*-\\s*#(?!\\si18n-tasks-use)',
'slim' => '^\\s*(?:-#|/)(?!\\si18n-tasks-use)',
'coffee' => '^\\s*#(?!\\si18n-tasks-use)',
'erb' => '^\\s*<%\\s*#(?!\\si18n-tasks-use)' }
}
]

]
# Scope this down, so we don't hit Spree.t twice
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not neccesary since v0.9.1. So you can have both by removing this and adding this to spree_t_scanner.rb instead:

I18n::Tasks.add_scanner 'I18n::Scanners::SpreeTScanner',
                         ignore_lines: {'opal'   => %q(^\s*#(?!\si18n-tasks-use)),
                                        'haml'   => %q(^\s*-\s*#(?!\si18n-tasks-use)),
                                        'slim'   => %q(^\s*(?:-#|/)(?!\si18n-tasks-use)),
                                        'coffee' => %q(^\s*#(?!\si18n-tasks-use)),
                                        'erb'    => %q(^\s*<%\s*#(?!\si18n-tasks-use))}

# if someone is better at regex, it's possible we can have both of these.
# I18n::Tasks::Scanners::PatternScanner.class_eval do
# def translate_call_re
# /(?<=^|[^\w'\-])(?!Spree)t(?:ranslate)?/
# end
# end
%>
5 changes: 3 additions & 2 deletions config/locales/bg.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
bg:
activerecord:
attributes:
Expand Down Expand Up @@ -377,8 +378,8 @@ bg:
orders: "Поръчки"
overview: "Прегледай"
products: "Продукти"
promotions: "Промоции"
promotion_categories:
promotions: "Промоции"
properties:
prototypes:
reports: "Доклади"
Expand Down Expand Up @@ -424,7 +425,7 @@ bg:
are_you_sure: "Сигурни ли сте"
are_you_sure_delete: "Сигурни ли сте, че искате да изтриете този запис?"
associated_adjustment_closed:
at_symbol: '@'
at_symbol: "@"
authorization_failure:
authorized:
auto_capture:
Expand Down
5 changes: 3 additions & 2 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
ca:
activerecord:
attributes:
Expand Down Expand Up @@ -381,8 +382,8 @@ ca:
orders:
overview:
products:
promotions:
promotion_categories:
promotions:
properties:
prototypes:
reports:
Expand Down Expand Up @@ -428,7 +429,7 @@ ca:
are_you_sure: Està segur?
are_you_sure_delete: Està segur que vol eliminar aquesta entrada?
associated_adjustment_closed:
at_symbol: '@'
at_symbol: "@"
authorization_failure: Fallada d'autorització
authorized:
auto_capture:
Expand Down
Loading