locale.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  6. * @module utils/locale
  7. */
  8. import { translate } from './translation-service';
  9. const RTL_LANGUAGE_CODES = { ar: 1, fa: 1, he: 1, ku: 1, ug: 1 };
  10. /**
  11. * Represents the localization services.
  12. */
  13. export default class Locale {
  14. /**
  15. * Creates a new instance of the Locale class.
  16. *
  17. * @param {String} [language='en'] The editor language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  18. * @param {String} [contentLanguage] The editor content language code in the
  19. * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  20. */
  21. constructor( language, contentLanguage ) {
  22. /**
  23. * The editor language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  24. *
  25. * It controls the language of the editor UI. If the {@link #contentLanguage content language}
  26. * was not specified in the `Locale` constructor, it also defines the language of the content.
  27. *
  28. * @readonly
  29. * @member {String}
  30. */
  31. this.language = language || 'en';
  32. /**
  33. * The editor content language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
  34. *
  35. * Usually the same as {@link #language editor language}, it can be customized by passing an optional
  36. * argument to the `Locale` constructor.
  37. *
  38. * @readonly
  39. * @member {String}
  40. */
  41. this.contentLanguage = contentLanguage || this.language;
  42. /**
  43. * Text direction of the {@link #language editor language}.
  44. *
  45. * @readonly
  46. * @member {String}
  47. */
  48. this.languageDirection = RTL_LANGUAGE_CODES[ this.language ] ? 'rtl' : 'ltr';
  49. /**
  50. * Text direction of the {@link #contentLanguage editor content language}.
  51. *
  52. * @readonly
  53. * @member {String}
  54. */
  55. this.contentLanguageDirection = RTL_LANGUAGE_CODES[ this.contentLanguage ] ? 'rtl' : 'ltr';
  56. /**
  57. * Translates the given string to the {@link #language}. This method is also available in {@link module:core/editor/editor~Editor#t}
  58. * and {@link module:ui/view~View#t}.
  59. *
  60. * The strings may contain placeholders (`%<index>`) for values which are passed as the second argument.
  61. * `<index>` is the index in the `values` array.
  62. *
  63. * editor.t( 'Created file "%0" in %1ms.', [ fileName, timeTaken ] );
  64. *
  65. * This method's context is statically bound to Locale instance,
  66. * so it can be called as a function:
  67. *
  68. * const t = this.t;
  69. * t( 'Label' );
  70. *
  71. * @method #t
  72. * @param {String} str The string to translate.
  73. * @param {String[]} [values] Values that should be used to interpolate the string.
  74. */
  75. this.t = ( ...args ) => this._t( ...args );
  76. }
  77. /**
  78. * Base for the {@link #t} method.
  79. *
  80. * @private
  81. */
  82. _t( str, values ) {
  83. let translatedString = translate( this.language, str );
  84. if ( values ) {
  85. translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {
  86. return ( index < values.length ) ? values[ index ] : match;
  87. } );
  88. }
  89. return translatedString;
  90. }
  91. }