8
0

translation-service.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window */
  6. /**
  7. * @module utils/translation-service
  8. */
  9. /* istanbul ignore else */
  10. if ( !window.CKEDITOR_TRANSLATIONS ) {
  11. window.CKEDITOR_TRANSLATIONS = {};
  12. }
  13. /**
  14. * Adds translations to existing ones.
  15. * These translations will later be available for the {@link module:utils/translation-service~translate `translate()`} function.
  16. *
  17. * add( 'pl', {
  18. * 'OK': 'OK',
  19. * 'Cancel [context: reject]': 'Anuluj'
  20. * } );
  21. *
  22. * If you cannot import this function from this module (e.g. because you use a CKEditor 5 build), then you can
  23. * still add translations by extending the global `window.CKEDITOR_TRANSLATIONS` object by using a function like
  24. * the one below:
  25. *
  26. * function addTranslations( language, translations ) {
  27. * if ( !window.CKEDITOR_TRANSLATIONS ) {
  28. * window.CKEDITOR_TRANSLATIONS = {};
  29. * }
  30. *
  31. * const dictionary = window.CKEDITOR_TRANSLATIONS[ language ] || ( window.CKEDITOR_TRANSLATIONS[ language ] = {} );
  32. *
  33. * // Extend the dictionary for the given language.
  34. * Object.assign( dictionary, translations );
  35. * }
  36. *
  37. * @param {String} language Target language.
  38. * @param {Object.<String, String>} translations Translations which will be added to the dictionary.
  39. */
  40. export function add( language, translations ) {
  41. const dictionary = window.CKEDITOR_TRANSLATIONS[ language ] || ( window.CKEDITOR_TRANSLATIONS[ language ] = {} );
  42. Object.assign( dictionary, translations );
  43. }
  44. /**
  45. * Translates string if the translation of the string was previously added to the dictionary.
  46. * See {@link module:utils/translation-service Translation Service}.
  47. * This happens in a multi-language mode were translation modules are created by the bundler.
  48. *
  49. * When no translation is defined in the dictionary or the dictionary doesn't exist this function returns
  50. * the original string without the `'[context: ]'` (happens in development and single-language modes).
  51. *
  52. * In a single-language mode (when values passed to `t()` were replaced with target language strings) the dictionary
  53. * is left empty, so this function will return the original strings always.
  54. *
  55. * translate( 'pl', 'Cancel [context: reject]' );
  56. *
  57. * @param {String} language Target language.
  58. * @param {String} translationKey String that will be translated.
  59. * @returns {String} Translated sentence.
  60. */
  61. export function translate( language, translationKey ) {
  62. const numberOfLanguages = getNumberOfLanguages();
  63. if ( numberOfLanguages === 1 ) {
  64. // Override the language to the only supported one.
  65. // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization.
  66. language = Object.keys( window.CKEDITOR_TRANSLATIONS )[ 0 ];
  67. }
  68. if ( numberOfLanguages === 0 || !hasTranslation( language, translationKey ) ) {
  69. return translationKey.replace( / \[context: [^\]]+\]$/, '' );
  70. }
  71. const dictionary = window.CKEDITOR_TRANSLATIONS[ language ];
  72. // In case of missing translations we still need to cut off the `[context: ]` parts.
  73. return dictionary[ translationKey ].replace( / \[context: [^\]]+\]$/, '' );
  74. }
  75. /**
  76. * Clears dictionaries for test purposes.
  77. *
  78. * @protected
  79. */
  80. export function _clear() {
  81. window.CKEDITOR_TRANSLATIONS = {};
  82. }
  83. // Checks whether the dictionary exists and translation in that dictionary exists.
  84. function hasTranslation( language, translationKey ) {
  85. return (
  86. ( language in window.CKEDITOR_TRANSLATIONS ) &&
  87. ( translationKey in window.CKEDITOR_TRANSLATIONS[ language ] )
  88. );
  89. }
  90. function getNumberOfLanguages() {
  91. return Object.keys( window.CKEDITOR_TRANSLATIONS ).length;
  92. }