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
Uk/1562 add i18n js gem #1564
Uk/1562 add i18n js gem #1564
Changes from all commits
3adb13e
d2ba465
2637574
f396d30
9429695
5166a3d
c6f6f5b
ec6a365
e4627fe
199bfe5
686a8f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
# Old aliases before i18n-js was introduced. | ||
# TODO - delete it after everything is moved to i18n-js | ||
|
||
# Declares the translation function t. | ||
# You can use translate('login') or t('login') in Javascript. | ||
window.translate = (key, options = {}) -> | ||
unless 'I18n' of window | ||
console.log 'The I18n object is undefined. Cannot translate text.' | ||
return key | ||
dict = I18n | ||
parts = key.split '.' | ||
while (parts.length) | ||
part = parts.shift() | ||
return key unless part of dict | ||
dict = dict[part] | ||
text = dict | ||
for name, value of options | ||
text = text.split("%{#{name}}").join(value) | ||
text | ||
I18n.t(key, options) | ||
window.t = window.translate |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module I18nHelper | ||
private | ||
|
||
def set_locale | ||
I18n.locale = params[:locale] || I18n.default_locale | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/ insert_before "div#wrapper" | ||
|
||
= render "layouts/i18n_script" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
%script | ||
I18n.default_locale = "#{I18n.default_locale}"; | ||
I18n.locale = "#{I18n.locale}"; | ||
I18n.fallbacks = "default_locale"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This is copied from https://github.com/fnando/i18n-js/blob/master/lib/i18n/js.rb | ||
# As in spree core en.yml there are translations - | ||
# en: | ||
# no: "No" | ||
# yes: "Yes" | ||
# Which become to true and false and those have no #to_sym method | ||
# TODO - remove this after spree core locales are fixed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great. This way it will be super easy to see what needs to be changed after the Spree upgrade. |
||
|
||
module I18n | ||
module JS | ||
# Filter translations according to the specified scope. | ||
def self.filter(translations, scopes) | ||
scopes = scopes.split(".") if scopes.is_a?(String) | ||
scopes = scopes.clone | ||
scope = scopes.shift | ||
if scope == "*" | ||
results = {} | ||
translations.each do |scope, translations| | ||
tmp = scopes.empty? ? translations : filter(translations, scopes) | ||
scope_symbol = scope.respond_to?(:to_sym) ? scope.to_sym : scope.to_s.to_sym | ||
results[scope_symbol] = tmp unless tmp.nil? | ||
end | ||
return results | ||
elsif translations.respond_to?(:key?) && translations.key?(scope.to_sym) | ||
return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)} | ||
end | ||
nil | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
|
||
feature 'Multilingual', js: true do | ||
include AuthenticationWorkflow | ||
include WebHelper | ||
|
||
background do | ||
login_to_admin_section | ||
end | ||
|
||
it 'has two locales available' do | ||
expect(Rails.application.config.i18n[:default_locale]).to eq 'en' | ||
expect(Rails.application.config.i18n[:locale]).to eq 'en' | ||
expect(Rails.application.config.i18n[:available_locales]).to eq ['en', 'es'] | ||
end | ||
|
||
it 'can switch language by params' do | ||
expect(get_i18n_locale).to eq 'en' | ||
expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'My Enterprises' | ||
expect(page).to have_content 'My Enterprises' | ||
|
||
visit spree.admin_path(locale: 'es') | ||
expect(get_i18n_locale).to eq 'es' | ||
expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'Mis Organizaciones' | ||
expect(page).to have_content 'Mis Organizaciones' | ||
end | ||
|
||
it 'fallbacks to default_locale' do | ||
pending 'current spree core has a bug if not available locale is provided' | ||
# undefined method `delete_if' for "translation missing: it.date.month_names":String | ||
# inside core/app/views/spree/admin/shared/_translations.html.erb | ||
|
||
# I18n-js fallsback to 'en' | ||
visit spree.admin_path(locale: 'it') | ||
expect(get_i18n_locale).to eq 'it' | ||
expect(get_i18n_translation('spree_admin_overview_enterprises_header')).to eq 'My Enterprises' | ||
# This still is italian until we change enforce_available_locales to `true` | ||
expect(page).to have_content 'Le Mie Aziende' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'spec_helper' | ||
|
||
feature 'Multilingual', js: true do | ||
include WebHelper | ||
|
||
it 'has two locales available' do | ||
expect(Rails.application.config.i18n[:default_locale]).to eq 'en' | ||
expect(Rails.application.config.i18n[:locale]).to eq 'en' | ||
expect(Rails.application.config.i18n[:available_locales]).to eq ['en', 'es'] | ||
end | ||
|
||
it 'can switch language by params' do | ||
visit root_path | ||
expect(get_i18n_locale).to eq 'en' | ||
expect(get_i18n_translation('label_shops')).to eq 'Shops' | ||
expect(page).to have_content 'Interested in getting on the Open Food Network?' | ||
expect(page).to have_content 'SHOPS' | ||
|
||
visit root_path(locale: 'es') | ||
expect(get_i18n_locale).to eq 'es' | ||
expect(get_i18n_translation('label_shops')).to eq 'Tiendas' | ||
expect(page).to have_content '¿Estás interesada en entrar en Open Food Network?' | ||
expect(page).to have_content 'TIENDAS' | ||
|
||
# I18n-js fallsback to 'en' | ||
visit root_path(locale: 'it') | ||
expect(get_i18n_locale).to eq 'it' | ||
expect(get_i18n_translation('label_shops')).to eq 'Shops' | ||
# This still is italian until we change enforce_available_locales to `true` | ||
expect(page).to have_content 'NEGOZI' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
//= require textAngular-sanitize.min.js | ||
//= require textAngular.min.js | ||
//= require moment | ||
//= require i18n | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this change related to Karma? If not, I'd suggest moving this change to a separate commit. But see above for comments about treating this as a spike. |
||
|
||
angular.module('templates', []) | ||
angular.module('google-maps', []); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a gem can be in a single commit.