8
0

translation-service.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window */
  6. /**
  7. * @module utils/translation-service
  8. */
  9. let dictionaries = {};
  10. /**
  11. * Adds package translations to existing ones.
  12. * These translations will later be available for {@link module:utils/translation-service~translate translate}.
  13. *
  14. * add( 'pl', {
  15. * 'OK': 'OK',
  16. * 'Cancel [context: reject]': 'Anuluj'
  17. * } );
  18. *
  19. * @param {String} lang Target language.
  20. * @param {Object.<String, String>} translations Translations which will be added to the dictionary.
  21. */
  22. export function add( lang, translations ) {
  23. dictionaries[ lang ] = dictionaries[ lang ] || {};
  24. Object.assign( dictionaries[ lang ], translations );
  25. }
  26. /**
  27. * Translates string if the translation of the string was previously {@link module:utils/translation-service~add added}
  28. * to the dictionary. This happens in a multi-language mode were translation modules are created by the bundler.
  29. *
  30. * When no translation is defined in the dictionary or the dictionary doesn't exist this function returns
  31. * the original string without the `'[context: ]'` (happens in development and single-language modes).
  32. *
  33. * In a single-language mode (when values passed to `t()` were replaced with target language strings) the dictionary
  34. * is left empty, so this function will return the original strings always.
  35. *
  36. * translate( 'pl', 'Cancel [context: reject]' );
  37. *
  38. * @param {String} lang Target language.
  39. * @param {String} translationKey String which is going to be translated.
  40. * @returns {String} Translated sentence.
  41. */
  42. export function translate( lang, translationKey ) {
  43. const numberOfLanguages = getNumberOfLanguages();
  44. if ( numberOfLanguages === 1 ) {
  45. // Override the language to the only supported one.
  46. // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initializes.
  47. lang = Object.keys( dictionaries )[ 0 ];
  48. }
  49. if ( numberOfLanguages === 0 || !hasTranslation( lang, translationKey ) ) {
  50. return translationKey.replace( / \[context: [^\]]+\]$/, '' );
  51. }
  52. return dictionaries[ lang ][ translationKey ];
  53. }
  54. // Checks whether the dictionary exists and translation in that dictionary exists.
  55. function hasTranslation( lang, translationKey ) {
  56. return (
  57. ( lang in dictionaries ) &&
  58. ( translationKey in dictionaries[ lang ] )
  59. );
  60. }
  61. /**
  62. * Clears dictionaries for test purposes.
  63. *
  64. * @protected
  65. */
  66. export function _clear() {
  67. dictionaries = {};
  68. }
  69. function getNumberOfLanguages() {
  70. return Object.keys( dictionaries ).length;
  71. }
  72. // Export globally add function to enable adding later translations.
  73. // See https://github.com/ckeditor/ckeditor5/issues/624
  74. window.CKEDITOR_TRANSLATIONS = window.CKEDITOR_TRANSLATIONS || {};
  75. window.CKEDITOR_TRANSLATIONS.add = add;