|
|
@@ -6,15 +6,14 @@
|
|
|
/* globals console */
|
|
|
|
|
|
import Locale from '../src/locale';
|
|
|
+import {
|
|
|
+ add as addTranslations,
|
|
|
+ _clear as clearTranslations
|
|
|
+} from '../src/translation-service';
|
|
|
|
|
|
describe( 'Locale', () => {
|
|
|
- let locale;
|
|
|
-
|
|
|
- beforeEach( () => {
|
|
|
- locale = new Locale();
|
|
|
- } );
|
|
|
-
|
|
|
afterEach( () => {
|
|
|
+ clearTranslations();
|
|
|
sinon.restore();
|
|
|
} );
|
|
|
|
|
|
@@ -115,35 +114,51 @@ describe( 'Locale', () => {
|
|
|
} );
|
|
|
|
|
|
describe( 't', () => {
|
|
|
- it( 'has the context bound', () => {
|
|
|
- const t = locale.t;
|
|
|
+ let locale;
|
|
|
|
|
|
- expect( t( 'Foo' ) ).to.equal( 'Foo' );
|
|
|
- } );
|
|
|
+ beforeEach( () => {
|
|
|
+ // eslint-disable-next-line no-nested-ternary
|
|
|
+ const getPolishPluralForm = n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2;
|
|
|
|
|
|
- it( 'interpolates 1 value', () => {
|
|
|
- const t = locale.t;
|
|
|
+ addTranslations( 'pl', {
|
|
|
+ 'foo': 'foo_pl',
|
|
|
+ 'bar': [ 'bar_pl_0', '%0 bar_pl_1', '%0 bar_pl_2' ]
|
|
|
+ }, getPolishPluralForm );
|
|
|
|
|
|
- expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
|
|
|
+ addTranslations( 'de', {
|
|
|
+ 'foo': 'foo_de',
|
|
|
+ 'bar': [ 'bar_de_0', '%0 bar_de_1', '%0 bar_de_2' ]
|
|
|
+ } );
|
|
|
+
|
|
|
+ locale = new Locale( {
|
|
|
+ uiLanguage: 'pl',
|
|
|
+ contentLanguage: 'de'
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- it( 'interpolates 3 values', () => {
|
|
|
+ it( 'should translate a string to the target ui language', () => {
|
|
|
const t = locale.t;
|
|
|
|
|
|
- expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
|
|
|
+ expect( t( 'foo' ) ).to.equal( 'foo_pl' );
|
|
|
} );
|
|
|
|
|
|
- // Those test make sure that if %0 is really to be used, then it's going to work.
|
|
|
- // It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
|
|
|
- it( 'does not interpolate placeholders if values not passed', () => {
|
|
|
+ it( 'should translate a plural form to the target ui language based on the first value and interpolate the string', () => {
|
|
|
const t = locale.t;
|
|
|
|
|
|
- expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
|
|
|
+ expect( t( { string: 'bar', plural: '%0 bars' }, [ 1 ] ), 1 ).to.equal( 'bar_pl_0' );
|
|
|
+ expect( t( { string: 'bar', plural: '%0 bars' }, [ 2 ] ), 2 ).to.equal( '2 bar_pl_1' );
|
|
|
+ expect( t( { string: 'bar', plural: '%0 bars' }, [ 5 ] ), 3 ).to.equal( '5 bar_pl_2' );
|
|
|
} );
|
|
|
|
|
|
- it( 'does not interpolate those placeholders for which values has not been passed', () => {
|
|
|
+ it( 'should interpolate a message with provided values', () => {
|
|
|
const t = locale.t;
|
|
|
|
|
|
+ expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
|
|
|
+ expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
|
|
|
+
|
|
|
+ // Those test make sure that if %0 is really to be used, then it's going to work.
|
|
|
+ // It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
|
|
|
+ expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
|
|
|
expect( t( '%1 - %0 - %2', [ 'a' ] ) ).to.equal( '%1 - a - %2' );
|
|
|
} );
|
|
|
} );
|
|
|
@@ -151,6 +166,7 @@ describe( 'Locale', () => {
|
|
|
describe( 'language()', () => {
|
|
|
it( 'should return #uiLanguage', () => {
|
|
|
const stub = sinon.stub( console, 'warn' );
|
|
|
+ const locale = new Locale();
|
|
|
|
|
|
expect( locale.language ).to.equal( locale.uiLanguage );
|
|
|
sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
|
|
|
@@ -158,6 +174,7 @@ describe( 'Locale', () => {
|
|
|
|
|
|
it( 'should warn about deprecation', () => {
|
|
|
const stub = sinon.stub( console, 'warn' );
|
|
|
+ const locale = new Locale();
|
|
|
|
|
|
expect( locale.language ).to.equal( 'en' );
|
|
|
sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
|