8
0

locale.js 4.3 KB

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