Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Allow overriding the locale for individual translations
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalcora authored and Grantovich committed Apr 25, 2016
1 parent e0ee8e8 commit 9e84028
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
33 changes: 30 additions & 3 deletions addon/services/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ export default Parent.extend(Evented, {
// A list of found locales.
locales: computed(getLocales),

// @public
// Whether to allow overriding the locale for individual translations.
allowLocaleOverride: undefined,

// @public
//
// Returns the translation `key` interpolated with `data`
// in the current `locale`.
t: function(key, data = {}) {
const locale = this.get('_locale');
const locale = this._getLocaleWithOverride(data);

assert("I18n: Cannot translate when locale is null", locale);
const count = get(data, 'count');

Expand All @@ -41,7 +46,7 @@ export default Parent.extend(Evented, {

// @public
exists: function(key, data = {}) {
const locale = this.get('_locale');
const locale = this._getLocaleWithOverride(data);
assert("I18n: Cannot check existance when locale is null", locale);
const count = get(data, 'count');

Expand Down Expand Up @@ -71,6 +76,10 @@ export default Parent.extend(Evented, {
}
this.set('locale', defaultLocale);
}

if (this.get('allowLocaleOverride') == null) {
this.set('allowLocaleOverride', (ENV.i18n || {}).allowLocaleOverride);
}
}),

// @private
Expand All @@ -83,6 +92,24 @@ export default Parent.extend(Evented, {
_locale: computed('locale', function() {
const locale = this.get('locale');
return locale ? new Locale(this.get('locale'), getOwner(this)) : null;
})
}),

// @private
// Retrieves the current locale, allowing it to be overridden by a `locale`
// option in the translation data if `allowLocaleOverride` is enabled.
_getLocaleWithOverride: function(data) {
let locale = this.get('_locale');

if (data.locale && this.get('allowLocaleOverride') !== false) {
warn(
'Specifying an interpolation key named `locale` now overrides the target locale for translation. To silence this warning, set ENV.i18n.allowLocaleOverride to true. To disable this behavior and treat `locale` as a normal interpolation key, set ENV.i18n.allowLocaleOverride to false.',
(this.get('allowLocaleOverride') !== undefined),
{ id: 'ember-i18n.locale-override' }
);
locale = new Locale(data.locale, getOwner(this));
delete data.locale;
}

return locale;
}
});
4 changes: 4 additions & 0 deletions tests/acceptance/t-helper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ test('updates when the locale changes', function(assert) {
assert.textIs('.no-interpolations', 'texto sin interpolaciones');
});
});

test('can override the target locale', function(assert) {
assert.textIs('.locale-override', 'texto sin interpolaciones');
});
3 changes: 2 additions & 1 deletion tests/dummy/app/locales/en/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export default {
'no.interpolations.either': 'another text without interpolations',

'with': {
interpolations: 'Clicks: {{clicks}}'
interpolations: 'Clicks: {{clicks}}',
'locale.interpolation': 'Locale: {{locale}}'
},

'pluralized.translation': {
Expand Down
4 changes: 4 additions & 0 deletions tests/dummy/app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
<section>
<button class='switch-to-es' {{action "switchLocale" "es"}}>Switch to Spanish</button>
</section>

<section>
<span class='locale-override'>{{t "no.interpolations" locale="es"}}</span>
</section>
6 changes: 6 additions & 0 deletions tests/unit/i18n-exists-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ test('fallsback to the parent locale', function(assert) {

assert.equal(i18n.exists('no.interpolations.either'), true);
});

test('allows overriding the locale', function(assert) {
const i18n = this.subject({ locale: 'en' });

assert.equal(i18n.exists('no.interpolations.either', { locale: 'es' }), false);
});
42 changes: 41 additions & 1 deletion tests/unit/i18n-t-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ import { moduleFor, test } from 'ember-qunit';

const { run } = Ember;

let originalWarn = null;
const warnings = Ember.A();

moduleFor('service:i18n', 'I18nService#t', {
integration: true
integration: true,

beforeEach: function() {
originalWarn = Ember.Logger.warn;
Ember.Logger.warn = function(message, ...args) {
warnings.pushObject(message);
originalWarn(message, ...args);
};
},

afterEach: function() {
warnings.clear();
Ember.Logger.warn = originalWarn;
}
});

test('falls back to parent locale', function(assert) {
Expand Down Expand Up @@ -91,3 +107,27 @@ test("check unknown locale", function(assert) {
const result = this.subject({ locale: 'uy' }).t('not.yet.translated', {count: 2});
assert.equal('Missing translation: not.yet.translated', result);
});

test("locale can be overridden and warns by default", function(assert) {
const i18n = this.subject({ locale: 'en' });

const result = i18n.t('no.interpolations', { locale: 'es' });
assert.equal(result, 'texto sin interpolaciones');
assert.equal(warnings.length, 1);
});

test("locale can be overridden and does not warn if allowLocaleOverride is true", function(assert) {
const i18n = this.subject({ locale: 'en', allowLocaleOverride: true });

const result = i18n.t('no.interpolations', { locale: 'es' });
assert.equal(result, 'texto sin interpolaciones');
assert.equal(warnings.length, 0);
});

test("locale can be used as an interpolation key if allowLocaleOverride is false", function(assert) {
const i18n = this.subject({ locale: 'en', allowLocaleOverride: false });

const result = i18n.t('with.locale.interpolation', { locale: 'es' });
assert.equal(result, 'Locale: es');
assert.equal(warnings.length, 0);
});

0 comments on commit 9e84028

Please sign in to comment.