8
0

locale.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console */
  6. import Locale from '../src/locale';
  7. import {
  8. add as addTranslations,
  9. _clear as clearTranslations
  10. } from '../src/translation-service';
  11. describe( 'Locale', () => {
  12. afterEach( () => {
  13. clearTranslations();
  14. sinon.restore();
  15. } );
  16. describe( 'constructor', () => {
  17. it( 'sets the #language', () => {
  18. const locale = new Locale( {
  19. uiLanguage: 'pl'
  20. } );
  21. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  22. } );
  23. it( 'sets the #contentLanguage', () => {
  24. const locale = new Locale( {
  25. uiLanguage: 'pl',
  26. contentLanguage: 'en'
  27. } );
  28. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  29. expect( locale ).to.have.property( 'contentLanguage', 'en' );
  30. } );
  31. it( 'defaults #language to en', () => {
  32. const locale = new Locale();
  33. expect( locale ).to.have.property( 'uiLanguage', 'en' );
  34. } );
  35. it( 'inherits the #contentLanguage from the #language (if not passed)', () => {
  36. const locale = new Locale( {
  37. uiLanguage: 'pl'
  38. } );
  39. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  40. expect( locale ).to.have.property( 'contentLanguage', 'pl' );
  41. } );
  42. it( 'determines the #uiLanguageDirection', () => {
  43. expect( new Locale( {
  44. uiLanguage: 'pl'
  45. } ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
  46. expect( new Locale( {
  47. uiLanguage: 'en'
  48. } ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
  49. expect( new Locale( {
  50. uiLanguage: 'ar'
  51. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  52. expect( new Locale( {
  53. uiLanguage: 'fa'
  54. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  55. expect( new Locale( {
  56. uiLanguage: 'he'
  57. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  58. expect( new Locale( {
  59. uiLanguage: 'ku'
  60. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  61. expect( new Locale( {
  62. uiLanguage: 'ug'
  63. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  64. } );
  65. it( 'determines the #contentLanguageDirection (not passed)', () => {
  66. expect( new Locale( {
  67. uiLanguage: 'pl'
  68. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  69. expect( new Locale( {
  70. uiLanguage: 'en'
  71. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  72. expect( new Locale( {
  73. uiLanguage: 'ar'
  74. } ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  75. } );
  76. it( 'determines the #contentLanguageDirection (passed)', () => {
  77. expect( new Locale( {
  78. uiLanguage: 'pl',
  79. contentLanguage: 'pl'
  80. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  81. expect( new Locale( {
  82. uiLanguage: 'en',
  83. contentLanguage: 'ar'
  84. } ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  85. expect( new Locale( {
  86. uiLanguage: 'ar',
  87. contentLanguage: 'pl'
  88. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  89. } );
  90. } );
  91. describe( 't', () => {
  92. let locale;
  93. beforeEach( () => {
  94. // eslint-disable-next-line no-nested-ternary
  95. const getPolishPluralForm = n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2;
  96. addTranslations( 'pl', {
  97. 'foo': 'foo_pl',
  98. 'bar': [ 'bar_pl_0', '%0 bar_pl_1', '%0 bar_pl_2' ]
  99. }, getPolishPluralForm );
  100. addTranslations( 'de', {
  101. 'foo': 'foo_de',
  102. 'bar': [ 'bar_de_0', '%0 bar_de_1', '%0 bar_de_2' ]
  103. } );
  104. locale = new Locale( {
  105. uiLanguage: 'pl',
  106. contentLanguage: 'de'
  107. } );
  108. } );
  109. it( 'should translate a message to the target ui language', () => {
  110. const t = locale.t;
  111. expect( t( 'foo' ) ).to.equal( 'foo_pl' );
  112. } );
  113. it( 'should translate a message including the message string and the message context', () => {
  114. const t = locale.t;
  115. addTranslations( 'pl', {
  116. 'foo_bar': 'foo_bar_pl'
  117. } );
  118. expect( t( { string: 'foo', context: 'bar' } ) ).to.equal( 'foo_bar_pl' );
  119. } );
  120. it( 'should translate a plural form to the target ui language based on the first value and interpolate the string', () => {
  121. const t = locale.t;
  122. expect( t( { string: 'bar', plural: '%0 bars' }, [ 1 ] ), 1 ).to.equal( 'bar_pl_0' );
  123. expect( t( { string: 'bar', plural: '%0 bars' }, [ 2 ] ), 2 ).to.equal( '2 bar_pl_1' );
  124. expect( t( { string: 'bar', plural: '%0 bars' }, [ 5 ] ), 3 ).to.equal( '5 bar_pl_2' );
  125. } );
  126. it( 'should translate a message supporting plural form with a context', () => {
  127. const t = locale.t;
  128. addTranslations( 'pl', {
  129. '%1 a space_Add/Remove a space': [ '%1 spację', '%1 %0 spacje', '%1 %0 spacji' ],
  130. 'Add': 'Dodaj',
  131. 'Remove': 'Usuń'
  132. } );
  133. const addOrRemoveSpaceMessage = { string: '%1 a space', plural: '%1 %0 spaces', context: 'Add/Remove a space' };
  134. expect( t( addOrRemoveSpaceMessage, [ 1, t( 'Add' ) ] ), 1 ).to.equal( 'Dodaj spację' );
  135. expect( t( addOrRemoveSpaceMessage, [ 2, t( 'Remove' ) ] ), 2 ).to.equal( 'Usuń 2 spacje' );
  136. expect( t( addOrRemoveSpaceMessage, [ 5, t( 'Add' ) ] ), 3 ).to.equal( 'Dodaj 5 spacji' );
  137. } );
  138. it( 'should interpolate a message with provided values', () => {
  139. const t = locale.t;
  140. expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
  141. expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
  142. // Those test make sure that if %0 is really to be used, then it's going to work.
  143. // It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
  144. expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
  145. expect( t( '%1 - %0 - %2', [ 'a' ] ) ).to.equal( '%1 - a - %2' );
  146. } );
  147. } );
  148. describe( 'language()', () => {
  149. it( 'should return #uiLanguage', () => {
  150. const stub = sinon.stub( console, 'warn' );
  151. const locale = new Locale();
  152. expect( locale.language ).to.equal( locale.uiLanguage );
  153. sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
  154. } );
  155. it( 'should warn about deprecation', () => {
  156. const stub = sinon.stub( console, 'warn' );
  157. const locale = new Locale();
  158. expect( locale.language ).to.equal( 'en' );
  159. sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
  160. } );
  161. } );
  162. } );