Skip to content

Commit

Permalink
New translations update tasks (#91)
Browse files Browse the repository at this point in the history
* Fix and refactor solidus_i18n:update_default task

- The file download was not working
- Download the file under /config/locales

* Add i18n-tasks development dependency

This gem can replace our custom, old and untested tasks for Yaml files
management.
This is not compatible with older solidus versions but that's not a problem. We
only need this when using the master branch.

* Use i18n-tasks for YAML files update

Remove the old code of the solidus_i18n:sync task and document the use of
i18n-tasks.
  • Loading branch information
alepore authored and tvdeyen committed Jun 2, 2017
1 parent 6898def commit 0ca92d8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.localeapp/locales
.project
coverage
default
config/locales/en.yml
Gemfile.lock
tmp
nbproject
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'mysql2'

group :development, :test do
gem "pry-rails"
gem 'i18n-tasks', '~> 0.9' if branch == 'master'
end

gemspec
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ you want your users to be able to select from the locale toggle on the frontend.

---

## Updating Translations

If you want to improve the translations on your language, run the tasks:

bundle exec rake solidus_i18n:update_default
bundle exec i18n-tasks add-missing --nil-value --locale <LOCALE>

Substitute <LOCALE> with your locale code (e.g: `it`).

This will do a cleanup and prepare `<LOCALE>.yml` with all the missing keys.
You can then write the translations and open a pull request.

---

## Model Translations

We **removed** support for translating models into [a separate Gem](https://github.com/solidusio-contrib/solidus_globalize).
Expand Down
51 changes: 4 additions & 47 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,16 @@ require 'solidus_i18n'
namespace :solidus_i18n do
desc 'Update by retrieving the latest Solidus locale files'
task :update_default do
puts "Fetching latest Solidus locale file to #{locales_dir}"
require 'uri'
require 'net/https'

require 'open-uri'
puts "Fetching latest Solidus locale file"
location = 'https://raw.github.com/solidusio/solidus/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}/solidus_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) = SolidusI18n::Utils.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] }
SolidusI18n::Utils.write_file(filename, basename, comments, other, false)
open("#{locales_dir}/en.yml", 'wb') do |file|
file << open(location).read
end
end

def translation_keys
(dummy_comments, words) = SolidusI18n::Utils.read_file(File.dirname(__FILE__) + '/default/solidus_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
1 change: 0 additions & 1 deletion lib/solidus_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
require 'solidus_i18n/engine'
require 'solidus_i18n/locale'
require 'solidus_i18n/version'
require 'solidus_i18n/utils'
require 'coffee_script'
require 'deface'
56 changes: 0 additions & 56 deletions lib/solidus_i18n/utils.rb

This file was deleted.

0 comments on commit 0ca92d8

Please sign in to comment.