/*---------------------------------------------------------------------------------------------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*--------------------------------------------------------------------------------------------*/45// eslint-disable-next-line local/code-import-patterns6import { getNLSLanguage, getNLSMessages } from './nls.messages.js';7// eslint-disable-next-line local/code-import-patterns8export { getNLSLanguage, getNLSMessages } from './nls.messages.js';910declare const document: { location?: { hash?: string } } | undefined;11const isPseudo = getNLSLanguage() === 'pseudo' || (typeof document !== 'undefined' && document.location && typeof document.location.hash === 'string' && document.location.hash.indexOf('pseudo=true') >= 0);1213export interface ILocalizeInfo {14key: string;15comment: string[];16}1718export interface ILocalizedString {19original: string;20value: string;21}2223function _format(message: string, args: (string | number | boolean | undefined | null)[]): string {24let result: string;2526if (args.length === 0) {27result = message;28} else {29result = message.replace(/\{(\d+)\}/g, (match, rest) => {30const index = rest[0];31const arg = args[index];32let result = match;33if (typeof arg === 'string') {34result = arg;35} else if (typeof arg === 'number' || typeof arg === 'boolean' || arg === void 0 || arg === null) {36result = String(arg);37}38return result;39});40}4142if (isPseudo) {43// FF3B and FF3D is the Unicode zenkaku representation for [ and ]44result = '\uFF3B' + result.replace(/[aouei]/g, '$&$&') + '\uFF3D';45}4647return result;48}4950/**51* Marks a string to be localized. Returns the localized string.52*53* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.54* @param message The string to localize55* @param args The arguments to the string56*57* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`58* @example `localize({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`59*60* @returns string The localized string.61*/62export function localize(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): string;6364/**65* Marks a string to be localized. Returns the localized string.66*67* @param key The key to use for localizing the string68* @param message The string to localize69* @param args The arguments to the string70*71* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`72* @example For example, `localize('sayHello', 'hello {0}', name)`73*74* @returns string The localized string.75*/76export function localize(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): string;7778/**79* @skipMangle80*/81export function localize(data: ILocalizeInfo | string /* | number when built */, message: string /* | null when built */, ...args: (string | number | boolean | undefined | null)[]): string {82if (typeof data === 'number') {83return _format(lookupMessage(data, message), args);84}85return _format(message, args);86}8788/**89* Only used when built: Looks up the message in the global NLS table.90* This table is being made available as a global through bootstrapping91* depending on the target context.92*/93function lookupMessage(index: number, fallback: string | null): string {94const message = getNLSMessages()?.[index];95if (typeof message !== 'string') {96if (typeof fallback === 'string') {97return fallback;98}99throw new Error(`!!! NLS MISSING: ${index} !!!`);100}101return message;102}103104/**105* Marks a string to be localized. Returns an {@linkcode ILocalizedString}106* which contains the localized string and the original string.107*108* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.109* @param message The string to localize110* @param args The arguments to the string111*112* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`113* @example `localize2({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`114*115* @returns ILocalizedString which contains the localized string and the original string.116*/117export function localize2(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;118119/**120* Marks a string to be localized. Returns an {@linkcode ILocalizedString}121* which contains the localized string and the original string.122*123* @param key The key to use for localizing the string124* @param message The string to localize125* @param args The arguments to the string126*127* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`128* @example `localize('sayHello', 'hello {0}', name)`129*130* @returns ILocalizedString which contains the localized string and the original string.131*/132export function localize2(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;133134/**135* @skipMangle136*/137export function localize2(data: ILocalizeInfo | string /* | number when built */, originalMessage: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString {138let message: string;139if (typeof data === 'number') {140message = lookupMessage(data, originalMessage);141} else {142message = originalMessage;143}144145const value = _format(message, args);146147return {148value,149original: originalMessage === message ? value : _format(originalMessage, args)150};151}152153export interface INLSLanguagePackConfiguration {154155/**156* The path to the translations config file that contains pointers to157* all message bundles for `main` and extensions.158*/159readonly translationsConfigFile: string;160161/**162* The path to the file containing the translations for this language163* pack as flat string array.164*/165readonly messagesFile: string;166167/**168* The path to the file that can be used to signal a corrupt language169* pack, for example when reading the `messagesFile` fails. This will170* instruct the application to re-create the cache on next startup.171*/172readonly corruptMarkerFile: string;173}174175export interface INLSConfiguration {176177/**178* Locale as defined in `argv.json` or `app.getLocale()`.179*/180readonly userLocale: string;181182/**183* Locale as defined by the OS (e.g. `app.getPreferredSystemLanguages()`).184*/185readonly osLocale: string;186187/**188* The actual language of the UI that ends up being used considering `userLocale`189* and `osLocale`.190*/191readonly resolvedLanguage: string;192193/**194* Defined if a language pack is used that is not the195* default english language pack. This requires a language196* pack to be installed as extension.197*/198readonly languagePack?: INLSLanguagePackConfiguration;199200/**201* The path to the file containing the default english messages202* as flat string array. The file is only present in built203* versions of the application.204*/205readonly defaultMessagesFile: string;206207/**208* Below properties are deprecated and only there to continue support209* for `vscode-nls` module that depends on them.210* Refs https://github.com/microsoft/vscode-nls/blob/main/src/node/main.ts#L36-L46211*/212/** @deprecated */213readonly locale: string;214/** @deprecated */215readonly availableLanguages: Record<string, string>;216/** @deprecated */217readonly _languagePackSupport?: boolean;218/** @deprecated */219readonly _languagePackId?: string;220/** @deprecated */221readonly _translationsConfigFile?: string;222/** @deprecated */223readonly _cacheRoot?: string;224/** @deprecated */225readonly _resolvedLanguagePackCoreLocation?: string;226/** @deprecated */227readonly _corruptedFile?: string;228}229230export interface ILanguagePack {231readonly hash: string;232readonly label: string | undefined;233readonly extensions: {234readonly extensionIdentifier: { readonly id: string; readonly uuid?: string };235readonly version: string;236}[];237readonly translations: Record<string, string | undefined>;238}239240export type ILanguagePacks = Record<string, ILanguagePack | undefined>;241242243