|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// IMPORTANT: Do not import any node fs related modules here, as they do not work in browser. |
| 7 | + |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import * as path from 'path'; |
| 10 | +import { EXTENSION_ROOT_DIR } from '../../constants'; |
| 11 | +import { IFileSystem } from '../platform/types'; |
| 12 | + |
| 13 | +// Skip using vscode-nls and instead just compute our strings based on key values. Key values |
| 14 | +// can be loaded out of the nls.<locale>.json files |
| 15 | +let loadedCollection: Record<string, string> | undefined; |
| 16 | +let defaultCollection: Record<string, string> | undefined; |
| 17 | +let askedForCollection: Record<string, string> = {}; |
| 18 | +let loadedLocale: string; |
| 19 | + |
| 20 | +// This is exported only for testing purposes. |
| 21 | +export function _resetCollections(): void { |
| 22 | + loadedLocale = ''; |
| 23 | + loadedCollection = undefined; |
| 24 | + askedForCollection = {}; |
| 25 | +} |
| 26 | + |
| 27 | +// This is exported only for testing purposes. |
| 28 | +export function _getAskedForCollection(): Record<string, string> { |
| 29 | + return askedForCollection; |
| 30 | +} |
| 31 | + |
| 32 | +export function shouldLoadUsingNodeFS(): boolean { |
| 33 | + return !loadedCollection || parseLocale() !== loadedLocale; |
| 34 | +} |
| 35 | + |
| 36 | +declare let navigator: { language: string } | undefined; |
| 37 | + |
| 38 | +function parseLocale(): string { |
| 39 | + try { |
| 40 | + if (navigator?.language) { |
| 41 | + return navigator.language.toLowerCase(); |
| 42 | + } |
| 43 | + } catch { |
| 44 | + // Fall through |
| 45 | + } |
| 46 | + // Attempt to load from the vscode locale. If not there, use english |
| 47 | + const vscodeConfigString = process.env.VSCODE_NLS_CONFIG; |
| 48 | + return vscodeConfigString ? JSON.parse(vscodeConfigString).locale : 'en-us'; |
| 49 | +} |
| 50 | + |
| 51 | +export function getLocalizedString(key: string, defValue?: string): string { |
| 52 | + // The default collection (package.nls.json) is the fallback. |
| 53 | + // Note that we are guaranteed the following (during shipping) |
| 54 | + // 1. defaultCollection was initialized by the load() call above |
| 55 | + // 2. defaultCollection has the key (see the "keys exist" test) |
| 56 | + let collection = defaultCollection; |
| 57 | + |
| 58 | + // Use the current locale if the key is defined there. |
| 59 | + if (loadedCollection && loadedCollection.hasOwnProperty(key)) { |
| 60 | + collection = loadedCollection; |
| 61 | + } |
| 62 | + if (collection === undefined) { |
| 63 | + throw new Error(`Localizations haven't been loaded yet for key: ${key}`); |
| 64 | + } |
| 65 | + let result = collection[key]; |
| 66 | + if (!result && defValue) { |
| 67 | + // This can happen during development if you haven't fixed up the nls file yet or |
| 68 | + // if for some reason somebody broke the functional test. |
| 69 | + result = defValue; |
| 70 | + } |
| 71 | + askedForCollection[key] = result; |
| 72 | + |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Can be used to synchronously load localized strings, useful if we want localized strings at module level itself. |
| 78 | + * Cannot be used in VSCode web or any browser. Must be called before any use of the locale. |
| 79 | + */ |
| 80 | +export function loadLocalizedStringsUsingNodeFS(fs: IFileSystem): void { |
| 81 | + // Figure out our current locale. |
| 82 | + loadedLocale = parseLocale(); |
| 83 | + |
| 84 | + // Find the nls file that matches (if there is one) |
| 85 | + const nlsFile = path.join(EXTENSION_ROOT_DIR, `package.nls.${loadedLocale}.json`); |
| 86 | + if (fs.fileExistsSync(nlsFile)) { |
| 87 | + const contents = fs.readFileSync(nlsFile); |
| 88 | + loadedCollection = JSON.parse(contents); |
| 89 | + } else { |
| 90 | + // If there isn't one, at least remember that we looked so we don't try to load a second time |
| 91 | + loadedCollection = {}; |
| 92 | + } |
| 93 | + |
| 94 | + // Get the default collection if necessary. Strings may be in the default or the locale json |
| 95 | + if (!defaultCollection) { |
| 96 | + const defaultNlsFile = path.join(EXTENSION_ROOT_DIR, 'package.nls.json'); |
| 97 | + if (fs.fileExistsSync(defaultNlsFile)) { |
| 98 | + const contents = fs.readFileSync(defaultNlsFile); |
| 99 | + defaultCollection = JSON.parse(contents); |
| 100 | + } else { |
| 101 | + defaultCollection = {}; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Only uses the VSCode APIs to query filesystem and not the node fs APIs, as |
| 108 | + * they're not available in browser. Must be called before any use of the locale. |
| 109 | + */ |
| 110 | +export async function loadLocalizedStringsForBrowser(): Promise<void> { |
| 111 | + // Figure out our current locale. |
| 112 | + loadedLocale = parseLocale(); |
| 113 | + |
| 114 | + loadedCollection = await parseNLS(loadedLocale); |
| 115 | + |
| 116 | + // Get the default collection if necessary. Strings may be in the default or the locale json |
| 117 | + if (!defaultCollection) { |
| 118 | + defaultCollection = await parseNLS(); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +async function parseNLS(locale?: string) { |
| 123 | + try { |
| 124 | + const filename = locale ? `package.nls.${locale}.json` : `package.nls.json`; |
| 125 | + const nlsFile = vscode.Uri.joinPath(vscode.Uri.file(EXTENSION_ROOT_DIR), filename); |
| 126 | + const buffer = await vscode.workspace.fs.readFile(nlsFile); |
| 127 | + const contents = new TextDecoder().decode(buffer); |
| 128 | + return JSON.parse(contents); |
| 129 | + } catch { |
| 130 | + // If there isn't one, at least remember that we looked so we don't try to load a second time. |
| 131 | + return {}; |
| 132 | + } |
| 133 | +} |
0 commit comments