/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45export function getNLSMessages(): string[] {6return globalThis._VSCODE_NLS_MESSAGES;7}89export function getNLSLanguage(): string | undefined {10return globalThis._VSCODE_NLS_LANGUAGE;11}1213declare const document: { location?: { hash?: string } } | undefined;14const isPseudo = getNLSLanguage() === 'pseudo' || (typeof document !== 'undefined' && document.location && typeof document.location.hash === 'string' && document.location.hash.indexOf('pseudo=true') >= 0);1516export interface ILocalizeInfo {17key: string;18comment: string[];19}2021export interface ILocalizedString {22original: string;23value: string;24}2526function _format(message: string, args: (string | number | boolean | undefined | null)[]): string {27let result: string;2829if (args.length === 0) {30result = message;31} else {32result = message.replace(/\{(\d+)\}/g, (match, rest) => {33const index = rest[0];34const arg = args[index];35let result = match;36if (typeof arg === 'string') {37result = arg;38} else if (typeof arg === 'number' || typeof arg === 'boolean' || arg === void 0 || arg === null) {39result = String(arg);40}41return result;42});43}4445if (isPseudo) {46// FF3B and FF3D is the Unicode zenkaku representation for [ and ]47result = '\uFF3B' + result.replace(/[aouei]/g, '$&$&') + '\uFF3D';48}4950return result;51}5253/**54* Marks a string to be localized. Returns the localized string.55*56* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.57* @param message The string to localize58* @param args The arguments to the string59*60* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`61* @example `localize({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`62*63* @returns string The localized string.64*/65export function localize(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): string;6667/**68* Marks a string to be localized. Returns the localized string.69*70* @param key The key to use for localizing the string71* @param message The string to localize72* @param args The arguments to the string73*74* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`75* @example For example, `localize('sayHello', 'hello {0}', name)`76*77* @returns string The localized string.78*/79export function localize(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): string;8081/**82* @skipMangle83*/84export function localize(data: ILocalizeInfo | string /* | number when built */, message: string /* | null when built */, ...args: (string | number | boolean | undefined | null)[]): string {85if (typeof data === 'number') {86return _format(lookupMessage(data, message), args);87}88return _format(message, args);89}9091/**92* Only used when built: Looks up the message in the global NLS table.93* This table is being made available as a global through bootstrapping94* depending on the target context.95*/96function lookupMessage(index: number, fallback: string | null): string {97const message = getNLSMessages()?.[index];98if (typeof message !== 'string') {99if (typeof fallback === 'string') {100return fallback;101}102throw new Error(`!!! NLS MISSING: ${index} !!!`);103}104return message;105}106107/**108* Marks a string to be localized. Returns an {@linkcode ILocalizedString}109* which contains the localized string and the original string.110*111* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.112* @param message The string to localize113* @param args The arguments to the string114*115* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`116* @example `localize2({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`117*118* @returns ILocalizedString which contains the localized string and the original string.119*/120export function localize2(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;121122/**123* Marks a string to be localized. Returns an {@linkcode ILocalizedString}124* which contains the localized string and the original string.125*126* @param key The key to use for localizing the string127* @param message The string to localize128* @param args The arguments to the string129*130* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`131* @example `localize('sayHello', 'hello {0}', name)`132*133* @returns ILocalizedString which contains the localized string and the original string.134*/135export function localize2(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;136137/**138* @skipMangle139*/140export function localize2(data: ILocalizeInfo | string /* | number when built */, originalMessage: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString {141let message: string;142if (typeof data === 'number') {143message = lookupMessage(data, originalMessage);144} else {145message = originalMessage;146}147148const value = _format(message, args);149150return {151value,152original: originalMessage === message ? value : _format(originalMessage, args)153};154}155156export interface INLSLanguagePackConfiguration {157158/**159* The path to the translations config file that contains pointers to160* all message bundles for `main` and extensions.161*/162readonly translationsConfigFile: string;163164/**165* The path to the file containing the translations for this language166* pack as flat string array.167*/168readonly messagesFile: string;169170/**171* The path to the file that can be used to signal a corrupt language172* pack, for example when reading the `messagesFile` fails. This will173* instruct the application to re-create the cache on next startup.174*/175readonly corruptMarkerFile: string;176}177178export interface INLSConfiguration {179180/**181* Locale as defined in `argv.json` or `app.getLocale()`.182*/183readonly userLocale: string;184185/**186* Locale as defined by the OS (e.g. `app.getPreferredSystemLanguages()`).187*/188readonly osLocale: string;189190/**191* The actual language of the UI that ends up being used considering `userLocale`192* and `osLocale`.193*/194readonly resolvedLanguage: string;195196/**197* Defined if a language pack is used that is not the198* default english language pack. This requires a language199* pack to be installed as extension.200*/201readonly languagePack?: INLSLanguagePackConfiguration;202203/**204* The path to the file containing the default english messages205* as flat string array. The file is only present in built206* versions of the application.207*/208readonly defaultMessagesFile: string;209210/**211* Below properties are deprecated and only there to continue support212* for `vscode-nls` module that depends on them.213* Refs https://github.com/microsoft/vscode-nls/blob/main/src/node/main.ts#L36-L46214*/215/** @deprecated */216readonly locale: string;217/** @deprecated */218readonly availableLanguages: Record<string, string>;219/** @deprecated */220readonly _languagePackSupport?: boolean;221/** @deprecated */222readonly _languagePackId?: string;223/** @deprecated */224readonly _translationsConfigFile?: string;225/** @deprecated */226readonly _cacheRoot?: string;227/** @deprecated */228readonly _resolvedLanguagePackCoreLocation?: string;229/** @deprecated */230readonly _corruptedFile?: string;231}232233export interface ILanguagePack {234readonly hash: string;235readonly label: string | undefined;236readonly extensions: {237readonly extensionIdentifier: { readonly id: string; readonly uuid?: string };238readonly version: string;239}[];240readonly translations: Record<string, string | undefined>;241}242243export type ILanguagePacks = Record<string, ILanguagePack | undefined>;244245246