Add JsLocaleHelper tests for i18n fallback
This commit is contained in:
parent
ff219bc65c
commit
cb395662d0
|
@ -1,7 +1,7 @@
|
|||
module JsLocaleHelper
|
||||
|
||||
def self.load_translations(locale)
|
||||
@loaded_translations ||= {}
|
||||
@loaded_translations ||= HashWithIndifferentAccess.new
|
||||
@loaded_translations[locale] ||= begin
|
||||
locale_str = locale.to_s
|
||||
|
||||
|
@ -69,19 +69,21 @@ module JsLocaleHelper
|
|||
end
|
||||
end
|
||||
|
||||
def self.output_locale(locale, request=nil)
|
||||
current_locale = I18n.locale
|
||||
I18n.locale = locale.to_sym
|
||||
|
||||
def self.output_locale(locale)
|
||||
locale_sym = locale.to_sym
|
||||
locale_str = locale.to_s
|
||||
|
||||
current_locale = I18n.locale
|
||||
I18n.locale = locale_sym
|
||||
|
||||
site_locale = SiteSetting.default_locale.to_sym
|
||||
|
||||
if locale == :en
|
||||
translations = load_translations(locale)
|
||||
elsif locale == site_locale || site_locale == :en
|
||||
translations = load_translations_merged(locale, :en)
|
||||
if locale_sym == :en
|
||||
translations = load_translations(locale_sym)
|
||||
elsif locale_sym == site_locale || site_locale == :en
|
||||
translations = load_translations_merged(locale_sym, :en)
|
||||
else
|
||||
translations = load_translations_merged(locale, site_locale, :en)
|
||||
translations = load_translations_merged(locale_sym, site_locale, :en)
|
||||
end
|
||||
|
||||
message_formats = strip_out_message_formats!(translations[locale_str]['js'])
|
||||
|
|
|
@ -2,6 +2,24 @@ require 'spec_helper'
|
|||
require_dependency 'js_locale_helper'
|
||||
|
||||
describe JsLocaleHelper do
|
||||
|
||||
module StubLoadTranslations
|
||||
def set_translations(locale, translations)
|
||||
@loaded_translations ||= HashWithIndifferentAccess.new
|
||||
@loaded_translations[locale] = translations
|
||||
end
|
||||
|
||||
def clear_cache!
|
||||
@loaded_translations = nil
|
||||
@loaded_merges = nil
|
||||
end
|
||||
end
|
||||
JsLocaleHelper.extend StubLoadTranslations
|
||||
|
||||
after do
|
||||
JsLocaleHelper.clear_cache!
|
||||
end
|
||||
|
||||
it 'should be able to generate translations' do
|
||||
expect(JsLocaleHelper.output_locale('en').length).to be > 0
|
||||
end
|
||||
|
@ -57,21 +75,23 @@ describe JsLocaleHelper do
|
|||
it 'handles message format special keys' do
|
||||
ctx = V8::Context.new
|
||||
ctx.eval("I18n = {};")
|
||||
ctx.eval(JsLocaleHelper.output_locale('en',
|
||||
{
|
||||
"en" =>
|
||||
{
|
||||
"js" => {
|
||||
"hello" => "world",
|
||||
"test_MF" => "{HELLO} {COUNT, plural, one {1 duck} other {# ducks}}",
|
||||
"error_MF" => "{{BLA}",
|
||||
"simple_MF" => "{COUNT, plural, one {1} other {#}}"
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
expect(ctx.eval('I18n.translations')["en"]["js"]["hello"]).to eq("world")
|
||||
expect(ctx.eval('I18n.translations')["en"]["js"]["test_MF"]).to eq(nil)
|
||||
JsLocaleHelper.set_translations 'en', {
|
||||
"en" =>
|
||||
{
|
||||
"js" => {
|
||||
"hello" => "world",
|
||||
"test_MF" => "{HELLO} {COUNT, plural, one {1 duck} other {# ducks}}",
|
||||
"error_MF" => "{{BLA}",
|
||||
"simple_MF" => "{COUNT, plural, one {1} other {#}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.eval(JsLocaleHelper.output_locale('en'))
|
||||
|
||||
expect(ctx.eval('I18n.translations["en"]["js"]["hello"]')).to eq("world")
|
||||
expect(ctx.eval('I18n.translations["en"]["js"]["test_MF"]')).to eq(nil)
|
||||
|
||||
expect(ctx.eval('I18n.messageFormat("test_MF", { HELLO: "hi", COUNT: 3 })')).to eq("hi 3 ducks")
|
||||
expect(ctx.eval('I18n.messageFormat("error_MF", { HELLO: "hi", COUNT: 3 })')).to match(/Invalid Format/)
|
||||
|
@ -85,7 +105,64 @@ describe JsLocaleHelper do
|
|||
end
|
||||
|
||||
it 'performs fallbacks to english if a translation is not available' do
|
||||
skip('todo: write test')
|
||||
JsLocaleHelper.set_translations 'en', {
|
||||
"en" => {
|
||||
"js" => {
|
||||
"only_english" => "1-en",
|
||||
"english_and_site" => "3-en",
|
||||
"english_and_user" => "5-en",
|
||||
"all_three" => "7-en",
|
||||
}
|
||||
}
|
||||
}
|
||||
JsLocaleHelper.set_translations 'ru', {
|
||||
"ru" => {
|
||||
"js" => {
|
||||
"only_site" => "2-ru",
|
||||
"english_and_site" => "3-ru",
|
||||
"site_and_user" => "6-ru",
|
||||
"all_three" => "7-ru",
|
||||
}
|
||||
}
|
||||
}
|
||||
JsLocaleHelper.set_translations 'uk', {
|
||||
"uk" => {
|
||||
"js" => {
|
||||
"only_user" => "4-uk",
|
||||
"english_and_user" => "5-uk",
|
||||
"site_and_user" => "6-uk",
|
||||
"all_three" => "7-uk",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expected = {
|
||||
"none" => "[uk.js.none]",
|
||||
"only_english" => "1-en",
|
||||
"only_site" => "2-ru",
|
||||
"english_and_site" => "3-ru",
|
||||
"only_user" => "4-uk",
|
||||
"english_and_user" => "5-uk",
|
||||
"site_and_user" => "6-uk",
|
||||
"all_three" => "7-uk",
|
||||
}
|
||||
|
||||
SiteSetting.default_locale = 'ru'
|
||||
I18n.locale = :uk
|
||||
|
||||
ctx = V8::Context.new
|
||||
ctx.eval('var window = this;')
|
||||
ctx.load(Rails.root + 'app/assets/javascripts/locales/i18n.js')
|
||||
ctx.eval(JsLocaleHelper.output_locale(I18n.locale))
|
||||
ctx.eval('I18n.defaultLocale = "ru";')
|
||||
|
||||
# Test - unneeded translations are not emitted
|
||||
expect(ctx.eval('I18n.translations.en.js').keys).to eq(["only_english"])
|
||||
expect(ctx.eval('I18n.translations.ru.js').keys).to eq(["only_site", "english_and_site"])
|
||||
|
||||
expected.each do |key, expect|
|
||||
expect(ctx.eval("I18n.t(#{"js.#{key}".inspect})")).to eq(expect)
|
||||
end
|
||||
end
|
||||
|
||||
LocaleSiteSetting.values.each do |locale|
|
||||
|
|
Loading…
Reference in New Issue