vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | import * as Platform from '../platform/platform.js'; |
| 5 | |
| 6 | // eslint-disable-next-line |
vidorteg | 3e2942e | 2020-09-17 04:16:56 | [diff] [blame] | 7 | import i18nBundle from '../third_party/i18n/i18n.js'; |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * The locale that DevTools displays |
| 11 | * @param {string} locale |
| 12 | * @param {*} lhlMessages |
| 13 | */ |
| 14 | export const registerLocaleData = i18nBundle.registerLocaleData; |
| 15 | |
| 16 | /** |
| 17 | * The locale that DevTools displays |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 18 | * @type {string|undefined} |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 19 | */ |
| 20 | export let registeredLocale; |
| 21 | |
| 22 | /** |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 23 | * The strings from the module.json file |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 24 | * @type {!Object|undefined} |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 25 | */ |
| 26 | let moduleJSONStrings; |
| 27 | |
| 28 | /** |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 29 | * Returns an instance of an object of formatted strings based on locale. If the instance is not |
| 30 | * set at the time of calling, it is created. |
| 31 | * @return {!Object} |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 32 | */ |
| 33 | function getOrSetModuleJSONStrings() { |
| 34 | if (!registeredLocale) { |
| 35 | throw new Error(`Unsupported locale '${registeredLocale}'`); |
| 36 | } |
| 37 | |
| 38 | moduleJSONStrings = moduleJSONStrings || i18nBundle.getRendererFormattedStrings(registeredLocale); |
| 39 | return moduleJSONStrings; |
| 40 | } |
| 41 | |
| 42 | /** |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 43 | * Take the locale passed in from the browser(host), run through the fallback logic (example: es-419 -> es) |
| 44 | * to find the DevTools supported locale and register it. |
| 45 | * @param {string} locale |
| 46 | */ |
| 47 | export function registerLocale(locale) { |
| 48 | registeredLocale = i18nBundle.lookupLocale(locale); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Retrieve the localized string. |
| 53 | * @param {function(string, ?Object):string} str_ |
| 54 | * @param {string} id |
| 55 | * @param {!Object} values |
Tim van der Lippe | 3b1f745 | 2021-01-08 11:25:13 | [diff] [blame^] | 56 | * @return {!Platform.UIString.LocalizedString} the localized version of the |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 57 | */ |
| 58 | export function getLocalizedString(str_, id, values = {}) { |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 59 | if (!registeredLocale) { |
| 60 | throw new Error(`Unsupported locale '${registeredLocale}'`); |
| 61 | } |
| 62 | |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 63 | const icuMessage = str_(id, values); |
Tim van der Lippe | 3b1f745 | 2021-01-08 11:25:13 | [diff] [blame^] | 64 | return /** @type {!Platform.UIString.LocalizedString} */ (i18nBundle.getFormatted(icuMessage, registeredLocale)); |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Register a file's UIStrings with i18n, return function to generate the string ids. |
| 69 | * @param {string} path |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 70 | * @param {!Object} stringStructure |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 71 | * @return {function(string, ?Object):string} return function to generate the string ids. |
| 72 | */ |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 73 | export function registerUIStrings(path, stringStructure) { |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 74 | /** |
| 75 | * Convert a message string & replacement values into an |
| 76 | * indexed id value in the form '{messageid} | # {index}'. |
| 77 | * |
| 78 | * @param {string} id |
| 79 | * @param {?Object} value |
| 80 | * */ |
| 81 | const str = (id, value) => { |
| 82 | try { |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 83 | const i18nInstance = i18nBundle.createMessageInstanceIdFn(path, stringStructure); |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 84 | return i18nInstance(id, value); |
| 85 | } catch (e) { |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 86 | // ID was not in the main file search for module.json strings |
| 87 | if (e instanceof i18nBundle.idNotInMainDictionaryException) { |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 88 | const stringMappingArray = Object.getOwnPropertyNames(getOrSetModuleJSONStrings()); |
vidorteg | 8bce4b0 | 2020-10-09 01:16:21 | [diff] [blame] | 89 | const index = stringMappingArray.indexOf(id); |
| 90 | if (index >= 0) { |
| 91 | return stringMappingArray[index]; |
| 92 | } |
| 93 | } |
| 94 | |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 95 | return id; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | return str; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns a span element that may contains other DOM element as placeholders |
| 104 | * @param {function(string, ?Object):string} str_ |
| 105 | * @param {string} stringId |
Christy Chen | 2e411e2 | 2020-10-29 19:19:40 | [diff] [blame] | 106 | * @param {!Object<string, *>} placeholders |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 107 | * @return {!Element} the localized result |
| 108 | */ |
Christy Chen | 2e411e2 | 2020-10-29 19:19:40 | [diff] [blame] | 109 | export function getFormatLocalizedString(str_, stringId, placeholders) { |
Kalon Hinds | 3cb4a09 | 2020-11-24 22:55:31 | [diff] [blame] | 110 | if (!registeredLocale) { |
| 111 | throw new Error(`Unsupported locale '${registeredLocale}'`); |
| 112 | } |
| 113 | |
vidorteg | 1fd76f8 | 2020-08-26 18:09:20 | [diff] [blame] | 114 | const icuMessage = str_(stringId, placeholders); |
| 115 | const formatter = i18nBundle.getFormatter(icuMessage, registeredLocale); |
| 116 | |
| 117 | const icuElements = formatter.getAst().elements; |
| 118 | const args = []; |
| 119 | let formattedString = ''; |
| 120 | for (const element of icuElements) { |
| 121 | if (element.type === 'argumentElement') { |
| 122 | const placeholderValue = placeholders[element.id]; |
| 123 | if (placeholderValue) { |
| 124 | args.push(placeholderValue); |
| 125 | element.value = '%s'; // convert the {PH} back to %s to use StringUtilities |
| 126 | } |
| 127 | } |
| 128 | formattedString += element.value; |
| 129 | } |
| 130 | return formatLocalized(formattedString, args); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param {string} formattedString |
| 135 | * @param {?ArrayLike<?>} args |
| 136 | * @return {!Element} the formatted result. |
| 137 | */ |
| 138 | export function formatLocalized(formattedString, args) { |
| 139 | /** @type {function(string):string} */ |
| 140 | const substitution = substitution => { |
| 141 | return substitution; |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * @param {!Element} a |
| 146 | * @param {?} b |
| 147 | * @return {!Element} |
| 148 | */ |
| 149 | function append(a, b) { |
| 150 | a.appendChild(typeof b === 'string' ? document.createTextNode(b) : b); |
| 151 | return a; |
| 152 | } |
| 153 | |
| 154 | const formatters = {s: substitution}; |
| 155 | return Platform.StringUtilities.format(formattedString, args, formatters, document.createElement('span'), append) |
| 156 | .formattedResult; |
| 157 | } |