From 780e9d93ee24d90288dc3c93f060b8cf6276aa66 Mon Sep 17 00:00:00 2001 From: denisbalyko Date: Fri, 23 Jun 2017 14:34:17 +0300 Subject: [PATCH] support as a fallback language --- .../i-bem/__i18n/i-bem__i18n.i18n/core.js | 12 +++++++++++- .../i-bem/__i18n/test/test-i18n-base.js | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/blocks-common/i-bem/__i18n/i-bem__i18n.i18n/core.js b/blocks-common/i-bem/__i18n/i-bem__i18n.i18n/core.js index 456425d8..d17fc937 100644 --- a/blocks-common/i-bem/__i18n/i-bem__i18n.i18n/core.js +++ b/blocks-common/i-bem/__i18n/i-bem__i18n.i18n/core.js @@ -201,13 +201,23 @@ bem_.I18N = (function(base) { klass.key = function(name, params) { var proto = this._proto, result, - ksetRestored; + ksetRestored, + langRestored; proto.lang(this._lang).key(name); // TODO: kiss result = proto.val.call(proto, params, klass); + // DEFAULT_LANG if not found translation + // instead of an empty string + if (result === '') { + langRestored = proto._lang; + proto.lang(DEFAULT_LANG); + result = proto.val.call(proto, params, klass); + proto.lang(langRestored); + } + // restoring keyset's context // NOTE: should not save current ctx, `saveCtx = false` ksetRestored = _popStack(); diff --git a/blocks-common/i-bem/__i18n/test/test-i18n-base.js b/blocks-common/i-bem/__i18n/test/test-i18n-base.js index 486752e8..1569b6fe 100644 --- a/blocks-common/i-bem/__i18n/test/test-i18n-base.js +++ b/blocks-common/i-bem/__i18n/test/test-i18n-base.js @@ -69,6 +69,13 @@ suite('BEM.I18N Simple test', function() { }, { "lang" : "ru" }); + + BEM.I18N.decl('i-keyset-0', { + + "key1" : "Ключ1Кастом" + + }, { "lang" : "custom" }); + }); test('Simple', function() { @@ -87,4 +94,14 @@ suite('BEM.I18N Simple test', function() { assert.equal('Ключ1 : Ключ1', BEM.I18N('i-keyset-2', 'key', { })); }); + test('Simple language redeclaration', function() { + BEM.I18N.lang('custom'); + assert.equal('Ключ1Кастом', BEM.I18N('i-keyset-0', 'key1')); + }); + + test('Simple as a fallback language in the default', function() { + BEM.I18N.lang('custom'); + assert.equal('Ключ2', BEM.I18N('i-keyset-0', 'key2')); + }); + });