Path: blob/main/extensions/copilot/src/util/vs/nls.ts
13397 views
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'12/*---------------------------------------------------------------------------------------------3* Copyright (c) Microsoft Corporation. All rights reserved.4* Licensed under the MIT License. See License.txt in the project root for license information.5*--------------------------------------------------------------------------------------------*/67export function getNLSMessages(): string[] {8return globalThis._VSCODE_NLS_MESSAGES;9}1011export function getNLSLanguage(): string | undefined {12return globalThis._VSCODE_NLS_LANGUAGE;13}1415declare const document: { location?: { hash?: string } } | undefined;16const isPseudo = getNLSLanguage() === 'pseudo' || (typeof document !== 'undefined' && document.location && typeof document.location.hash === 'string' && document.location.hash.indexOf('pseudo=true') >= 0);1718export interface ILocalizeInfo {19key: string;20comment: string[];21}2223export interface ILocalizedString {24original: string;25value: string;26}2728function _format(message: string, args: (string | number | boolean | undefined | null)[]): string {29let result: string;3031if (args.length === 0) {32result = message;33} else {34result = message.replace(/\{(\d+)\}/g, (match, rest) => {35const index = rest[0];36const arg = args[index];37let result = match;38if (typeof arg === 'string') {39result = arg;40} else if (typeof arg === 'number' || typeof arg === 'boolean' || arg === void 0 || arg === null) {41result = String(arg);42}43return result;44});45}4647if (isPseudo) {48// FF3B and FF3D is the Unicode zenkaku representation for [ and ]49result = '\uFF3B' + result.replace(/[aouei]/g, '$&$&') + '\uFF3D';50}5152return result;53}5455/**56* Marks a string to be localized. Returns the localized string.57*58* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.59* @param message The string to localize60* @param args The arguments to the string61*62* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`63* @example `localize({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`64*65* @returns string The localized string.66*/67export function localize(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): string;6869/**70* Marks a string to be localized. Returns the localized string.71*72* @param key The key to use for localizing the string73* @param message The string to localize74* @param args The arguments to the string75*76* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`77* @example For example, `localize('sayHello', 'hello {0}', name)`78*79* @returns string The localized string.80*/81export function localize(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): string;8283/**84* @skipMangle85*/86export function localize(data: ILocalizeInfo | string /* | number when built */, message: string /* | null when built */, ...args: (string | number | boolean | undefined | null)[]): string {87if (typeof data === 'number') {88return _format(lookupMessage(data, message), args);89}90return _format(message, args);91}9293/**94* Only used when built: Looks up the message in the global NLS table.95* This table is being made available as a global through bootstrapping96* depending on the target context.97*/98function lookupMessage(index: number, fallback: string | null): string {99const message = getNLSMessages()?.[index];100if (typeof message !== 'string') {101if (typeof fallback === 'string') {102return fallback;103}104throw new Error(`!!! NLS MISSING: ${index} !!!`);105}106return message;107}108109/**110* Marks a string to be localized. Returns an {@linkcode ILocalizedString}111* which contains the localized string and the original string.112*113* @param info The {@linkcode ILocalizeInfo} which describes the id and comments associated with the localized string.114* @param message The string to localize115* @param args The arguments to the string116*117* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`118* @example `localize2({ key: 'sayHello', comment: ['Welcomes user'] }, 'hello {0}', name)`119*120* @returns ILocalizedString which contains the localized string and the original string.121*/122export function localize2(info: ILocalizeInfo, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;123124/**125* Marks a string to be localized. Returns an {@linkcode ILocalizedString}126* which contains the localized string and the original string.127*128* @param key The key to use for localizing the string129* @param message The string to localize130* @param args The arguments to the string131*132* @note `message` can contain `{n}` notation where it is replaced by the nth value in `...args`133* @example `localize('sayHello', 'hello {0}', name)`134*135* @returns ILocalizedString which contains the localized string and the original string.136*/137export function localize2(key: string, message: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString;138139/**140* @skipMangle141*/142export function localize2(data: ILocalizeInfo | string /* | number when built */, originalMessage: string, ...args: (string | number | boolean | undefined | null)[]): ILocalizedString {143let message: string;144if (typeof data === 'number') {145message = lookupMessage(data, originalMessage);146} else {147message = originalMessage;148}149150const value = _format(message, args);151152return {153value,154original: originalMessage === message ? value : _format(originalMessage, args)155};156}157158export interface INLSLanguagePackConfiguration {159160/**161* The path to the translations config file that contains pointers to162* all message bundles for `main` and extensions.163*/164readonly translationsConfigFile: string;165166/**167* The path to the file containing the translations for this language168* pack as flat string array.169*/170readonly messagesFile: string;171172/**173* The path to the file that can be used to signal a corrupt language174* pack, for example when reading the `messagesFile` fails. This will175* instruct the application to re-create the cache on next startup.176*/177readonly corruptMarkerFile: string;178}179180export interface INLSConfiguration {181182/**183* Locale as defined in `argv.json` or `app.getLocale()`.184*/185readonly userLocale: string;186187/**188* Locale as defined by the OS (e.g. `app.getPreferredSystemLanguages()`).189*/190readonly osLocale: string;191192/**193* The actual language of the UI that ends up being used considering `userLocale`194* and `osLocale`.195*/196readonly resolvedLanguage: string;197198/**199* Defined if a language pack is used that is not the200* default english language pack. This requires a language201* pack to be installed as extension.202*/203readonly languagePack?: INLSLanguagePackConfiguration;204205/**206* The path to the file containing the default english messages207* as flat string array. The file is only present in built208* versions of the application.209*/210readonly defaultMessagesFile: string;211212/**213* Below properties are deprecated and only there to continue support214* for `vscode-nls` module that depends on them.215* Refs https://github.com/microsoft/vscode-nls/blob/main/src/node/main.ts#L36-L46216*/217/** @deprecated */218readonly locale: string;219/** @deprecated */220readonly availableLanguages: Record<string, string>;221/** @deprecated */222readonly _languagePackSupport?: boolean;223/** @deprecated */224readonly _languagePackId?: string;225/** @deprecated */226readonly _translationsConfigFile?: string;227/** @deprecated */228readonly _cacheRoot?: string;229/** @deprecated */230readonly _resolvedLanguagePackCoreLocation?: string;231/** @deprecated */232readonly _corruptedFile?: string;233}234235export interface ILanguagePack {236readonly hash: string;237readonly label: string | undefined;238readonly extensions: {239readonly extensionIdentifier: { readonly id: string; readonly uuid?: string };240readonly version: string;241}[];242readonly translations: Record<string, string | undefined>;243}244245export type ILanguagePacks = Record<string, ILanguagePack | undefined>;246247248