Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/nls.ts
5241 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export function getNLSMessages(): string[] {
7
return globalThis._VSCODE_NLS_MESSAGES;
8
}
9
10
export function getNLSLanguage(): string | undefined {
11
return globalThis._VSCODE_NLS_LANGUAGE;
12
}
13
14
declare const document: { location?: { hash?: string } } | undefined;
15
const isPseudo = getNLSLanguage() === 'pseudo' || (typeof document !== 'undefined' && document.location && typeof document.location.hash === 'string' && document.location.hash.indexOf('pseudo=true') >= 0);
16
17
export interface ILocalizeInfo {
18
key: string;
19
comment: string[];
20
}
21
22
export interface ILocalizedString {
23
original: string;
24
value: string;
25
}
26
27
function _format(message: string, args: (string | number | boolean | undefined | null)[]): string {
28
let result: string;
29
30
if (args.length === 0) {
31
result = message;
32
} else {
33
result = message.replace(/\{(\d+)\}/g, (match, rest) => {
34
const index = rest[0];
35
const arg = args[index];
36
let result = match;
37
if (typeof arg === 'string') {
38
result = arg;
39
} else if (typeof arg === 'number' || typeof arg === 'boolean' || arg === void 0 || arg === null) {
40
result = String(arg);
41
}
42
return result;
43
});
44
}
45
46
if (isPseudo) {
47
// FF3B and FF3D is the Unicode zenkaku representation for [ and ]
48
result = '\uFF3B' + result.replace(/[aouei]/g, '$&$&') + '\uFF3D';
49
}
50
51
return result;
52
}
53
54
/**
55
* Marks a string to be localized. Returns the localized string.
56
*
57
* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.
58
* @param message The string to localize
59
* @param args The arguments to the string
60
*
61
* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`
62
* @example `localize({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`
63
*
64
* @returns string The localized string.
65
*/
66
export function localize(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): string;
67
68
/**
69
* Marks a string to be localized. Returns the localized string.
70
*
71
* @param key The key to use for localizing the string
72
* @param message The string to localize
73
* @param args The arguments to the string
74
*
75
* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`
76
* @example For example, `localize('sayHello', 'hello {0}', name)`
77
*
78
* @returns string The localized string.
79
*/
80
export function localize(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): string;
81
82
/**
83
* @skipMangle
84
*/
85
export function localize(data: ILocalizeInfo | string /* | number when built */, message: string /* | null when built */, ...args: (string | number | boolean | undefined | null)[]): string {
86
if (typeof data === 'number') {
87
return _format(lookupMessage(data, message), args);
88
}
89
return _format(message, args);
90
}
91
92
/**
93
* Only used when built: Looks up the message in the global NLS table.
94
* This table is being made available as a global through bootstrapping
95
* depending on the target context.
96
*/
97
function lookupMessage(index: number, fallback: string | null): string {
98
const message = getNLSMessages()?.[index];
99
if (typeof message !== 'string') {
100
if (typeof fallback === 'string') {
101
return fallback;
102
}
103
throw new Error(`!!! NLS MISSING: ${index} !!!`);
104
}
105
return message;
106
}
107
108
/**
109
* Marks a string to be localized. Returns an {@linkcode ILocalizedString}
110
* which contains the localized string and the original string.
111
*
112
* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.
113
* @param message The string to localize
114
* @param args The arguments to the string
115
*
116
* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`
117
* @example `localize2({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`
118
*
119
* @returns ILocalizedString which contains the localized string and the original string.
120
*/
121
export function localize2(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;
122
123
/**
124
* Marks a string to be localized. Returns an {@linkcode ILocalizedString}
125
* which contains the localized string and the original string.
126
*
127
* @param key The key to use for localizing the string
128
* @param message The string to localize
129
* @param args The arguments to the string
130
*
131
* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`
132
* @example `localize('sayHello', 'hello {0}', name)`
133
*
134
* @returns ILocalizedString which contains the localized string and the original string.
135
*/
136
export function localize2(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;
137
138
/**
139
* @skipMangle
140
*/
141
export function localize2(data: ILocalizeInfo | string /* | number when built */, originalMessage: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString {
142
let message: string;
143
if (typeof data === 'number') {
144
message = lookupMessage(data, originalMessage);
145
} else {
146
message = originalMessage;
147
}
148
149
const value = _format(message, args);
150
151
return {
152
value,
153
original: originalMessage === message ? value : _format(originalMessage, args)
154
};
155
}
156
157
export interface INLSLanguagePackConfiguration {
158
159
/**
160
* The path to the translations config file that contains pointers to
161
* all message bundles for `main` and extensions.
162
*/
163
readonly translationsConfigFile: string;
164
165
/**
166
* The path to the file containing the translations for this language
167
* pack as flat string array.
168
*/
169
readonly messagesFile: string;
170
171
/**
172
* The path to the file that can be used to signal a corrupt language
173
* pack, for example when reading the `messagesFile` fails. This will
174
* instruct the application to re-create the cache on next startup.
175
*/
176
readonly corruptMarkerFile: string;
177
}
178
179
export interface INLSConfiguration {
180
181
/**
182
* Locale as defined in `argv.json` or `app.getLocale()`.
183
*/
184
readonly userLocale: string;
185
186
/**
187
* Locale as defined by the OS (e.g. `app.getPreferredSystemLanguages()`).
188
*/
189
readonly osLocale: string;
190
191
/**
192
* The actual language of the UI that ends up being used considering `userLocale`
193
* and `osLocale`.
194
*/
195
readonly resolvedLanguage: string;
196
197
/**
198
* Defined if a language pack is used that is not the
199
* default english language pack. This requires a language
200
* pack to be installed as extension.
201
*/
202
readonly languagePack?: INLSLanguagePackConfiguration;
203
204
/**
205
* The path to the file containing the default english messages
206
* as flat string array. The file is only present in built
207
* versions of the application.
208
*/
209
readonly defaultMessagesFile: string;
210
211
/**
212
* Below properties are deprecated and only there to continue support
213
* for `vscode-nls` module that depends on them.
214
* Refs https://github.com/microsoft/vscode-nls/blob/main/src/node/main.ts#L36-L46
215
*/
216
/** @deprecated */
217
readonly locale: string;
218
/** @deprecated */
219
readonly availableLanguages: Record<string, string>;
220
/** @deprecated */
221
readonly _languagePackSupport?: boolean;
222
/** @deprecated */
223
readonly _languagePackId?: string;
224
/** @deprecated */
225
readonly _translationsConfigFile?: string;
226
/** @deprecated */
227
readonly _cacheRoot?: string;
228
/** @deprecated */
229
readonly _resolvedLanguagePackCoreLocation?: string;
230
/** @deprecated */
231
readonly _corruptedFile?: string;
232
}
233
234
export interface ILanguagePack {
235
readonly hash: string;
236
readonly label: string | undefined;
237
readonly extensions: {
238
readonly extensionIdentifier: { readonly id: string; readonly uuid?: string };
239
readonly version: string;
240
}[];
241
readonly translations: Record<string, string | undefined>;
242
}
243
244
export type ILanguagePacks = Record<string, ILanguagePack | undefined>;
245
246