| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type * as vscode from 'vscode'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import fs from 'node:fs'; |
| b69ab31 | | | 11 | import path from 'node:path'; |
| b69ab31 | | | 12 | import {tryJsonParse} from 'shared/utils'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | // VS Code requires a restart if you change the configured language, |
| b69ab31 | | | 15 | // thus we can globally load this value on startup. |
| b69ab31 | | | 16 | const nlsConfig = tryJsonParse(process.env.VSCODE_NLS_CONFIG as string) as |
| b69ab31 | | | 17 | | {locale: string} |
| b69ab31 | | | 18 | | undefined; |
| b69ab31 | | | 19 | export const locale = validateLocale(nlsConfig?.locale) ?? 'en'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /** The locale will be inserted into the HTML of the webview as a JS variable, |
| b69ab31 | | | 22 | * so it's important that what we get from the environment variable |
| b69ab31 | | | 23 | * really is just a string locale. */ |
| b69ab31 | | | 24 | function validateLocale(l: string | undefined): string | undefined { |
| b69ab31 | | | 25 | if (l == null) { |
| b69ab31 | | | 26 | return undefined; |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | return /^[a-zA-Z_]+$/.test(l) ? l : undefined; |
| b69ab31 | | | 29 | } |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | let translations: {[key: string]: string} | undefined = undefined; |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | /** |
| b69ab31 | | | 34 | * Load translations for configured language from disk. |
| b69ab31 | | | 35 | * Should be called in extension's activate() method before |
| b69ab31 | | | 36 | * any calls to `t()`. |
| b69ab31 | | | 37 | */ |
| b69ab31 | | | 38 | export async function ensureTranslationsLoaded( |
| b69ab31 | | | 39 | extensionContext: vscode.ExtensionContext, |
| b69ab31 | | | 40 | ): Promise<void> { |
| b69ab31 | | | 41 | try { |
| b69ab31 | | | 42 | const translationsData = await fs.promises.readFile( |
| b69ab31 | | | 43 | path.join( |
| b69ab31 | | | 44 | extensionContext.extensionPath, |
| b69ab31 | | | 45 | locale === 'en' ? 'package.nls.json' : `package.nls.${locale}.json`, |
| b69ab31 | | | 46 | ), |
| b69ab31 | | | 47 | 'utf-8', |
| b69ab31 | | | 48 | ); |
| b69ab31 | | | 49 | translations = JSON.parse(translationsData); |
| b69ab31 | | | 50 | } catch (err) { |
| b69ab31 | | | 51 | // eslint-disable-next-line no-console |
| b69ab31 | | | 52 | console.error(`failed to load translation data for locale ${locale}: ${err}`); |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | /** |
| b69ab31 | | | 57 | * Internationalize (i18n-ize) a string from a given key. |
| b69ab31 | | | 58 | * This implementation is specifically for the vscode extension. |
| b69ab31 | | | 59 | * The webview has its own i18n system which can hook into the |
| b69ab31 | | | 60 | * VS Code configured language. |
| b69ab31 | | | 61 | * This function is used for UI-visible text from the extension host. |
| b69ab31 | | | 62 | * The translations live in package.nls.*.json files, |
| b69ab31 | | | 63 | * which includes translations for values within package.json, |
| b69ab31 | | | 64 | * such as command names. These nls files are included in the |
| b69ab31 | | | 65 | * distributed extension VSIX, but are not bundled directly into the JS when bundling. |
| b69ab31 | | | 66 | */ |
| b69ab31 | | | 67 | export function t(key: string): string { |
| b69ab31 | | | 68 | return translations?.[key] ?? key; |
| b69ab31 | | | 69 | } |