@ckeditor/ckeditor5-utils 40.0.0 → 40.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -26
- package/LICENSE.md +3 -3
- package/package.json +1 -1
- package/src/abortabledebounce.d.ts +17 -0
- package/src/abortabledebounce.js +22 -0
- package/src/areconnectedthroughproperties.d.ts +11 -11
- package/src/areconnectedthroughproperties.js +73 -73
- package/src/ckeditorerror.d.ts +123 -123
- package/src/ckeditorerror.js +176 -176
- package/src/collection.d.ts +433 -427
- package/src/collection.js +583 -575
- package/src/comparearrays.d.ts +30 -30
- package/src/comparearrays.js +47 -47
- package/src/config.d.ts +163 -163
- package/src/config.js +163 -162
- package/src/count.d.ts +18 -18
- package/src/count.js +24 -24
- package/src/delay.d.ts +19 -19
- package/src/delay.js +26 -26
- package/src/diff.d.ts +31 -31
- package/src/diff.js +115 -115
- package/src/difftochanges.d.ts +59 -59
- package/src/difftochanges.js +79 -79
- package/src/dom/createelement.d.ts +57 -57
- package/src/dom/createelement.js +40 -40
- package/src/dom/emittermixin.d.ts +142 -142
- package/src/dom/emittermixin.js +239 -239
- package/src/dom/findclosestscrollableancestor.d.ts +11 -11
- package/src/dom/findclosestscrollableancestor.js +31 -31
- package/src/dom/getancestors.d.ts +17 -17
- package/src/dom/getancestors.js +27 -27
- package/src/dom/getborderwidths.d.ts +24 -24
- package/src/dom/getborderwidths.js +24 -24
- package/src/dom/getcommonancestor.d.ts +12 -12
- package/src/dom/getcommonancestor.js +25 -25
- package/src/dom/getdatafromelement.d.ts +14 -14
- package/src/dom/getdatafromelement.js +20 -20
- package/src/dom/getpositionedancestor.d.ts +10 -10
- package/src/dom/getpositionedancestor.js +22 -22
- package/src/dom/global.d.ts +32 -32
- package/src/dom/global.js +35 -35
- package/src/dom/indexof.d.ts +14 -14
- package/src/dom/indexof.js +21 -21
- package/src/dom/insertat.d.ts +15 -15
- package/src/dom/insertat.js +17 -17
- package/src/dom/iscomment.d.ts +11 -11
- package/src/dom/iscomment.js +14 -14
- package/src/dom/isnode.d.ts +11 -11
- package/src/dom/isnode.js +21 -21
- package/src/dom/isrange.d.ts +11 -11
- package/src/dom/isrange.js +13 -13
- package/src/dom/istext.d.ts +11 -11
- package/src/dom/istext.js +13 -13
- package/src/dom/isvalidattributename.d.ts +10 -10
- package/src/dom/isvalidattributename.js +22 -22
- package/src/dom/isvisible.d.ts +18 -18
- package/src/dom/isvisible.js +20 -20
- package/src/dom/iswindow.d.ts +11 -11
- package/src/dom/iswindow.js +22 -22
- package/src/dom/position.d.ts +211 -211
- package/src/dom/position.js +313 -313
- package/src/dom/rect.d.ts +195 -195
- package/src/dom/rect.js +474 -474
- package/src/dom/remove.d.ts +13 -13
- package/src/dom/remove.js +18 -18
- package/src/dom/resizeobserver.d.ts +74 -74
- package/src/dom/resizeobserver.js +126 -126
- package/src/dom/scroll.d.ts +73 -73
- package/src/dom/scroll.js +383 -383
- package/src/dom/setdatainelement.d.ts +14 -14
- package/src/dom/setdatainelement.js +20 -20
- package/src/dom/tounit.d.ts +22 -22
- package/src/dom/tounit.js +16 -16
- package/src/elementreplacer.d.ts +31 -31
- package/src/elementreplacer.js +43 -43
- package/src/emittermixin.d.ts +312 -312
- package/src/emittermixin.js +453 -453
- package/src/env.d.ts +117 -117
- package/src/env.js +122 -122
- package/src/eventinfo.d.ts +58 -58
- package/src/eventinfo.js +26 -26
- package/src/fastdiff.d.ts +112 -112
- package/src/fastdiff.js +248 -248
- package/src/first.d.ts +11 -11
- package/src/first.js +17 -17
- package/src/focustracker.d.ts +75 -75
- package/src/focustracker.js +95 -95
- package/src/index.d.ts +64 -61
- package/src/index.js +63 -60
- package/src/inserttopriorityarray.d.ts +30 -30
- package/src/inserttopriorityarray.js +21 -21
- package/src/isiterable.d.ts +14 -14
- package/src/isiterable.js +16 -16
- package/src/keyboard.d.ts +126 -126
- package/src/keyboard.js +221 -221
- package/src/keystrokehandler.d.ts +87 -87
- package/src/keystrokehandler.js +122 -122
- package/src/language.d.ts +17 -17
- package/src/language.js +19 -19
- package/src/locale.d.ts +120 -120
- package/src/locale.js +76 -76
- package/src/mapsequal.d.ts +15 -15
- package/src/mapsequal.js +27 -27
- package/src/mix.d.ts +85 -85
- package/src/mix.js +50 -50
- package/src/nth.d.ts +16 -16
- package/src/nth.js +24 -24
- package/src/objecttomap.d.ts +23 -23
- package/src/objecttomap.js +27 -27
- package/src/observablemixin.d.ts +560 -560
- package/src/observablemixin.js +580 -580
- package/src/priorities.d.ts +33 -33
- package/src/priorities.js +23 -23
- package/src/retry.d.ts +33 -0
- package/src/retry.js +47 -0
- package/src/splicearray.d.ts +26 -26
- package/src/splicearray.js +40 -40
- package/src/spy.d.ts +21 -21
- package/src/spy.js +22 -22
- package/src/toarray.d.ts +25 -25
- package/src/toarray.js +7 -7
- package/src/tomap.d.ts +19 -19
- package/src/tomap.js +29 -29
- package/src/translation-service.d.ts +168 -168
- package/src/translation-service.js +198 -198
- package/src/uid.d.ts +15 -15
- package/src/uid.js +57 -57
- package/src/unicode.d.ts +54 -54
- package/src/unicode.js +85 -85
- package/src/verifylicense.d.ts +15 -15
- package/src/verifylicense.js +87 -87
- package/src/version.d.ts +10 -10
- package/src/version.js +153 -153
- package/src/wait.d.ts +16 -0
- package/src/wait.js +29 -0
|
@@ -1,198 +1,198 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/* eslint-disable no-var */
|
|
6
|
-
/**
|
|
7
|
-
* @module utils/translation-service
|
|
8
|
-
*/
|
|
9
|
-
import CKEditorError from './ckeditorerror';
|
|
10
|
-
import global from './dom/global';
|
|
11
|
-
/* istanbul ignore else -- @preserve */
|
|
12
|
-
if (!global.window.CKEDITOR_TRANSLATIONS) {
|
|
13
|
-
global.window.CKEDITOR_TRANSLATIONS = {};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Adds translations to existing ones or overrides the existing translations. These translations will later
|
|
17
|
-
* be available for the {@link module:utils/locale~Locale#t `t()`} function.
|
|
18
|
-
*
|
|
19
|
-
* The `translations` is an object which consists of `messageId: translation` pairs. Note that the message ID can be
|
|
20
|
-
* either constructed from the message string or from the message ID if it was passed
|
|
21
|
-
* (this happens rarely and mostly for short messages or messages with placeholders).
|
|
22
|
-
* Since the editor displays only the message string, the message ID can be found either in the source code or in the
|
|
23
|
-
* built translations for another language.
|
|
24
|
-
*
|
|
25
|
-
* ```ts
|
|
26
|
-
* add( 'pl', {
|
|
27
|
-
* 'Cancel': 'Anuluj',
|
|
28
|
-
* 'IMAGE': 'obraz', // Note that the `IMAGE` comes from the message ID, while the string can be `image`.
|
|
29
|
-
* } );
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* If the message is supposed to support various plural forms, make sure to provide an array with the singular form and all plural forms:
|
|
33
|
-
*
|
|
34
|
-
* ```ts
|
|
35
|
-
* add( 'pl', {
|
|
36
|
-
* 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
|
|
37
|
-
* } );
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* You should also specify the third argument (the `getPluralForm()` function) that will be used to determine the plural form if no
|
|
41
|
-
* language file was loaded for that language. All language files coming from CKEditor 5 sources will have this option set, so
|
|
42
|
-
* these plural form rules will be reused by other translations added to the registered languages. The `getPluralForm()` function
|
|
43
|
-
* can return either a Boolean or a number.
|
|
44
|
-
*
|
|
45
|
-
* ```ts
|
|
46
|
-
* add( 'en', {
|
|
47
|
-
* // ... Translations.
|
|
48
|
-
* }, n => n !== 1 );
|
|
49
|
-
* add( 'pl', {
|
|
50
|
-
* // ... Translations.
|
|
51
|
-
* }, n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* All translations extend the global `window.CKEDITOR_TRANSLATIONS` object. An example of this object can be found below:
|
|
55
|
-
*
|
|
56
|
-
* ```ts
|
|
57
|
-
* {
|
|
58
|
-
* pl: {
|
|
59
|
-
* dictionary: {
|
|
60
|
-
* 'Cancel': 'Anuluj',
|
|
61
|
-
* 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
|
|
62
|
-
* },
|
|
63
|
-
* // A function that returns the plural form index.
|
|
64
|
-
* getPluralForm: n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
|
|
65
|
-
* }
|
|
66
|
-
* // Other languages.
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* If you cannot import this function from this module (e.g. because you use a CKEditor 5 build), you can
|
|
71
|
-
* still add translations by extending the global `window.CKEDITOR_TRANSLATIONS` object by using a function like
|
|
72
|
-
* the one below:
|
|
73
|
-
*
|
|
74
|
-
* ```ts
|
|
75
|
-
* function addTranslations( language, translations, getPluralForm ) {
|
|
76
|
-
* if ( !global.window.CKEDITOR_TRANSLATIONS ) {
|
|
77
|
-
* global.window.CKEDITOR_TRANSLATIONS = {};
|
|
78
|
-
* }
|
|
79
|
-
|
|
80
|
-
* if ( !global.window.CKEDITOR_TRANSLATIONS[ language ] ) {
|
|
81
|
-
* global.window.CKEDITOR_TRANSLATIONS[ language ] = {};
|
|
82
|
-
* }
|
|
83
|
-
*
|
|
84
|
-
* const languageTranslations = global.window.CKEDITOR_TRANSLATIONS[ language ];
|
|
85
|
-
*
|
|
86
|
-
* languageTranslations.dictionary = languageTranslations.dictionary || {};
|
|
87
|
-
* languageTranslations.getPluralForm = getPluralForm || languageTranslations.getPluralForm;
|
|
88
|
-
*
|
|
89
|
-
* // Extend the dictionary for the given language.
|
|
90
|
-
* Object.assign( languageTranslations.dictionary, translations );
|
|
91
|
-
* }
|
|
92
|
-
* ```
|
|
93
|
-
*
|
|
94
|
-
* @param language Target language.
|
|
95
|
-
* @param translations An object with translations which will be added to the dictionary.
|
|
96
|
-
* For each message ID the value should be either a translation or an array of translations if the message
|
|
97
|
-
* should support plural forms.
|
|
98
|
-
* @param getPluralForm A function that returns the plural form index (a number).
|
|
99
|
-
*/
|
|
100
|
-
export function add(language, translations, getPluralForm) {
|
|
101
|
-
if (!global.window.CKEDITOR_TRANSLATIONS[language]) {
|
|
102
|
-
global.window.CKEDITOR_TRANSLATIONS[language] = {};
|
|
103
|
-
}
|
|
104
|
-
const languageTranslations = global.window.CKEDITOR_TRANSLATIONS[language];
|
|
105
|
-
languageTranslations.dictionary = languageTranslations.dictionary || {};
|
|
106
|
-
languageTranslations.getPluralForm = getPluralForm || languageTranslations.getPluralForm;
|
|
107
|
-
Object.assign(languageTranslations.dictionary, translations);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* **Note:** This method is internal, use {@link module:utils/locale~Locale#t the `t()` function} instead to translate
|
|
111
|
-
* the editor UI parts.
|
|
112
|
-
*
|
|
113
|
-
* This function is responsible for translating messages to the specified language. It uses translations added perviously
|
|
114
|
-
* by {@link module:utils/translation-service~add} (a translations dictionary and the `getPluralForm()` function
|
|
115
|
-
* to provide accurate translations of plural forms).
|
|
116
|
-
*
|
|
117
|
-
* When no translation is defined in the dictionary or the dictionary does not exist, this function returns
|
|
118
|
-
* the original message string or the message plural depending on the number of elements.
|
|
119
|
-
*
|
|
120
|
-
* ```ts
|
|
121
|
-
* translate( 'pl', { string: 'Cancel' } ); // 'Cancel'
|
|
122
|
-
* ```
|
|
123
|
-
*
|
|
124
|
-
* The third optional argument is the number of elements, based on which the single form or one of the plural forms
|
|
125
|
-
* should be picked when the message is supposed to support various plural forms.
|
|
126
|
-
*
|
|
127
|
-
* ```ts
|
|
128
|
-
* translate( 'en', { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Add a space'
|
|
129
|
-
* translate( 'en', { string: 'Add a space', plural: 'Add %0 spaces' }, 3 ); // 'Add %0 spaces'
|
|
130
|
-
* ```
|
|
131
|
-
*
|
|
132
|
-
* The message should provide an ID using the `id` property when the message strings are not unique and their
|
|
133
|
-
* translations should be different.
|
|
134
|
-
*
|
|
135
|
-
* ```ts
|
|
136
|
-
* translate( 'en', { string: 'image', id: 'ADD_IMAGE' } );
|
|
137
|
-
* translate( 'en', { string: 'image', id: 'AN_IMAGE' } );
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* @internal
|
|
141
|
-
* @param language Target language.
|
|
142
|
-
* @param message A message that will be translated.
|
|
143
|
-
* @param quantity The number of elements for which a plural form should be picked from the target language dictionary.
|
|
144
|
-
* @returns Translated sentence.
|
|
145
|
-
*/
|
|
146
|
-
export function _translate(language, message, quantity = 1) {
|
|
147
|
-
if (typeof quantity !== 'number') {
|
|
148
|
-
/**
|
|
149
|
-
* An incorrect value was passed to the translation function. This was probably caused
|
|
150
|
-
* by an incorrect message interpolation of a plural form. Note that for messages supporting plural forms
|
|
151
|
-
* the second argument of the `t()` function should always be a number or an array with a number as the first element.
|
|
152
|
-
*
|
|
153
|
-
* @error translation-service-quantity-not-a-number
|
|
154
|
-
*/
|
|
155
|
-
throw new CKEditorError('translation-service-quantity-not-a-number', null, { quantity });
|
|
156
|
-
}
|
|
157
|
-
const numberOfLanguages = getNumberOfLanguages();
|
|
158
|
-
if (numberOfLanguages === 1) {
|
|
159
|
-
// Override the language to the only supported one.
|
|
160
|
-
// This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization.
|
|
161
|
-
language = Object.keys(global.window.CKEDITOR_TRANSLATIONS)[0];
|
|
162
|
-
}
|
|
163
|
-
const messageId = message.id || message.string;
|
|
164
|
-
if (numberOfLanguages === 0 || !hasTranslation(language, messageId)) {
|
|
165
|
-
if (quantity !== 1) {
|
|
166
|
-
// Return the default plural form that was passed in the `message.plural` parameter.
|
|
167
|
-
return message.plural;
|
|
168
|
-
}
|
|
169
|
-
return message.string;
|
|
170
|
-
}
|
|
171
|
-
const dictionary = global.window.CKEDITOR_TRANSLATIONS[language].dictionary;
|
|
172
|
-
const getPluralForm = global.window.CKEDITOR_TRANSLATIONS[language].getPluralForm || (n => n === 1 ? 0 : 1);
|
|
173
|
-
const translation = dictionary[messageId];
|
|
174
|
-
if (typeof translation === 'string') {
|
|
175
|
-
return translation;
|
|
176
|
-
}
|
|
177
|
-
const pluralFormIndex = Number(getPluralForm(quantity));
|
|
178
|
-
// Note: The `translate` function is not responsible for replacing `%0, %1, ...` with values.
|
|
179
|
-
return translation[pluralFormIndex];
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Clears dictionaries for test purposes.
|
|
183
|
-
*
|
|
184
|
-
* @internal
|
|
185
|
-
*/
|
|
186
|
-
export function _clear() {
|
|
187
|
-
global.window.CKEDITOR_TRANSLATIONS = {};
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Checks whether the dictionary exists and translation in that dictionary exists.
|
|
191
|
-
*/
|
|
192
|
-
function hasTranslation(language, messageId) {
|
|
193
|
-
return (!!global.window.CKEDITOR_TRANSLATIONS[language] &&
|
|
194
|
-
!!global.window.CKEDITOR_TRANSLATIONS[language].dictionary[messageId]);
|
|
195
|
-
}
|
|
196
|
-
function getNumberOfLanguages() {
|
|
197
|
-
return Object.keys(global.window.CKEDITOR_TRANSLATIONS).length;
|
|
198
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/* eslint-disable no-var */
|
|
6
|
+
/**
|
|
7
|
+
* @module utils/translation-service
|
|
8
|
+
*/
|
|
9
|
+
import CKEditorError from './ckeditorerror';
|
|
10
|
+
import global from './dom/global';
|
|
11
|
+
/* istanbul ignore else -- @preserve */
|
|
12
|
+
if (!global.window.CKEDITOR_TRANSLATIONS) {
|
|
13
|
+
global.window.CKEDITOR_TRANSLATIONS = {};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Adds translations to existing ones or overrides the existing translations. These translations will later
|
|
17
|
+
* be available for the {@link module:utils/locale~Locale#t `t()`} function.
|
|
18
|
+
*
|
|
19
|
+
* The `translations` is an object which consists of `messageId: translation` pairs. Note that the message ID can be
|
|
20
|
+
* either constructed from the message string or from the message ID if it was passed
|
|
21
|
+
* (this happens rarely and mostly for short messages or messages with placeholders).
|
|
22
|
+
* Since the editor displays only the message string, the message ID can be found either in the source code or in the
|
|
23
|
+
* built translations for another language.
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* add( 'pl', {
|
|
27
|
+
* 'Cancel': 'Anuluj',
|
|
28
|
+
* 'IMAGE': 'obraz', // Note that the `IMAGE` comes from the message ID, while the string can be `image`.
|
|
29
|
+
* } );
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* If the message is supposed to support various plural forms, make sure to provide an array with the singular form and all plural forms:
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* add( 'pl', {
|
|
36
|
+
* 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
|
|
37
|
+
* } );
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* You should also specify the third argument (the `getPluralForm()` function) that will be used to determine the plural form if no
|
|
41
|
+
* language file was loaded for that language. All language files coming from CKEditor 5 sources will have this option set, so
|
|
42
|
+
* these plural form rules will be reused by other translations added to the registered languages. The `getPluralForm()` function
|
|
43
|
+
* can return either a Boolean or a number.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* add( 'en', {
|
|
47
|
+
* // ... Translations.
|
|
48
|
+
* }, n => n !== 1 );
|
|
49
|
+
* add( 'pl', {
|
|
50
|
+
* // ... Translations.
|
|
51
|
+
* }, n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* All translations extend the global `window.CKEDITOR_TRANSLATIONS` object. An example of this object can be found below:
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* {
|
|
58
|
+
* pl: {
|
|
59
|
+
* dictionary: {
|
|
60
|
+
* 'Cancel': 'Anuluj',
|
|
61
|
+
* 'Add space': [ 'Dodaj spację', 'Dodaj %0 spacje', 'Dodaj %0 spacji' ]
|
|
62
|
+
* },
|
|
63
|
+
* // A function that returns the plural form index.
|
|
64
|
+
* getPluralForm: n => n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && ( n % 100 < 10 || n % 100 >= 20 ) ? 1 : 2 );
|
|
65
|
+
* }
|
|
66
|
+
* // Other languages.
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* If you cannot import this function from this module (e.g. because you use a CKEditor 5 build), you can
|
|
71
|
+
* still add translations by extending the global `window.CKEDITOR_TRANSLATIONS` object by using a function like
|
|
72
|
+
* the one below:
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* function addTranslations( language, translations, getPluralForm ) {
|
|
76
|
+
* if ( !global.window.CKEDITOR_TRANSLATIONS ) {
|
|
77
|
+
* global.window.CKEDITOR_TRANSLATIONS = {};
|
|
78
|
+
* }
|
|
79
|
+
|
|
80
|
+
* if ( !global.window.CKEDITOR_TRANSLATIONS[ language ] ) {
|
|
81
|
+
* global.window.CKEDITOR_TRANSLATIONS[ language ] = {};
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* const languageTranslations = global.window.CKEDITOR_TRANSLATIONS[ language ];
|
|
85
|
+
*
|
|
86
|
+
* languageTranslations.dictionary = languageTranslations.dictionary || {};
|
|
87
|
+
* languageTranslations.getPluralForm = getPluralForm || languageTranslations.getPluralForm;
|
|
88
|
+
*
|
|
89
|
+
* // Extend the dictionary for the given language.
|
|
90
|
+
* Object.assign( languageTranslations.dictionary, translations );
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @param language Target language.
|
|
95
|
+
* @param translations An object with translations which will be added to the dictionary.
|
|
96
|
+
* For each message ID the value should be either a translation or an array of translations if the message
|
|
97
|
+
* should support plural forms.
|
|
98
|
+
* @param getPluralForm A function that returns the plural form index (a number).
|
|
99
|
+
*/
|
|
100
|
+
export function add(language, translations, getPluralForm) {
|
|
101
|
+
if (!global.window.CKEDITOR_TRANSLATIONS[language]) {
|
|
102
|
+
global.window.CKEDITOR_TRANSLATIONS[language] = {};
|
|
103
|
+
}
|
|
104
|
+
const languageTranslations = global.window.CKEDITOR_TRANSLATIONS[language];
|
|
105
|
+
languageTranslations.dictionary = languageTranslations.dictionary || {};
|
|
106
|
+
languageTranslations.getPluralForm = getPluralForm || languageTranslations.getPluralForm;
|
|
107
|
+
Object.assign(languageTranslations.dictionary, translations);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* **Note:** This method is internal, use {@link module:utils/locale~Locale#t the `t()` function} instead to translate
|
|
111
|
+
* the editor UI parts.
|
|
112
|
+
*
|
|
113
|
+
* This function is responsible for translating messages to the specified language. It uses translations added perviously
|
|
114
|
+
* by {@link module:utils/translation-service~add} (a translations dictionary and the `getPluralForm()` function
|
|
115
|
+
* to provide accurate translations of plural forms).
|
|
116
|
+
*
|
|
117
|
+
* When no translation is defined in the dictionary or the dictionary does not exist, this function returns
|
|
118
|
+
* the original message string or the message plural depending on the number of elements.
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* translate( 'pl', { string: 'Cancel' } ); // 'Cancel'
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* The third optional argument is the number of elements, based on which the single form or one of the plural forms
|
|
125
|
+
* should be picked when the message is supposed to support various plural forms.
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* translate( 'en', { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Add a space'
|
|
129
|
+
* translate( 'en', { string: 'Add a space', plural: 'Add %0 spaces' }, 3 ); // 'Add %0 spaces'
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* The message should provide an ID using the `id` property when the message strings are not unique and their
|
|
133
|
+
* translations should be different.
|
|
134
|
+
*
|
|
135
|
+
* ```ts
|
|
136
|
+
* translate( 'en', { string: 'image', id: 'ADD_IMAGE' } );
|
|
137
|
+
* translate( 'en', { string: 'image', id: 'AN_IMAGE' } );
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @internal
|
|
141
|
+
* @param language Target language.
|
|
142
|
+
* @param message A message that will be translated.
|
|
143
|
+
* @param quantity The number of elements for which a plural form should be picked from the target language dictionary.
|
|
144
|
+
* @returns Translated sentence.
|
|
145
|
+
*/
|
|
146
|
+
export function _translate(language, message, quantity = 1) {
|
|
147
|
+
if (typeof quantity !== 'number') {
|
|
148
|
+
/**
|
|
149
|
+
* An incorrect value was passed to the translation function. This was probably caused
|
|
150
|
+
* by an incorrect message interpolation of a plural form. Note that for messages supporting plural forms
|
|
151
|
+
* the second argument of the `t()` function should always be a number or an array with a number as the first element.
|
|
152
|
+
*
|
|
153
|
+
* @error translation-service-quantity-not-a-number
|
|
154
|
+
*/
|
|
155
|
+
throw new CKEditorError('translation-service-quantity-not-a-number', null, { quantity });
|
|
156
|
+
}
|
|
157
|
+
const numberOfLanguages = getNumberOfLanguages();
|
|
158
|
+
if (numberOfLanguages === 1) {
|
|
159
|
+
// Override the language to the only supported one.
|
|
160
|
+
// This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization.
|
|
161
|
+
language = Object.keys(global.window.CKEDITOR_TRANSLATIONS)[0];
|
|
162
|
+
}
|
|
163
|
+
const messageId = message.id || message.string;
|
|
164
|
+
if (numberOfLanguages === 0 || !hasTranslation(language, messageId)) {
|
|
165
|
+
if (quantity !== 1) {
|
|
166
|
+
// Return the default plural form that was passed in the `message.plural` parameter.
|
|
167
|
+
return message.plural;
|
|
168
|
+
}
|
|
169
|
+
return message.string;
|
|
170
|
+
}
|
|
171
|
+
const dictionary = global.window.CKEDITOR_TRANSLATIONS[language].dictionary;
|
|
172
|
+
const getPluralForm = global.window.CKEDITOR_TRANSLATIONS[language].getPluralForm || (n => n === 1 ? 0 : 1);
|
|
173
|
+
const translation = dictionary[messageId];
|
|
174
|
+
if (typeof translation === 'string') {
|
|
175
|
+
return translation;
|
|
176
|
+
}
|
|
177
|
+
const pluralFormIndex = Number(getPluralForm(quantity));
|
|
178
|
+
// Note: The `translate` function is not responsible for replacing `%0, %1, ...` with values.
|
|
179
|
+
return translation[pluralFormIndex];
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Clears dictionaries for test purposes.
|
|
183
|
+
*
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
186
|
+
export function _clear() {
|
|
187
|
+
global.window.CKEDITOR_TRANSLATIONS = {};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Checks whether the dictionary exists and translation in that dictionary exists.
|
|
191
|
+
*/
|
|
192
|
+
function hasTranslation(language, messageId) {
|
|
193
|
+
return (!!global.window.CKEDITOR_TRANSLATIONS[language] &&
|
|
194
|
+
!!global.window.CKEDITOR_TRANSLATIONS[language].dictionary[messageId]);
|
|
195
|
+
}
|
|
196
|
+
function getNumberOfLanguages() {
|
|
197
|
+
return Object.keys(global.window.CKEDITOR_TRANSLATIONS).length;
|
|
198
|
+
}
|
package/src/uid.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Returns a unique id. The id starts with an "e" character and a randomly generated string of
|
|
7
|
-
* 32 alphanumeric characters.
|
|
8
|
-
*
|
|
9
|
-
* **Note**: The characters the unique id is built from correspond to the hex number notation
|
|
10
|
-
* (from "0" to "9", from "a" to "f"). In other words, each id corresponds to an "e" followed
|
|
11
|
-
* by 16 8-bit numbers next to each other.
|
|
12
|
-
*
|
|
13
|
-
* @returns An unique id string.
|
|
14
|
-
*/
|
|
15
|
-
export default function uid(): string;
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Returns a unique id. The id starts with an "e" character and a randomly generated string of
|
|
7
|
+
* 32 alphanumeric characters.
|
|
8
|
+
*
|
|
9
|
+
* **Note**: The characters the unique id is built from correspond to the hex number notation
|
|
10
|
+
* (from "0" to "9", from "a" to "f"). In other words, each id corresponds to an "e" followed
|
|
11
|
+
* by 16 8-bit numbers next to each other.
|
|
12
|
+
*
|
|
13
|
+
* @returns An unique id string.
|
|
14
|
+
*/
|
|
15
|
+
export default function uid(): string;
|
package/src/uid.js
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module utils/uid
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* A hash table of hex numbers to avoid using toString() in uid() which is costly.
|
|
10
|
-
* [ '00', '01', '02', ..., 'fe', 'ff' ]
|
|
11
|
-
*/
|
|
12
|
-
const HEX_NUMBERS = new Array(256).fill('')
|
|
13
|
-
.map((_, index) => ('0' + (index).toString(16)).slice(-2));
|
|
14
|
-
/**
|
|
15
|
-
* Returns a unique id. The id starts with an "e" character and a randomly generated string of
|
|
16
|
-
* 32 alphanumeric characters.
|
|
17
|
-
*
|
|
18
|
-
* **Note**: The characters the unique id is built from correspond to the hex number notation
|
|
19
|
-
* (from "0" to "9", from "a" to "f"). In other words, each id corresponds to an "e" followed
|
|
20
|
-
* by 16 8-bit numbers next to each other.
|
|
21
|
-
*
|
|
22
|
-
* @returns An unique id string.
|
|
23
|
-
*/
|
|
24
|
-
export default function uid() {
|
|
25
|
-
// Let's create some positive random 32bit integers first.
|
|
26
|
-
//
|
|
27
|
-
// 1. Math.random() is a float between 0 and 1.
|
|
28
|
-
// 2. 0x100000000 is 2^32 = 4294967296.
|
|
29
|
-
// 3. >>> 0 enforces integer (in JS all numbers are floating point).
|
|
30
|
-
//
|
|
31
|
-
// For instance:
|
|
32
|
-
// Math.random() * 0x100000000 = 3366450031.853859
|
|
33
|
-
// but
|
|
34
|
-
// Math.random() * 0x100000000 >>> 0 = 3366450031.
|
|
35
|
-
const r1 = Math.random() * 0x100000000 >>> 0;
|
|
36
|
-
const r2 = Math.random() * 0x100000000 >>> 0;
|
|
37
|
-
const r3 = Math.random() * 0x100000000 >>> 0;
|
|
38
|
-
const r4 = Math.random() * 0x100000000 >>> 0;
|
|
39
|
-
// Make sure that id does not start with number.
|
|
40
|
-
return 'e' +
|
|
41
|
-
HEX_NUMBERS[r1 >> 0 & 0xFF] +
|
|
42
|
-
HEX_NUMBERS[r1 >> 8 & 0xFF] +
|
|
43
|
-
HEX_NUMBERS[r1 >> 16 & 0xFF] +
|
|
44
|
-
HEX_NUMBERS[r1 >> 24 & 0xFF] +
|
|
45
|
-
HEX_NUMBERS[r2 >> 0 & 0xFF] +
|
|
46
|
-
HEX_NUMBERS[r2 >> 8 & 0xFF] +
|
|
47
|
-
HEX_NUMBERS[r2 >> 16 & 0xFF] +
|
|
48
|
-
HEX_NUMBERS[r2 >> 24 & 0xFF] +
|
|
49
|
-
HEX_NUMBERS[r3 >> 0 & 0xFF] +
|
|
50
|
-
HEX_NUMBERS[r3 >> 8 & 0xFF] +
|
|
51
|
-
HEX_NUMBERS[r3 >> 16 & 0xFF] +
|
|
52
|
-
HEX_NUMBERS[r3 >> 24 & 0xFF] +
|
|
53
|
-
HEX_NUMBERS[r4 >> 0 & 0xFF] +
|
|
54
|
-
HEX_NUMBERS[r4 >> 8 & 0xFF] +
|
|
55
|
-
HEX_NUMBERS[r4 >> 16 & 0xFF] +
|
|
56
|
-
HEX_NUMBERS[r4 >> 24 & 0xFF];
|
|
57
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module utils/uid
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* A hash table of hex numbers to avoid using toString() in uid() which is costly.
|
|
10
|
+
* [ '00', '01', '02', ..., 'fe', 'ff' ]
|
|
11
|
+
*/
|
|
12
|
+
const HEX_NUMBERS = new Array(256).fill('')
|
|
13
|
+
.map((_, index) => ('0' + (index).toString(16)).slice(-2));
|
|
14
|
+
/**
|
|
15
|
+
* Returns a unique id. The id starts with an "e" character and a randomly generated string of
|
|
16
|
+
* 32 alphanumeric characters.
|
|
17
|
+
*
|
|
18
|
+
* **Note**: The characters the unique id is built from correspond to the hex number notation
|
|
19
|
+
* (from "0" to "9", from "a" to "f"). In other words, each id corresponds to an "e" followed
|
|
20
|
+
* by 16 8-bit numbers next to each other.
|
|
21
|
+
*
|
|
22
|
+
* @returns An unique id string.
|
|
23
|
+
*/
|
|
24
|
+
export default function uid() {
|
|
25
|
+
// Let's create some positive random 32bit integers first.
|
|
26
|
+
//
|
|
27
|
+
// 1. Math.random() is a float between 0 and 1.
|
|
28
|
+
// 2. 0x100000000 is 2^32 = 4294967296.
|
|
29
|
+
// 3. >>> 0 enforces integer (in JS all numbers are floating point).
|
|
30
|
+
//
|
|
31
|
+
// For instance:
|
|
32
|
+
// Math.random() * 0x100000000 = 3366450031.853859
|
|
33
|
+
// but
|
|
34
|
+
// Math.random() * 0x100000000 >>> 0 = 3366450031.
|
|
35
|
+
const r1 = Math.random() * 0x100000000 >>> 0;
|
|
36
|
+
const r2 = Math.random() * 0x100000000 >>> 0;
|
|
37
|
+
const r3 = Math.random() * 0x100000000 >>> 0;
|
|
38
|
+
const r4 = Math.random() * 0x100000000 >>> 0;
|
|
39
|
+
// Make sure that id does not start with number.
|
|
40
|
+
return 'e' +
|
|
41
|
+
HEX_NUMBERS[r1 >> 0 & 0xFF] +
|
|
42
|
+
HEX_NUMBERS[r1 >> 8 & 0xFF] +
|
|
43
|
+
HEX_NUMBERS[r1 >> 16 & 0xFF] +
|
|
44
|
+
HEX_NUMBERS[r1 >> 24 & 0xFF] +
|
|
45
|
+
HEX_NUMBERS[r2 >> 0 & 0xFF] +
|
|
46
|
+
HEX_NUMBERS[r2 >> 8 & 0xFF] +
|
|
47
|
+
HEX_NUMBERS[r2 >> 16 & 0xFF] +
|
|
48
|
+
HEX_NUMBERS[r2 >> 24 & 0xFF] +
|
|
49
|
+
HEX_NUMBERS[r3 >> 0 & 0xFF] +
|
|
50
|
+
HEX_NUMBERS[r3 >> 8 & 0xFF] +
|
|
51
|
+
HEX_NUMBERS[r3 >> 16 & 0xFF] +
|
|
52
|
+
HEX_NUMBERS[r3 >> 24 & 0xFF] +
|
|
53
|
+
HEX_NUMBERS[r4 >> 0 & 0xFF] +
|
|
54
|
+
HEX_NUMBERS[r4 >> 8 & 0xFF] +
|
|
55
|
+
HEX_NUMBERS[r4 >> 16 & 0xFF] +
|
|
56
|
+
HEX_NUMBERS[r4 >> 24 & 0xFF];
|
|
57
|
+
}
|
package/src/unicode.d.ts
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Set of utils to handle unicode characters.
|
|
7
|
-
*
|
|
8
|
-
* @module utils/unicode
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Checks whether given `character` is a combining mark.
|
|
12
|
-
*
|
|
13
|
-
* @param character Character to check.
|
|
14
|
-
*/
|
|
15
|
-
export declare function isCombiningMark(character: string): boolean;
|
|
16
|
-
/**
|
|
17
|
-
* Checks whether given `character` is a high half of surrogate pair.
|
|
18
|
-
*
|
|
19
|
-
* Using UTF-16 terminology, a surrogate pair denotes UTF-16 character using two UTF-8 characters. The surrogate pair
|
|
20
|
-
* consist of high surrogate pair character followed by low surrogate pair character.
|
|
21
|
-
*
|
|
22
|
-
* @param character Character to check.
|
|
23
|
-
*/
|
|
24
|
-
export declare function isHighSurrogateHalf(character: string): boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Checks whether given `character` is a low half of surrogate pair.
|
|
27
|
-
*
|
|
28
|
-
* Using UTF-16 terminology, a surrogate pair denotes UTF-16 character using two UTF-8 characters. The surrogate pair
|
|
29
|
-
* consist of high surrogate pair character followed by low surrogate pair character.
|
|
30
|
-
*
|
|
31
|
-
* @param character Character to check.
|
|
32
|
-
*/
|
|
33
|
-
export declare function isLowSurrogateHalf(character: string): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Checks whether given offset in a string is inside a surrogate pair (between two surrogate halves).
|
|
36
|
-
*
|
|
37
|
-
* @param string String to check.
|
|
38
|
-
* @param offset Offset to check.
|
|
39
|
-
*/
|
|
40
|
-
export declare function isInsideSurrogatePair(string: string, offset: number): boolean;
|
|
41
|
-
/**
|
|
42
|
-
* Checks whether given offset in a string is between base character and combining mark or between two combining marks.
|
|
43
|
-
*
|
|
44
|
-
* @param string String to check.
|
|
45
|
-
* @param offset Offset to check.
|
|
46
|
-
*/
|
|
47
|
-
export declare function isInsideCombinedSymbol(string: string, offset: number): boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Checks whether given offset in a string is inside multi-character emoji sequence.
|
|
50
|
-
*
|
|
51
|
-
* @param string String to check.
|
|
52
|
-
* @param offset Offset to check.
|
|
53
|
-
*/
|
|
54
|
-
export declare function isInsideEmojiSequence(string: string, offset: number): boolean;
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Set of utils to handle unicode characters.
|
|
7
|
+
*
|
|
8
|
+
* @module utils/unicode
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Checks whether given `character` is a combining mark.
|
|
12
|
+
*
|
|
13
|
+
* @param character Character to check.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isCombiningMark(character: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether given `character` is a high half of surrogate pair.
|
|
18
|
+
*
|
|
19
|
+
* Using UTF-16 terminology, a surrogate pair denotes UTF-16 character using two UTF-8 characters. The surrogate pair
|
|
20
|
+
* consist of high surrogate pair character followed by low surrogate pair character.
|
|
21
|
+
*
|
|
22
|
+
* @param character Character to check.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isHighSurrogateHalf(character: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Checks whether given `character` is a low half of surrogate pair.
|
|
27
|
+
*
|
|
28
|
+
* Using UTF-16 terminology, a surrogate pair denotes UTF-16 character using two UTF-8 characters. The surrogate pair
|
|
29
|
+
* consist of high surrogate pair character followed by low surrogate pair character.
|
|
30
|
+
*
|
|
31
|
+
* @param character Character to check.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isLowSurrogateHalf(character: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Checks whether given offset in a string is inside a surrogate pair (between two surrogate halves).
|
|
36
|
+
*
|
|
37
|
+
* @param string String to check.
|
|
38
|
+
* @param offset Offset to check.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isInsideSurrogatePair(string: string, offset: number): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Checks whether given offset in a string is between base character and combining mark or between two combining marks.
|
|
43
|
+
*
|
|
44
|
+
* @param string String to check.
|
|
45
|
+
* @param offset Offset to check.
|
|
46
|
+
*/
|
|
47
|
+
export declare function isInsideCombinedSymbol(string: string, offset: number): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Checks whether given offset in a string is inside multi-character emoji sequence.
|
|
50
|
+
*
|
|
51
|
+
* @param string String to check.
|
|
52
|
+
* @param offset Offset to check.
|
|
53
|
+
*/
|
|
54
|
+
export declare function isInsideEmojiSequence(string: string, offset: number): boolean;
|