locale.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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( 'pl' );
  14. expect( locale ).to.have.property( 'language', 'pl' );
  15. } );
  16. it( 'sets the #contentLanguage', () => {
  17. const locale = new Locale( 'pl', 'en' );
  18. expect( locale ).to.have.property( 'language', 'pl' );
  19. expect( locale ).to.have.property( 'contentLanguage', 'en' );
  20. } );
  21. it( 'defaults #language to en', () => {
  22. const locale = new Locale();
  23. expect( locale ).to.have.property( 'language', 'en' );
  24. } );
  25. it( 'inherits the #contentLanguage from the #language (if not passed)', () => {
  26. const locale = new Locale( 'pl' );
  27. expect( locale ).to.have.property( 'language', 'pl' );
  28. expect( locale ).to.have.property( 'contentLanguage', 'pl' );
  29. } );
  30. it( 'determines the #languageDirection', () => {
  31. expect( new Locale( 'pl' ) ).to.have.property( 'languageDirection', 'ltr' );
  32. expect( new Locale( 'en' ) ).to.have.property( 'languageDirection', 'ltr' );
  33. expect( new Locale( 'ar' ) ).to.have.property( 'languageDirection', 'rtl' );
  34. expect( new Locale( 'fa' ) ).to.have.property( 'languageDirection', 'rtl' );
  35. expect( new Locale( 'he' ) ).to.have.property( 'languageDirection', 'rtl' );
  36. expect( new Locale( 'ku' ) ).to.have.property( 'languageDirection', 'rtl' );
  37. expect( new Locale( 'ug' ) ).to.have.property( 'languageDirection', 'rtl' );
  38. } );
  39. it( 'determines the #contentLanguageDirection (not passed)', () => {
  40. expect( new Locale( 'pl' ) ).to.have.property( 'contentLanguageDirection', 'auto' );
  41. expect( new Locale( 'en' ) ).to.have.property( 'contentLanguageDirection', 'auto' );
  42. expect( new Locale( 'ar' ) ).to.have.property( 'contentLanguageDirection', 'auto' );
  43. } );
  44. it( 'determines the #contentLanguageDirection (passed)', () => {
  45. expect( new Locale( 'pl', 'pl' ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  46. expect( new Locale( 'en', 'ar' ) ).to.have.property( 'contentLanguageDirection', 'rtl' );
  47. expect( new Locale( 'ar', 'pl' ) ).to.have.property( 'contentLanguageDirection', 'ltr' );
  48. } );
  49. } );
  50. describe( 't', () => {
  51. it( 'has the context bound', () => {
  52. const t = locale.t;
  53. expect( t( 'Foo' ) ).to.equal( 'Foo' );
  54. } );
  55. it( 'interpolates 1 value', () => {
  56. const t = locale.t;
  57. expect( t( '%0 - %0', [ 'foo' ] ) ).to.equal( 'foo - foo' );
  58. } );
  59. it( 'interpolates 3 values', () => {
  60. const t = locale.t;
  61. expect( t( '%1 - %0 - %2', [ 'a', 'b', 'c' ] ) ).to.equal( 'b - a - c' );
  62. } );
  63. // Those test make sure that if %0 is really to be used, then it's going to work.
  64. // It'd be a super rare case if one would need to use %0 and at the same time interpolate something.
  65. it( 'does not interpolate placeholders if values not passed', () => {
  66. const t = locale.t;
  67. expect( t( '%1 - %0 - %2' ) ).to.equal( '%1 - %0 - %2' );
  68. } );
  69. it( 'does not interpolate those placeholders for which values has not been passed', () => {
  70. const t = locale.t;
  71. expect( t( '%1 - %0 - %2', [ 'a' ] ) ).to.equal( '%1 - a - %2' );
  72. } );
  73. } );
  74. } );