translation-service.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. import { _translate, add, _clear } from '../src/translation-service';
  6. describe( 'translation-service', () => {
  7. afterEach( () => {
  8. _clear();
  9. } );
  10. describe( 'add()', () => {
  11. it( 'should merge translation added several times', () => {
  12. add( 'pl', { 'foo': 'foo_pl' } );
  13. add( 'pl', { 'bar': 'bar_pl' } );
  14. const translatedFoo = _translate( 'pl', { string: 'foo' } );
  15. const translatedBar = _translate( 'pl', { string: 'bar' } );
  16. expect( translatedFoo ).to.equal( 'foo_pl' );
  17. expect( translatedBar ).to.equal( 'bar_pl' );
  18. } );
  19. it( 'should overwrite previously added translations for the same message ids', () => {
  20. add( 'pl', { 'foo': 'First' } );
  21. add( 'pl', { 'foo': 'Second' } );
  22. const translatedFoo = _translate( 'pl', { string: 'foo' } );
  23. expect( translatedFoo ).to.equal( 'Second' );
  24. } );
  25. it( 'should set the plural form function if it is provided', () => {
  26. add( 'pl', {
  27. 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
  28. } );
  29. // eslint-disable-next-line no-nested-ternary
  30. add( 'pl', {}, n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
  31. expect( _translate( 'pl', { string: 'Add space' }, 0 ) ).to.equal( 'Dodaj %0 spacji' );
  32. expect( _translate( 'pl', { string: 'Add space' }, 1 ) ).to.equal( 'Dodaj spację' );
  33. expect( _translate( 'pl', { string: 'Add space' }, 3 ) ).to.equal( 'Dodaj %0 spacje' );
  34. expect( _translate( 'pl', { string: 'Add space' }, 13 ) ).to.equal( 'Dodaj %0 spacji' );
  35. } );
  36. } );
  37. describe( '_translate()', () => {
  38. it( 'should return translated messages when translations are defined', () => {
  39. add( 'pl', {
  40. 'OK': 'OK',
  41. 'Cancel': 'Anuluj'
  42. } );
  43. add( 'en_US', {
  44. 'OK': 'OK',
  45. 'Cancel': 'Cancel'
  46. } );
  47. const translatedCancelPL = _translate( 'pl', { string: 'Cancel' } );
  48. const translatedCancelEN = _translate( 'en', { string: 'Cancel' } );
  49. expect( translatedCancelPL ).to.equal( 'Anuluj' );
  50. expect( translatedCancelEN ).to.equal( 'Cancel' );
  51. } );
  52. it( 'should return the original message string if no translation exists for the given message', () => {
  53. const translatedBold = _translate( 'pl', { string: 'Bold' } );
  54. expect( translatedBold ).to.equal( 'Bold' );
  55. } );
  56. it( 'should return the correct plural form of english message if no translation exists for the given message', () => {
  57. const addSpaces = _translate( 'pl', { string: 'Add a space', plural: 'Add %0 spaces' }, 3 );
  58. const addASpace = _translate( 'pl', { string: 'Add a space', plural: 'Add %0 spaces' }, 1 );
  59. expect( addSpaces ).to.equal( 'Add %0 spaces' );
  60. expect( addASpace ).to.equal( 'Add a space' );
  61. } );
  62. it( 'should return the original message string if a translation for the target language does not exist' +
  63. 'but translation doesn\'t', () => {
  64. add( 'pl', {
  65. 'OK': 'OK',
  66. 'Cancel': 'Anuluj'
  67. } );
  68. const translatedBold = _translate( 'pl', { string: 'Bold' } );
  69. expect( translatedBold ).to.equal( 'Bold' );
  70. } );
  71. it( 'should return a translated message when only one language is provided', () => {
  72. add( 'pl', {
  73. 'OK': 'OK',
  74. 'Cancel': 'Anuluj'
  75. } );
  76. const translatedCancel = _translate( 'de', { string: 'Cancel' } );
  77. expect( translatedCancel ).to.equal( 'Anuluj' );
  78. } );
  79. it( 'should return a translated message based on message id when it was passed', () => {
  80. add( 'pl', {
  81. 'ADD_IMAGE': 'obraz'
  82. } );
  83. const translatedFooBar = _translate( 'pl', { string: 'image', id: 'ADD_IMAGE' } );
  84. expect( translatedFooBar ).to.equal( 'obraz' );
  85. } );
  86. it( 'should return the correct plural form of the message based on the provided function', () => {
  87. add( 'pl', {
  88. 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ],
  89. 'Cancel': 'Anuluj'
  90. // eslint-disable-next-line no-nested-ternary
  91. }, n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
  92. expect( _translate( 'pl', { string: 'Add space' }, 0 ) ).to.equal( 'Dodaj %0 spacji' );
  93. expect( _translate( 'pl', { string: 'Add space' }, 1 ) ).to.equal( 'Dodaj spację' );
  94. expect( _translate( 'pl', { string: 'Add space' }, 3 ) ).to.equal( 'Dodaj %0 spacje' );
  95. expect( _translate( 'pl', { string: 'Add space' }, 13 ) ).to.equal( 'Dodaj %0 spacji' );
  96. } );
  97. it( 'should return a plural form based on rules for English if no function to determine the plural form was provided', () => {
  98. add( 'pl', {
  99. 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ],
  100. 'Cancel': 'Anuluj'
  101. } );
  102. expect( _translate( 'pl', { string: 'Add space' }, 1 ) ).to.equal( 'Dodaj spację' );
  103. expect( _translate( 'pl', { string: 'Add space' }, 0 ) ).to.equal( 'Dodaj %0 spacje' );
  104. expect( _translate( 'pl', { string: 'Add space' }, 3 ) ).to.equal( 'Dodaj %0 spacje' );
  105. expect( _translate( 'pl', { string: 'Add space' }, 13 ) ).to.equal( 'Dodaj %0 spacje' );
  106. } );
  107. it( 'should support a plural form rule that returns a boolean', () => {
  108. add( 'pl', {
  109. 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje' ],
  110. 'Cancel': 'Anuluj'
  111. }, n => n !== 1 );
  112. expect( _translate( 'pl', { string: 'Add space' }, 1 ) ).to.equal( 'Dodaj spację' );
  113. expect( _translate( 'pl', { string: 'Add space' }, 0 ) ).to.equal( 'Dodaj %0 spacje' );
  114. expect( _translate( 'pl', { string: 'Add space' }, 3 ) ).to.equal( 'Dodaj %0 spacje' );
  115. expect( _translate( 'pl', { string: 'Add space' }, 13 ) ).to.equal( 'Dodaj %0 spacje' );
  116. } );
  117. } );
  118. } );