8
0

locale.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. import { expectToThrowCKEditorError } from './_utils/utils';
  12. describe( 'Locale', () => {
  13. afterEach( () => {
  14. clearTranslations();
  15. sinon.restore();
  16. } );
  17. describe( 'constructor', () => {
  18. it( 'sets the #language', () => {
  19. const locale = new Locale( {
  20. uiLanguage: 'pl'
  21. } );
  22. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  23. } );
  24. it( 'sets the #contentLanguage', () => {
  25. const locale = new Locale( {
  26. uiLanguage: 'pl',
  27. contentLanguage: 'en'
  28. } );
  29. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  30. expect( locale ).to.have.property( 'contentLanguage', 'en' );
  31. } );
  32. it( 'defaults #language to en', () => {
  33. const locale = new Locale();
  34. expect( locale ).to.have.property( 'uiLanguage', 'en' );
  35. } );
  36. it( 'inherits the #contentLanguage from the #language (if not passed)', () => {
  37. const locale = new Locale( {
  38. uiLanguage: 'pl'
  39. } );
  40. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  41. expect( locale ).to.have.property( 'contentLanguage', 'pl' );
  42. } );
  43. it( 'determines the #uiLanguageDirection', () => {
  44. expect( new Locale( {
  45. uiLanguage: 'pl'
  46. } ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
  47. expect( new Locale( {
  48. uiLanguage: 'en'
  49. } ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
  50. expect( new Locale( {
  51. uiLanguage: 'ar'
  52. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  53. expect( new Locale( {
  54. uiLanguage: 'fa'
  55. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  56. expect( new Locale( {
  57. uiLanguage: 'he'
  58. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  59. expect( new Locale( {
  60. uiLanguage: 'ku'
  61. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  62. expect( new Locale( {
  63. uiLanguage: 'ug'
  64. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  65. } );
  66. it( 'determines the #contentLanguageDirection (not passed)', () => {
  67. expect( new Locale( {
  68. uiLanguage: 'pl'
  69. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  70. expect( new Locale( {
  71. uiLanguage: 'en'
  72. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  73. expect( new Locale( {
  74. uiLanguage: 'ar'
  75. } ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  76. } );
  77. it( 'determines the #contentLanguageDirection (passed)', () => {
  78. expect( new Locale( {
  79. uiLanguage: 'pl',
  80. contentLanguage: 'pl'
  81. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  82. expect( new Locale( {
  83. uiLanguage: 'en',
  84. contentLanguage: 'ar'
  85. } ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  86. expect( new Locale( {
  87. uiLanguage: 'ar',
  88. contentLanguage: 'pl'
  89. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  90. } );
  91. } );
  92. describe( 't', () => {
  93. let locale;
  94. beforeEach( () => {
  95. // eslint-disable-next-line no-nested-ternary
  96. const getPolishPluralForm = n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2;
  97. addTranslations( 'pl', {
  98. 'foo': 'foo_pl',
  99. 'bar': [ 'bar_pl_0', '%0 bar_pl_1', '%0 bar_pl_2' ]
  100. }, getPolishPluralForm );
  101. addTranslations( 'de', {
  102. 'foo': 'foo_de',
  103. 'bar': [ 'bar_de_0', '%0 bar_de_1', '%0 bar_de_2' ]
  104. } );
  105. locale = new Locale( {
  106. uiLanguage: 'pl',
  107. contentLanguage: 'de'
  108. } );
  109. } );
  110. it( 'should translate a message to the target ui language', () => {
  111. const t = locale.t;
  112. expect( t( 'foo' ) ).to.equal( 'foo_pl' );
  113. } );
  114. it( 'should translate a message using the message id if it was passed', () => {
  115. const t = locale.t;
  116. addTranslations( 'pl', {
  117. 'ADD_IMAGE': 'obrazek',
  118. 'image': 'foo'
  119. } );
  120. expect( t( { string: 'image', id: 'ADD_IMAGE' } ) ).to.equal( 'obrazek' );
  121. } );
  122. it( 'should translate a message supporting plural forms', () => {
  123. const t = locale.t;
  124. expect( t( { string: 'bar', plural: '%0 bars' }, 1 ), 1 ).to.equal( 'bar_pl_0' );
  125. expect( t( { string: 'bar', plural: '%0 bars' }, 2 ), 2 ).to.equal( '2 bar_pl_1' );
  126. expect( t( { string: 'bar', plural: '%0 bars' }, 5 ), 3 ).to.equal( '5 bar_pl_2' );
  127. } );
  128. it( 'should translate a message supporting plural forms with a message id if it was passed', () => {
  129. const t = locale.t;
  130. addTranslations( 'pl', {
  131. 'ADD_SPACE': [ '%1 spację', '%1 %0 spacje', '%1 %0 spacji' ],
  132. 'Add': 'Dodaj',
  133. 'Remove': 'Usuń'
  134. } );
  135. const addOrRemoveSpaceMessage = { string: '%1 a space', plural: '%1 %0 spaces', id: 'ADD_SPACE' };
  136. expect( t( addOrRemoveSpaceMessage, [ 1, t( 'Add' ) ] ), 1 ).to.equal( 'Dodaj spację' );
  137. expect( t( addOrRemoveSpaceMessage, [ 2, t( 'Remove' ) ] ), 2 ).to.equal( 'Usuń 2 spacje' );
  138. expect( t( addOrRemoveSpaceMessage, [ 5, t( 'Add' ) ] ), 3 ).to.equal( 'Dodaj 5 spacji' );
  139. } );
  140. it( 'should interpolate a message with provided values', () => {
  141. const t = locale.t;
  142. expect( t( '%0 - %0', 'foo' ) ).to.equal( 'foo - foo' );
  143. expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
  144. // Those test make sure that if %0 is really to be used, then it's going to work.
  145. // It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
  146. expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
  147. expect( t( '%1 - %0 - %2', 'a' ) ).to.equal( '%1 - a - %2' );
  148. } );
  149. it( 'should interpolate a message with a provided value (shorthand version)', () => {
  150. const t = locale.t;
  151. expect( t( 'Add %0', 'space' ) ).to.equal( 'Add space' );
  152. expect( t( 'Remove %0 %1', 'spaces' ) ).to.equal( 'Remove spaces %1' );
  153. expect( t( '%0 bar %0', 'foo' ) ).to.equal( 'foo bar foo' );
  154. } );
  155. it( 'should throw an error when a value used to determine the plural version is not a number', () => {
  156. const t = locale.t;
  157. expectToThrowCKEditorError( () => {
  158. t( { string: 'Add space', plural: 'Add %0 spaces' }, 'space' );
  159. }, 'translation-service-quantity-not-a-number', null, { quantity: 'space' } );
  160. expectToThrowCKEditorError( () => {
  161. t( { string: 'Add space', plural: 'Add %0 spaces' }, [ 'space' ] );
  162. }, 'translation-service-quantity-not-a-number', null, { quantity: 'space' } );
  163. expect( () => {
  164. t( { string: 'Add space', plural: 'Add %0 spaces' }, [ 3 ] );
  165. t( { string: 'Add space', plural: 'Add %0 spaces' }, 3 );
  166. t( { string: 'Add %1', plural: 'Add %0 %1' }, [ 3, 'spaces' ] );
  167. t( { string: 'Add %0' }, [ 'space' ] );
  168. t( { string: 'Add %0' }, 'space' );
  169. } ).to.not.throw();
  170. } );
  171. } );
  172. describe( 'language()', () => {
  173. it( 'should return #uiLanguage', () => {
  174. const stub = sinon.stub( console, 'warn' );
  175. const locale = new Locale();
  176. expect( locale.language ).to.equal( locale.uiLanguage );
  177. sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
  178. } );
  179. it( 'should warn about deprecation', () => {
  180. const stub = sinon.stub( console, 'warn' );
  181. const locale = new Locale();
  182. expect( locale.language ).to.equal( 'en' );
  183. sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
  184. } );
  185. } );
  186. } );