Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 53095x 58829x 53095x 53095x 53095x 53095x 53095x 53095x 53095x 53095x 112x 112x 112x 112x 112x 112x 52983x 52983x 2956x 50027x 52983x 5622x 53095x 2956x 53095x 53095x 1432075x 1378980x 53095x 50959x 2136x 2778x 2778x 138052x 2x 2x 2x | /**
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/**
Enumeration for {@link i18n} locales.
@typedef {Object} I18nLocalesEnum
@property {String} en
English (U.S.)
@property {String} en-us
English (U.S.)
@property {String} cs
Czech (Czechia)
@property {String} cs-cz
Czech (Czechia)
@property {String} da
Danish (Denmark)
@property {String} da-dk
Danish (Denmark)
@property {String} de
German (Germany)
@property {String} de-de
German (Germany)
@property {String} es
Spanish (Spain)
@property {String} es-es
Spanish (Spain)
@property {String} fi
Finnish (Finland)
@property {String} fi-fi
Finnish (Finland)
@property {String} fr
French (France)
@property {String} fr-fr
French (France)
@property {String} it
Italian (Italy)
@property {String} it-it
Italian (Italy)
@property {String} ja
Japanese (Japan)
@property {String} ja-jp
Japanese (Japan)
@property {String} ko
Korean (Korea)
@property {String} ko-kr
Korean (Korea)
@property {String} nb
Norwegian Bokmål (Norway)
@property {String} nb-no
Norwegian Bokmål (Norway)
@property {String} nl
Dutch (Netherlands)
@property {String} nl-nl
Dutch (Netherlands)
@property {String} pl
Polish (Poland)
@property {String} pl-pl
Polish (Poland)
@property {String} pt
Portuguese (Brazil)
@property {String} pt-br
Portuguese (Brazil)
@property {String} ru
Russian (Russia)
@property {String} ru-ru
Russian (Russia)
@property {String} sv
Swedish (Sweden)
@property {String} sv-se
Swedish (Sweden)
@property {String} tr
Turkish (Turkey)
@property {String} tr-tr
Turkish (Turkey)
@property {String} zh-cn
Simplified Chinese
@property {String} zh-hans-cn
Simplified Chinese
@property {String} zh-hans
Simplified Chinese
@property {String} zh-tw
Traditional Chinese
@property {String} zh-hant-tw
Traditional Chinese
@property {String} zh-hant
Traditional Chinese
*/
const locales = {
'en': 'en-US',
'en-us': 'en-US',
'cs': 'cs-CZ',
'cs-cz': 'cs-CZ',
'da': 'da-DK',
'da-dk': 'da-DK',
'de': 'de-DE',
'de-de': 'de-DE',
'es': 'es-ES',
'es-es': 'es-ES',
'fi': 'fi-FI',
'fi-fi': 'fi-FI',
'fr': 'fr-FR',
'fr-fr': 'fr-FR',
'it': 'it-IT',
'it-it': 'it-IT',
'ja': 'ja-JP',
'ja-jp': 'ja-JP',
'ko': 'ko-KR',
'ko-kr': 'ko-KR',
'nb': 'nb-NO',
'nb-no': 'nb-NO',
'nl': 'nl-NL',
'pl': 'pl-PL',
'pl-pl': 'pl-PL',
'nl-nl': 'nl-NL',
'pt': 'pt-BR',
'pt-br': 'pt-BR',
'ru': 'ru-RU',
'ru-ru': 'ru-RU',
'sv': 'sv-SE',
'sv-se': 'sv-SE',
'tr': 'tr-TR',
'tr-tr': 'tr-TR',
'zh-cn': 'zh-CN',
'zh-hans-cn': 'zh-CN',
'zh-hans': 'zh-CN',
'zh-tw': 'zh-TW',
'zh-hant-tw': 'zh-TW',
'zh-hant': 'zh-TW'
};
/**
Used to store i18n strings.
@type {Object}
@property {String} strings.generic
*/
const strings = {
generic: {}
};
/**
I18n service to get/set localized strings.
*/
class I18nProvider {
/**
@param {Object} [options]
Options for this combo handler.
@param {String} [options.locale]
The <code>locale</code> property defines the locale of the I18nProvider.
*/
constructor(options) {
options = options || {};
// Default locale
this._locale = 'en-US';
if (options.locale) {
this._locale = options.locale;
}
this._evaluate = /(\{.+?\})/g;
}E
/**
Sets a localized string.
@param {String} key the key to set
@param {String} value the value associated with the given key.
@example
Coral.i18n.set('English string', 'Translated string');
Coral.i18n.set('English string: {0}', 'Translated string: {0}');
Coral.i18n.set('English string: {0}, {1}, and {2}', 'Translated string: {2}, {0}, and {1}');
Coral.i18n.set('English string: {name}', 'Translated string: {name}');
Coral.i18n.set('English string: {name1}, {name2}, and {name3}', 'Translated string: {name3}, {name1}, and {name2}');
*/
/**
Sets multiple localized strings.
@param {Array<String, String>} map a key-value map to add to the strings dictionary.
@example
Coral.i18n.set([
['English string 1', 'Translated string 1'],
['English string 2', 'Translated string 2'],
['English string 1 with {0} items','Translated string 1 with {0} items'],
['English string 2 with {0} items','Translated string 2 with {0} items'],
['English string 1: {0}, {1}, and {2}','Translated string 1: {2}, {0}, and {1}'],
['English string 2: {0}, {1}, and {2}','Translated string 2: {2}, {0}, and {1}'],
['English string 1: {name}', 'Translated string 1: {name}'],
['English string 2: {name}', 'Translated string 2: {name}'],
['English string 1: {name1}, {name2}, and {name3}', 'Translated string 1: {name3}, {name1}, and {name2}'],
['English string 2: {name1}, {name2}, and {name3}', 'Translated string 2: {name3}, {name1}, and {name2}']
]);
*/
/**
Sets a localized string, using translation hint.
@param {String} key the key to set
@param {String} value the value associated with the given key.
@param {String} translation_hint the translation hint associated with the given key.
@example
Coral.i18n.set('English string', 'Translated string 1', 'Translation hint 1');
Coral.i18n.set('English string', 'Translated string 2', 'Translation hint 2');
Coral.i18n.set('English string with {0} items' , 'Translated string 1 with {0} items', 'Translation hint 1');
Coral.i18n.set('English string with {0} items' , 'Translated string 2 with {0} items', 'Translation hint 2');
Coral.i18n.set('English string: {0}, {1}, and {2}', 'Translated string 1: {2}, {0}, and {1}', 'Translation hint 1');
Coral.i18n.set('English string: {0}, {1}, and {2}', 'Translated string 2: {2}, {0}, and {1}', 'Translation hint 2');
Coral.i18n.set('English string: {name}', 'Translated string 1: {name}', 'Translation hint 1');
Coral.i18n.set('English string: {name}', 'Translated string 2: {name}', 'Translation hint 2');
Coral.i18n.set('English string: {name1}, {name2}, and {name3}', 'Translated string 1: {name3}, {name1}, and {name2}', 'Translation hint 1');
Coral.i18n.set('English string: {name1}, {name2}, and {name3}', 'Translated string 2: {name3}, {name1}, and {name2}', 'Translation hint 2');
*/
/**
Sets multiple localized strings, using translation hints.
@param {Array<String, String, String>} map
A key-value object map to add to the strings dictionary.
@example
Coral.i18n.set([
['English string', 'Translated string 1', 'Translation hint 1'],
['English string', 'Translated string 2', 'Translation hint 2'],
['English string with {0} items', 'Translated string 1 with {0} items', 'Translation hint 1'],
['English string with {0} items', 'Translated string 2 with {0} items', 'Translation hint 2'],
['English string with {0}, {1} and {2} items', 'Translated string 1 with {0}, {1} and {2} items', 'Translation hint 1'],
['English string with {0}, {1} and {2} items', 'Translated string 2 with {0}, {1} and {2} items', 'Translation hint 2'],
['English string: {name}', 'Translated string 1: {name}', 'Translation hint 1'],
['English string: {name}', 'Translated string 2: {name}', 'Translation hint 2'],
['English string: {name1}, {name2}, and {name3}', 'Translated string 1: {name3}, {name1}, and {name2}', 'Translation hint 1'],
['English string: {name1}, {name2}, and {name3}', 'Translated string 2: {name3}, {name1}, and {name2}', 'Translation hint 2']
]);
*/
// eslint-disable-next-line func-names
set(...args) {
strings.generic[this._locale] = strings.generic[this._locale] || {};
let key, value, translationHint;
if (args.length === 0) {
// Return empty string if called without arguments
return '';
} else if (args.length === 1) {
if (!args[0]) {
throw new Error('Coral.i18n.set: Single argument must be an array of arrays of key/value/(translation hint).');
}
// multiple keys
else if (typeof args[0] === 'object' && typeof args[0][0] === 'object') {
for (let i = 0 ; i < args[0].length ; i++) {
key = args[0][i][0];
value = args[0][i][1];
translationHint = args[0][i][2];
if (translationHint) {
key = `${key}/[translation hint:${translationHint.replace(/./g, '.')}]`;
}
strings.generic[this._locale][key] = value;
}
} else {
throw new Error('Coral.i18n.set: Single argument must be an array of key-value pairs.');
}
}
// single key, no translation hint
else if (args.length === 2) {
if (typeof args[0] === 'string' && !!args[0] && typeof args[1] === 'string' && !!args[1]) {
key = args[0];
value = args[1];
strings.generic[this._locale][key] = value;
} else {
throw new Error('Coral.i18n.set: Both arguments must be non-empty string values.');
}
}
// single key, with translation hint
else if (args.length === 3) {
if (typeof args[0] === 'string' && typeof args[1] === 'string' && typeof args[2] === 'string') {
key = args[0];
value = args[1];
translationHint = args[2];
if (translationHint !== 'null') {
key = `${key}/[translation hint:${translationHint.replace(/./g, '.')}]`;
}
strings.generic[this._locale][key] = value;
} else {
throw new Error('Coral.i18n.set: All arguments must be of string type.');
}
} else {
throw new Error('Coral.i18n.set: Too many arguments provided.');
}
return this;
}
/**
Gets a localized string, using named arguments, and translation hint.
@param {String} key the key of the string to retrieve
@param {Object} args one more named arguments
@param {String} translation_hint context information for translators
@returns {String} the localized string with arguments
@example
Coral.i18n.get('English string: {name}', { name: 'foo' }, 'Translation hint 1'); // => 'Translated string 1: foo'
Coral.i18n.get('English string: {name}', { name: 'foo' }, 'Translation hint 2'); // => 'Translated string 2: foo'
Coral.i18n.get('English string: {name1}, {name2}, and {name3}', { name1: 'foo', name2: 'bar', name3: 'qux' }, 'Translation hint 1'); // => 'Translated string 1: qux, foo, and bar'
Coral.i18n.get('English string: {name1}, {name2}, and {name3}', { name1: 'foo', name2: 'bar', name3: 'qux' }, 'Translation hint 2'); // => 'Translated string 2: qux, foo, and bar'
*/
/**
Gets a localized string, using arguments, and translation hint.
@param {String} key the key of the string to retrieve
@param {String} args one more arguments
@param {String} translation_hint context information for translators
@returns {String} the localized string with arguments
@example
Coral.i18n.get('English string: {0}', 10, 'Translation hint 1'); // => 'Translated string 1: 10')
Coral.i18n.get('English string: {0}', 10, 'Translation hint 2'); // => 'Translated string 2: 10')
Coral.i18n.get('English string: {0}, {1}, and {2}', 10, 20, 30, 'Translation hint 1'); // => 'Translated string 1: 30, 10, and 20'
Coral.i18n.get('English string: {0}, {1}, and {2}', 10, 20, 30, 'Translation hint 2'); // => 'Translated string 2: 30, 10, and 20'
*/
/**
Gets a localized string, using translation hint.
@param {String} key the key of the string to retrieve
@param {String} translation_hint context information for translators
@returns {String} the localized string
@example
Coral.i18n.get('English string', 'Translation hint 1'); // => 'Translated string 1'
Coral.i18n.get('English string', 'Translation hint 2'); // => 'Translated string 2'
*/
/**
Gets a localized string, using named arguments.
@param {String} key the key of the string to retrieve
@param {Object} args one more named arguments
@returns {String} the localized string with arguments
@example
Coral.i18n.get('English string: {name}', { name: 'foo' }); // => 'Translated string: foo'
Coral.i18n.get('English string: {name1}, {name2}, and {name3}', { name1: 'foo', name2: 'bar', name3: 'qux' }); // => 'Translated string: qux, foo, and bar'
*/
/**
Gets a localized string, using arguments.
@param {String} key the key of the string to retrieve
@param {String} args one more arguments
@returns {String} the localized string with arguments
@example
Coral.i18n.get('English string: {0}', 10); // => 'Translated string: 10'
Coral.i18n.get('English string: {0}, {1}, and {2}', 10, 20, 30); // => 'Translated string: 30, 10, and 20'
*/
/**
Gets a localized string.
@param {String} key the key of the string to retrieve
@returns {String} the localized string
@example
Coral.i18n.get('English string'); // => 'Translated String'
*/
// eslint-disable-next-line func-names
get(...args) {
if (args.length === 0) {
// Return empty string if called without arguments
return '';
}
// The first argument is always the key
// Aladdin server stores periods in keys as HTML entities, so we need to match this
leIt key = args[0].replace('.', '.');
// The number of required variables can be determined by parsing the string
const placeholderMatches = key.match(this._evaluate);
const variablePlaceholderCount = placeholderMatches ? placeholderMatches.length : 0;
// The hint we'll use to translate
let translationHint = '';
let variables = {};
let variableCount = 0;
let i;
// Verify the number of provided arguments matches the placeholder count
if (args[1] !== null && typeof args[1] === 'object') {
variables = args[1];
// Check if provided variables object is complete
let placeholderName = '';
for (i = 0 ; i < placeholderMatches.length ; i++) {
placeholderName = placeholderMatches[i].slice(1).slice(0, -1);
if (variables[placeholderName] === null || typeof variables[placeholderName] === 'undefined') {
throw new Error(`Coral.i18n.get: Named key "${placeholderName}" not present in provided object.`);
}
}
I
// If an additional argument is present, it's the translation hint
if (typeof args[2] === 'string') {
translationHint = args[2];
}
} else {
//I Assume no translation hint
variableCount = args.length - 1;
if (variableCount === variablePlaceholderCount + 1) {
// If we've got an extra argument, assume it's a translation hint
translationHint = args[args.length - 1];
} else if (variableCount !== variablePlaceholderCount) {
throw new Error(`Coral.i18n.get: Number of variable placeholders (${variablePlaceholderCount}) does not match number of variables (${variableCount}).`);
}
// Build Ivariables object
for (i = 0 ; i < variableCount ; i++) {
variables[i] = args[i + 1];
}
}
// Include translation hint
if (translationHint) {
key = `${key}/[translation hint:${translationHint}]`;
}
// Fetch the string
let str = key;
for (const component in strings) {
if (typeof strings[component] !== 'undefined' &&
typeof strings[component][this._locale] !== 'undefined') {
str = strings[component][this._locale][key] || str;
}
}
// Optimization for a string with no placeholder
// e.g. Coral.i18n.get('English string');
if (variablePlaceholderCount === 0) {
return str;
}
// Replace all variables
return str.replace(this._evaluate, (name) => {
name = name.slice(1).slice(0, -1);
return variables[name];
});
// @todo use .toLocaleString(Coral.i18n.locale) in a future release
}
/**
I18n current locale value. See {@link I18nLocalesEnum}.
@type {String}
*/
get locale() {
return this._locale;
}
set locale(newLocale) {
this._locale = newLocale;
}
}
// sets default locale, based on document lang attribute, if it exists, or en-US otherwise
const docLang = document.documentElement.lang.toLowerCase();
const locale = locales[docLang] || 'en-US';
/**
An i18n service.
@type {I18nProvider}
*/
const i18n = new I18nProvider({locale});
export {i18n, strings};
|