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