* @param {string} english_text - The English text to localize * @param {...string} interpolations - Strings to replace %1, %2, etc. * @returns {string} - Text in the current language
(english_text, ...interpolations)
| 15 | * @returns {string} - Text in the current language |
| 16 | */ |
| 17 | function localize(english_text, ...interpolations) { |
| 18 | function find_localization(english_text) { |
| 19 | if (AccessKeys.has(english_text)) { |
| 20 | const without_hotkey = AccessKeys.remove(english_text); |
| 21 | if (localizations[without_hotkey]) { |
| 22 | const hotkey_def = AccessKeys.get(english_text); |
| 23 | if (localizations[without_hotkey].toUpperCase().indexOf(hotkey_def.toUpperCase()) > -1) { |
| 24 | return localizations[without_hotkey]; |
| 25 | } else { |
| 26 | if (AccessKeys.has(localizations[without_hotkey])) { |
| 27 | // window.console?.warn(`Localization has differing access key hint: '${localizations[without_hotkey]}' vs '${english_text}'`); |
| 28 | // @TODO: detect differing access key more generally |
| 29 | return `${AccessKeys.remove(localizations[without_hotkey])} (${hotkey_def})`; |
| 30 | } |
| 31 | return `${localizations[without_hotkey]} (${hotkey_def})`; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | if (localizations[english_text]) { |
| 36 | return localizations[english_text]; |
| 37 | } |
| 38 | return english_text; |
| 39 | } |
| 40 | function interpolate(text, interpolations) { |
| 41 | for (let i = 0; i < interpolations.length; i++) { |
| 42 | text = text.replace(`%${i + 1}`, interpolations[i]); |
| 43 | } |
| 44 | return text; |
| 45 | } |
| 46 | return interpolate(find_localization(english_text), interpolations); |
| 47 | } |
| 48 | |
| 49 | const language_storage_key = "jspaint language"; |
| 50 | // @ts-ignore |
no test coverage detected