This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
Allow overriding the locale for individual translations #312
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
import Ember from 'ember'; | ||
import { moduleFor, test } from 'ember-qunit'; | ||
|
||
const { run, deprecate:originalDeprecate } = Ember; | ||
const { run, deprecate:originalDeprecate, warn:originalWarn } = Ember; | ||
const warnings = Ember.A(); | ||
|
||
moduleFor('service:i18n', 'I18nService#t', { | ||
integration: true, | ||
|
||
beforeEach() { | ||
Ember.warn = function(message, test, options) { | ||
if (!test) { warnings.pushObject(message); } | ||
originalWarn(message, test, options); | ||
}; | ||
}, | ||
|
||
afterEach() { | ||
Ember.deprecate = originalDeprecate; | ||
Ember.warn = originalWarn; | ||
warnings.clear(); | ||
} | ||
}); | ||
|
||
|
@@ -40,7 +50,7 @@ test('returns "missing translation" translations', function(assert) { | |
assert.equal('Missing translation: not.yet.translated', result); | ||
}); | ||
|
||
test('warns on the presence of htmlSafe and locale', function(assert) { | ||
test('warns on the presence of htmlSafe', function(assert) { | ||
const service = this.subject(); | ||
let deprecations = 0; | ||
|
||
|
@@ -49,20 +59,17 @@ test('warns on the presence of htmlSafe and locale', function(assert) { | |
|
||
const { id } = options; | ||
|
||
if (id === 'ember-i18n.reserve-htmlSafe' || id === 'ember-i18n.reserve-locale') { | ||
if (id === 'ember-i18n.reserve-htmlSafe') { | ||
deprecations += 1; | ||
} | ||
}; | ||
|
||
service.t('not.yet.translated', { htmlSafe: true }); | ||
assert.equal(deprecations, 1); | ||
|
||
service.t('not.yet.translated', { locale: true }); | ||
assert.equal(deprecations, 2); | ||
|
||
service.t('not.yet.translated'); | ||
service.t('not.yet.translated', { some: 'other key' }); | ||
assert.equal(deprecations, 2); | ||
assert.equal(deprecations, 1); | ||
}); | ||
|
||
test('emits "missing" events', function(assert) { | ||
|
@@ -120,3 +127,42 @@ 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); | ||
}); | ||
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. These tests are awesome :) |
||
|
||
test('emits "missing" events with an overridden locale', function(assert) { | ||
const i18n = this.subject({ locale: 'en' }); | ||
const calls = []; | ||
function spy() { calls.push(arguments); } | ||
i18n.on('missing', spy); | ||
|
||
i18n.t('not.yet.translated', { some: 'data', locale: 'es' }); | ||
|
||
assert.equal(calls.length, 1); | ||
assert.equal(calls[0].length, 3); | ||
assert.equal(calls[0][0], 'es'); | ||
assert.equal(calls[0][1], 'not.yet.translated'); | ||
assert.equal(calls[0][2].some, 'data'); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like the symmetry of
t
andexists
now. I'm glad you did that extraction.