UNPKG

5.03 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 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.localeId = 'localeId';
23 nls.locale = typeof window === 'object' && window && window.localStorage.getItem(nls.localeId) || undefined;
24 let keyProvider;
25 /**
26 * Automatically localizes a text if that text also exists in the vscode repository.
27 */
28 function localizeByDefault(defaultValue, ...args) {
29 if (nls.localization) {
30 const key = getDefaultKey(defaultValue);
31 if (key) {
32 return localize(key, defaultValue, ...args);
33 }
34 else {
35 console.warn(`Could not find translation key for default value: "${defaultValue}"`);
36 }
37 }
38 return localization_1.Localization.format(defaultValue, args);
39 }
40 nls.localizeByDefault = localizeByDefault;
41 function getDefaultKey(defaultValue) {
42 if (!keyProvider) {
43 keyProvider = new LocalizationKeyProvider();
44 }
45 const key = keyProvider.get(defaultValue);
46 if (key) {
47 return key;
48 }
49 return '';
50 }
51 nls.getDefaultKey = getDefaultKey;
52 function localize(key, defaultValue, ...args) {
53 return localization_1.Localization.localize(nls.localization, key, defaultValue, ...args);
54 }
55 nls.localize = localize;
56 function isSelectedLocale(id) {
57 if (nls.locale === undefined && id === 'en') {
58 return true;
59 }
60 return nls.locale === id;
61 }
62 nls.isSelectedLocale = isSelectedLocale;
63 function setLocale(id) {
64 window.localStorage.setItem(nls.localeId, id);
65 }
66 nls.setLocale = setLocale;
67})(nls = exports.nls || (exports.nls = {}));
68class LocalizationKeyProvider {
69 constructor() {
70 this.data = this.buildData();
71 }
72 get(defaultValue) {
73 const normalized = localization_1.Localization.normalize(defaultValue);
74 return this.data.get(normalized) || this.data.get(normalized.toUpperCase());
75 }
76 /**
77 * Transforms the data coming from the `nls.metadata.json` file into a map.
78 * The original data contains arrays of keys and messages.
79 * The result is a map that matches each message to the key that belongs to it.
80 *
81 * This allows us to skip the key in the localization process and map the original english default values to their translations in different languages.
82 */
83 buildData() {
84 const bundles = require('../../src/common/i18n/nls.metadata.json');
85 const keys = bundles.keys;
86 const messages = bundles.messages;
87 const data = new Map();
88 const keysAndMessages = this.buildKeyMessageTuples(keys, messages);
89 for (const { key, message } of keysAndMessages) {
90 data.set(message, key);
91 }
92 // Second pass adds each message again in upper case, if the message doesn't already exist in upper case
93 // The second pass is needed to not accidentally override any translations which actually use the upper case message
94 for (const { key, message } of keysAndMessages) {
95 const upperMessage = message.toUpperCase();
96 if (!data.has(upperMessage)) {
97 data.set(upperMessage, key);
98 }
99 }
100 return data;
101 }
102 buildKeyMessageTuples(keys, messages) {
103 const list = [];
104 for (const [fileKey, messageBundle] of Object.entries(messages)) {
105 const keyBundle = keys[fileKey];
106 for (let i = 0; i < messageBundle.length; i++) {
107 const message = localization_1.Localization.normalize(messageBundle[i]);
108 const key = keyBundle[i];
109 const localizationKey = this.buildKey(typeof key === 'string' ? key : key.key, fileKey);
110 list.push({
111 key: localizationKey,
112 message
113 });
114 }
115 }
116 return list;
117 }
118 buildKey(key, filepath) {
119 return `vscode/${localization_1.Localization.transformKey(filepath)}/${key}`;
120 }
121}
122//# sourceMappingURL=nls.js.map
\No newline at end of file