locale.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  6. * @module utils/locale
  7. */
  8. /* globals console */
  9. import { translate } from './translation-service';
  10. const RTL_LANGUAGE_CODES = [ 'ar', 'fa', 'he', 'ku', 'ug' ];
  11. /**
  12. * Represents the localization services.
  13. */
  14. export default class Locale {
  15. /**
  16. * Creates a new instance of the Locale class. Learn more about
  17. * {@glink features/ui-language configuring language of the editor}.
  18. *
  19. * @param {Object} [options] Locale configuration.
  20. * @param {String} [options.uiLanguage='en'] The editor UI language code in the
  21. * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. See {@link #uiLanguage}.
  22. * @param {String} [options.contentLanguage] The editor content language code in the
  23. * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
  24. * See {@link #contentLanguage}.
  25. */
  26. constructor( options = {} ) {
  27. /**
  28. * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  29. *
  30. * If the {@link #contentLanguage content language} was not specified in the `Locale` constructor,
  31. * it also defines the language of the content.
  32. *
  33. * @readonly
  34. * @member {String}
  35. */
  36. this.uiLanguage = options.uiLanguage || 'en';
  37. /**
  38. * The editor content language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  39. *
  40. * Usually the same as {@link #uiLanguage editor language}, it can be customized by passing an optional
  41. * argument to the `Locale` constructor.
  42. *
  43. * @readonly
  44. * @member {String}
  45. */
  46. this.contentLanguage = options.contentLanguage || this.uiLanguage;
  47. /**
  48. * Text direction of the {@link #uiLanguage editor UI language}. Either `'ltr'` or `'rtl'`.
  49. *
  50. * @readonly
  51. * @member {String}
  52. */
  53. this.uiLanguageDirection = getLanguageDirection( this.uiLanguage );
  54. /**
  55. * Text direction of the {@link #contentLanguage editor content language}.
  56. *
  57. * If the content language was passed directly to the `Locale` constructor, this property represents the
  58. * direction of that language.
  59. *
  60. * If the {@link #contentLanguage editor content language} was derived from the {@link #uiLanguage editor language},
  61. * the content language direction is the same as the {@link #uiLanguageDirection UI language direction}.
  62. *
  63. * The value is either `'ltr'` or `'rtl'`.
  64. *
  65. * @readonly
  66. * @member {String}
  67. */
  68. this.contentLanguageDirection = getLanguageDirection( this.contentLanguage );
  69. /**
  70. * Translates the given message to the {@link #uiLanguage}. This method is also available in
  71. * {@link module:core/editor/editor~Editor#t} and {@link module:ui/view~View#t}.
  72. *
  73. * This method's context is statically bound to the `Locale` instance and **always should be called as a function**:
  74. *
  75. * const t = locale.t;
  76. * t( 'Label' );
  77. *
  78. * The message can be either a string or an object implementing the {@link module:translation-service~Message} interface.
  79. *
  80. * A Message may contain placeholders (`%<index>`) for values that are passed as the second argument.
  81. * `<index>` is the index in the `values` array.
  82. *
  83. * t( 'Created file "%0" in %1ms.', [ fileName, timeTaken ] );
  84. *
  85. * A Message can provide a plural form using the `plural` property and a value - that should be always the first element
  86. * of the `values` array based on which the plural form of the target language should be picked. That property value will
  87. * be used as a default plural translation when the translation for the target language will be missing.
  88. *
  89. * t( { string: 'Add a space', plural: 'Add %0 spaces' }, [ spaces ] );
  90. * t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ spaces, 'Add' ] );
  91. *
  92. * A Message can provide a context using the `context` property when the message string may be not enough unique
  93. * across all messages. When the `context` property is set the message id will be constructed in
  94. * the following way: `${ message.string }_${ message.context }`. This context will be also used
  95. * by translators later as a context for the message that should be translated.
  96. *
  97. * t( { string: 'image', context: 'Add/Remove image' } );
  98. *
  99. * @method #t
  100. * @param {String|module:utils/translation-service~Message} message A message that will be localized.
  101. * @param {Array.<String>} [values] Values that should be used to interpolate the string.
  102. * @returns {String}
  103. */
  104. this.t = ( message, values ) => this._t( message, values );
  105. }
  106. /**
  107. * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  108. *
  109. * **Note**: This property has been deprecated. Please use {@link #uiLanguage} and {@link #contentLanguage}
  110. * properties instead.
  111. *
  112. * @deprecated
  113. * @member {String}
  114. */
  115. get language() {
  116. /**
  117. * The {@link module:utils/locale~Locale#language `Locale#language`} property has been deprecated and will
  118. * be removed in the near future. Please use {@link #uiLanguage} and {@link #contentLanguage} properties instead.
  119. *
  120. * @error locale-deprecated-language-property
  121. */
  122. console.warn(
  123. 'locale-deprecated-language-property: ' +
  124. 'The Locale#language property has been deprecated and will be removed in the near future. ' +
  125. 'Please use #uiLanguage and #contentLanguage properties instead.' );
  126. return this.uiLanguage;
  127. }
  128. /**
  129. * Base for the {@link #t} method.
  130. *
  131. * @param {String|module:utils/translation-service~Message} message
  132. * @param {Array.<String>} [values]
  133. * @returns {String}
  134. * @private
  135. */
  136. _t( message, values = [] ) {
  137. if ( typeof message === 'string' ) {
  138. message = { string: message };
  139. }
  140. const hasPluralForm = !!message.plural;
  141. const amount = hasPluralForm ? values[ 0 ] : 1;
  142. let translatedString = translate( this.uiLanguage, message, amount );
  143. if ( values ) {
  144. translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {
  145. return ( index < values.length ) ? values[ index ] : match;
  146. } );
  147. }
  148. return translatedString;
  149. }
  150. }
  151. // Helps determine whether a language is LTR or RTL.
  152. //
  153. // @param {String} language The ISO 639-1 language code.
  154. // @returns {String} 'ltr' or 'rtl
  155. function getLanguageDirection( languageCode ) {
  156. return RTL_LANGUAGE_CODES.includes( languageCode ) ? 'rtl' : 'ltr';
  157. }