UNPKG

5.08 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2021 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.nls = void 0;
19const localization_1 = require("./i18n/localization");
20var nls;
21(function (nls) {
22 nls.defaultLocale = 'en';
23 nls.localeId = 'localeId';
24 nls.locale = typeof window === 'object' && window && window.localStorage.getItem(nls.localeId) || undefined;
25 let keyProvider;
26 /**
27 * Automatically localizes a text if that text also exists in the vscode repository.
28 */
29 function localizeByDefault(defaultValue, ...args) {
30 if (nls.localization) {
31 const key = getDefaultKey(defaultValue);
32 if (key) {
33 return localize(key, defaultValue, ...args);
34 }
35 else {
36 console.warn(`Could not find translation key for default value: "${defaultValue}"`);
37 }
38 }
39 return localization_1.Localization.format(defaultValue, args);
40 }
41 nls.localizeByDefault = localizeByDefault;
42 function getDefaultKey(defaultValue) {
43 if (!keyProvider) {
44 keyProvider = new LocalizationKeyProvider();
45 }
46 const key = keyProvider.get(defaultValue);
47 if (key) {
48 return key;
49 }
50 return '';
51 }
52 nls.getDefaultKey = getDefaultKey;
53 function localize(key, defaultValue, ...args) {
54 return localization_1.Localization.localize(nls.localization, key, defaultValue, ...args);
55 }
56 nls.localize = localize;
57 function isSelectedLocale(id) {
58 if (nls.locale === undefined && id === nls.defaultLocale) {
59 return true;
60 }
61 return nls.locale === id;
62 }
63 nls.isSelectedLocale = isSelectedLocale;
64 function setLocale(id) {
65 window.localStorage.setItem(nls.localeId, id);
66 }
67 nls.setLocale = setLocale;
68})(nls = exports.nls || (exports.nls = {}));
69class LocalizationKeyProvider {
70 constructor() {
71 this.data = this.buildData();
72 }
73 get(defaultValue) {
74 const normalized = localization_1.Localization.normalize(defaultValue);
75 return this.data.get(normalized) || this.data.get(normalized.toUpperCase());
76 }
77 /**
78 * Transforms the data coming from the `nls.metadata.json` file into a map.
79 * The original data contains arrays of keys and messages.
80 * The result is a map that matches each message to the key that belongs to it.
81 *
82 * This allows us to skip the key in the localization process and map the original english default values to their translations in different languages.
83 */
84 buildData() {
85 const bundles = require('../../src/common/i18n/nls.metadata.json');
86 const keys = bundles.keys;
87 const messages = bundles.messages;
88 const data = new Map();
89 const keysAndMessages = this.buildKeyMessageTuples(keys, messages);
90 for (const { key, message } of keysAndMessages) {
91 data.set(message, key);
92 }
93 // Second pass adds each message again in upper case, if the message doesn't already exist in upper case
94 // The second pass is needed to not accidentally override any translations which actually use the upper case message
95 for (const { key, message } of keysAndMessages) {
96 const upperMessage = message.toUpperCase();
97 if (!data.has(upperMessage)) {
98 data.set(upperMessage, key);
99 }
100 }
101 return data;
102 }
103 buildKeyMessageTuples(keys, messages) {
104 const list = [];
105 for (const [fileKey, messageBundle] of Object.entries(messages)) {
106 const keyBundle = keys[fileKey];
107 for (let i = 0; i < messageBundle.length; i++) {
108 const message = localization_1.Localization.normalize(messageBundle[i]);
109 const key = keyBundle[i];
110 const localizationKey = this.buildKey(typeof key === 'string' ? key : key.key, fileKey);
111 list.push({
112 key: localizationKey,
113 message
114 });
115 }
116 }
117 return list;
118 }
119 buildKey(key, filepath) {
120 return `vscode/${localization_1.Localization.transformKey(filepath)}/${key}`;
121 }
122}
123//# sourceMappingURL=nls.js.map
\No newline at end of file