1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.nls = void 0;
|
19 | const localization_1 = require("./i18n/localization");
|
20 | var 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 |
|
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 = {}));
|
68 | class 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 |
|
78 |
|
79 |
|
80 |
|
81 |
|
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 |
|
93 |
|
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 |
|
\ | No newline at end of file |