8
0

locale.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. describe( 'Locale', () => {
  8. let locale;
  9. beforeEach( () => {
  10. locale = new Locale();
  11. } );
  12. afterEach( () => {
  13. sinon.restore();
  14. } );
  15. describe( 'constructor', () => {
  16. it( 'sets the #language', () => {
  17. const locale = new Locale( {
  18. uiLanguage: 'pl'
  19. } );
  20. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  21. } );
  22. it( 'sets the #contentLanguage', () => {
  23. const locale = new Locale( {
  24. uiLanguage: 'pl',
  25. contentLanguage: 'en'
  26. } );
  27. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  28. expect( locale ).to.have.property( 'contentLanguage', 'en' );
  29. } );
  30. it( 'defaults #language to en', () => {
  31. const locale = new Locale();
  32. expect( locale ).to.have.property( 'uiLanguage', 'en' );
  33. } );
  34. it( 'inherits the #contentLanguage from the #language (if not passed)', () => {
  35. const locale = new Locale( {
  36. uiLanguage: 'pl'
  37. } );
  38. expect( locale ).to.have.property( 'uiLanguage', 'pl' );
  39. expect( locale ).to.have.property( 'contentLanguage', 'pl' );
  40. } );
  41. it( 'determines the #uiLanguageDirection', () => {
  42. expect( new Locale( {
  43. uiLanguage: 'pl'
  44. } ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
  45. expect( new Locale( {
  46. uiLanguage: 'en'
  47. } ) ).to.have.property( 'uiLanguageDirection', 'ltr' );
  48. expect( new Locale( {
  49. uiLanguage: 'ar'
  50. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  51. expect( new Locale( {
  52. uiLanguage: 'fa'
  53. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  54. expect( new Locale( {
  55. uiLanguage: 'he'
  56. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  57. expect( new Locale( {
  58. uiLanguage: 'ku'
  59. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  60. expect( new Locale( {
  61. uiLanguage: 'ug'
  62. } ) ).to.have.property( 'uiLanguageDirection', 'rtl' );
  63. } );
  64. it( 'determines the #contentLanguageDirection (not passed)', () => {
  65. expect( new Locale( {
  66. uiLanguage: 'pl'
  67. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  68. expect( new Locale( {
  69. uiLanguage: 'en'
  70. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  71. expect( new Locale( {
  72. uiLanguage: 'ar'
  73. } ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  74. } );
  75. it( 'determines the #contentLanguageDirection (passed)', () => {
  76. expect( new Locale( {
  77. uiLanguage: 'pl',
  78. contentLanguage: 'pl'
  79. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  80. expect( new Locale( {
  81. uiLanguage: 'en',
  82. contentLanguage: 'ar'
  83. } ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  84. expect( new Locale( {
  85. uiLanguage: 'ar',
  86. contentLanguage: 'pl'
  87. } ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  88. } );
  89. } );
  90. describe( 't', () => {
  91. it( 'has the context bound', () => {
  92. const t = locale.t;
  93. expect( t( 'Foo' ) ).to.equal( 'Foo' );
  94. } );
  95. it( 'interpolates 1 value', () => {
  96. const t = locale.t;
  97. expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
  98. } );
  99. it( 'interpolates 3 values', () => {
  100. const t = locale.t;
  101. expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
  102. } );
  103. // Those test make sure that if %0 is really to be used, then it's going to work.
  104. // It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
  105. it( 'does not interpolate placeholders if values not passed', () => {
  106. const t = locale.t;
  107. expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
  108. } );
  109. it( 'does not interpolate those placeholders for which values has not been passed', () => {
  110. const t = locale.t;
  111. expect( t( '%1 - %0 - %2', [ 'a' ] ) ).to.equal( '%1 - a - %2' );
  112. } );
  113. } );
  114. describe( 'language()', () => {
  115. it( 'should return #uiLanguage', () => {
  116. const stub = sinon.stub( console, 'warn' );
  117. expect( locale.language ).to.equal( locale.uiLanguage );
  118. sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
  119. } );
  120. it( 'should warn about deprecation', () => {
  121. const stub = sinon.stub( console, 'warn' );
  122. expect( locale.language ).to.equal( 'en' );
  123. sinon.assert.calledWithMatch( stub, 'locale-deprecated-language-property' );
  124. } );
  125. } );
  126. } );