* Provide an async factory method for loading languages that clarifies the need to wait * for the language data to be loaded, so that other code doesn't press ahead before the language * is available. * * * @param {String} language_code - a language code or a fully-qualified URL to a langua
(language_code, script_path)
| 225 | * @returns {Language} - an instance of Language, or null if there's an error loading the translation file |
| 226 | */ |
| 227 | async function loadLanguage(language_code, script_path) { |
| 228 | var url = buildLanguageURL(language_code, script_path); |
| 229 | try { |
| 230 | if (!LANGUAGES[language_code]) { |
| 231 | let json = await fetchJSON(url) |
| 232 | LANGUAGES[language_code] = json |
| 233 | } |
| 234 | return new Language(language_code, script_path) |
| 235 | } catch (e) { |
| 236 | console.log(`Error loading language [${url}] ${e.statusText}`) |
| 237 | return null; |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | function buildLanguageURL(code, script_path) { |
| 243 | if (/\.json$/.test(code)) { |
no test coverage detected