|
@@ -9,27 +9,73 @@
|
|
|
|
|
|
|
|
import { translate } from './translation-service';
|
|
import { translate } from './translation-service';
|
|
|
|
|
|
|
|
|
|
+const RTL_LANGUAGE_CODES = [ 'ar', 'fa', 'he', 'ku', 'ug' ];
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Represents the localization services.
|
|
* Represents the localization services.
|
|
|
*/
|
|
*/
|
|
|
export default class Locale {
|
|
export default class Locale {
|
|
|
/**
|
|
/**
|
|
|
- * Creates a new instance of the Locale class.
|
|
|
|
|
|
|
+ * Creates a new instance of the Locale class. Learn more about
|
|
|
|
|
+ * {@glink features/ui-language configuring language of the editor}.
|
|
|
*
|
|
*
|
|
|
- * @param {String} [language='en'] The language code in [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
|
|
|
|
|
|
|
+ * @param {Object} [options] Locale configuration.
|
|
|
|
|
+ * @param {String} [options.uiLanguage='en'] The editor UI language code in the
|
|
|
|
|
+ * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. See {@link #uiLanguage}.
|
|
|
|
|
+ * @param {String} [options.contentLanguage] The editor content language code in the
|
|
|
|
|
+ * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
|
|
|
|
|
+ * See {@link #contentLanguage}.
|
|
|
*/
|
|
*/
|
|
|
- constructor( language ) {
|
|
|
|
|
|
|
+ constructor( options = {} ) {
|
|
|
|
|
+ /**
|
|
|
|
|
+ * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
|
|
|
|
|
+ *
|
|
|
|
|
+ * If the {@link #contentLanguage content language} was not specified in the `Locale` constructor,
|
|
|
|
|
+ * it also defines the language of the content.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @member {String}
|
|
|
|
|
+ */
|
|
|
|
|
+ this.uiLanguage = options.uiLanguage || 'en';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * The editor content language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Usually the same as {@link #uiLanguage editor language}, it can be customized by passing an optional
|
|
|
|
|
+ * argument to the `Locale` constructor.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @member {String}
|
|
|
|
|
+ */
|
|
|
|
|
+ this.contentLanguage = options.contentLanguage || this.uiLanguage;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * The language code in [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
|
|
|
|
|
|
|
+ * Text direction of the {@link #uiLanguage editor UI language}. Either `'ltr'` or `'rtl'`.
|
|
|
*
|
|
*
|
|
|
* @readonly
|
|
* @readonly
|
|
|
* @member {String}
|
|
* @member {String}
|
|
|
*/
|
|
*/
|
|
|
- this.language = language || 'en';
|
|
|
|
|
|
|
+ this.uiLanguageDirection = getLanguageDirection( this.uiLanguage );
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Translates the given string to the {@link #language}. This method is also available in {@link module:core/editor/editor~Editor#t}
|
|
|
|
|
- * and {@link module:ui/view~View#t}.
|
|
|
|
|
|
|
+ * Text direction of the {@link #contentLanguage editor content language}.
|
|
|
|
|
+ *
|
|
|
|
|
+ * If the content language was passed directly to the `Locale` constructor, this property represents the
|
|
|
|
|
+ * direction of that language.
|
|
|
|
|
+ *
|
|
|
|
|
+ * If the {@link #contentLanguage editor content language} was derived from the {@link #uiLanguage editor language},
|
|
|
|
|
+ * the content language direction is the same as the {@link #uiLanguageDirection UI language direction}.
|
|
|
|
|
+ *
|
|
|
|
|
+ * The value is either `'ltr'` or `'rtl'`.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @readonly
|
|
|
|
|
+ * @member {String}
|
|
|
|
|
+ */
|
|
|
|
|
+ this.contentLanguageDirection = getLanguageDirection( this.contentLanguage );
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Translates the given string to the {@link #uiLanguage}. This method is also available in
|
|
|
|
|
+ * {@link module:core/editor/editor~Editor#t} and {@link module:ui/view~View#t}.
|
|
|
*
|
|
*
|
|
|
* The strings may contain placeholders (`%<index>`) for values which are passed as the second argument.
|
|
* The strings may contain placeholders (`%<index>`) for values which are passed as the second argument.
|
|
|
* `<index>` is the index in the `values` array.
|
|
* `<index>` is the index in the `values` array.
|
|
@@ -55,7 +101,7 @@ export default class Locale {
|
|
|
* @private
|
|
* @private
|
|
|
*/
|
|
*/
|
|
|
_t( str, values ) {
|
|
_t( str, values ) {
|
|
|
- let translatedString = translate( this.language, str );
|
|
|
|
|
|
|
+ let translatedString = translate( this.uiLanguage, str );
|
|
|
|
|
|
|
|
if ( values ) {
|
|
if ( values ) {
|
|
|
translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {
|
|
translatedString = translatedString.replace( /%(\d+)/g, ( match, index ) => {
|
|
@@ -66,3 +112,11 @@ export default class Locale {
|
|
|
return translatedString;
|
|
return translatedString;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// Helps determine whether a language is LTR or RTL.
|
|
|
|
|
+//
|
|
|
|
|
+// @param {String} language The ISO 639-1 language code.
|
|
|
|
|
+// @returns {String} 'ltr' or 'rtl
|
|
|
|
|
+function getLanguageDirection( languageCode ) {
|
|
|
|
|
+ return RTL_LANGUAGE_CODES.includes( languageCode ) ? 'rtl' : 'ltr';
|
|
|
|
|
+}
|