* _resolveString * Try to find a localized string matching the given `stringID`. * This function will recurse through all `searchLocales` until a string is found. * or until we run out of locales, then we will return a special "Missing translation" string. * * Note: If the `stringID`
(origStringID, replacements, searchLocales)
| 340 | * @return {Object?} result containing the localized string and chosen locale |
| 341 | */ |
| 342 | _resolveString(origStringID, replacements, searchLocales) { |
| 343 | if (!Array.isArray(searchLocales)) { |
| 344 | searchLocales = this._currLocaleCodes.slice(); // copy |
| 345 | } |
| 346 | |
| 347 | const locale = searchLocales.shift(); // remove first one |
| 348 | |
| 349 | // Note that we don't overwrite `locale` because that `en-US` value |
| 350 | // might be used later by the pluralRule or number formatter. |
| 351 | let tryLocale = locale; |
| 352 | if (locale.toLowerCase() === 'en-us') { // `en-US` strings are stored as `en` |
| 353 | tryLocale = 'en'; |
| 354 | } |
| 355 | |
| 356 | let stringID = origStringID.trim(); |
| 357 | let scope = 'core'; |
| 358 | |
| 359 | if (stringID[0] === '_') { |
| 360 | const parts = stringID.split('.'); |
| 361 | scope = parts[0].slice(1); |
| 362 | stringID = parts.slice(1).join('.'); |
| 363 | } |
| 364 | |
| 365 | let path = stringID |
| 366 | .split('.') |
| 367 | .map(s => s.replace(/<TX_DOT>/g, '.')) |
| 368 | .reverse(); |
| 369 | |
| 370 | let result = this._cache[tryLocale] && this._cache[tryLocale][scope]; |
| 371 | while (result !== undefined && path.length) { |
| 372 | result = result[path.pop()]; |
| 373 | } |
| 374 | |
| 375 | if (result !== undefined) { |
| 376 | if (replacements) { |
| 377 | if (typeof result === 'object' && Object.keys(result).length) { |
| 378 | // If plural forms are provided, dig one level deeper based on the |
| 379 | // first numeric token replacement provided. |
| 380 | const number = Object.values(replacements).find(val => (typeof val === 'number')); |
| 381 | if (number !== undefined) { |
| 382 | const rule = this.pluralRule(number, locale); |
| 383 | if (result[rule]) { |
| 384 | result = result[rule]; |
| 385 | } else { |
| 386 | // We're pretty sure this should be a plural but no string |
| 387 | // could be found for the given rule. Just pick the first |
| 388 | // string and hope it makes sense. |
| 389 | result = Object.values(result)[0]; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (typeof result === 'string') { |
| 395 | for (let key in replacements) { |
| 396 | let value = replacements[key]; |
| 397 | if (typeof value === 'number') { |
| 398 | if (value.toLocaleString) { |
| 399 | // format numbers for the locale |