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