8
0

locale.js 3.8 KB

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