Path: blob/main/extensions/copilot/src/util/common/test/shims/l10n.ts
13405 views
/*---------------------------------------------------------------------------------------------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 t(message: string, args: Record<string, any>): string;6export function t(message: string, ...args: Array<string | number | boolean>): string;7export function t(options: { message: string; args?: Array<string | number | boolean> | Record<string, any>; comment: string | string[] }): string;8export function t(...params: [message: string, ...args: Array<string | number | boolean>] | [message: string, args: Record<string, any>] | [{ message: string; args?: Array<string | number | boolean> | Record<string, any>; comment: string | string[] }]): string {9if (typeof params[0] === 'string') {10const key = params.shift() as string;1112// We have either rest args which are Array<string | number | boolean> or an array with a single Record<string, any>.13// This ensures we get a Record<string | number, any> which will be formatted correctly.14const argsFormatted = !params || typeof params[0] !== 'object' ? params : params[0];15return getMessage({ message: key, args: argsFormatted as Record<string | number, any> | undefined });16}1718return getMessage(params[0]);19}2021interface IStringDetails {22message: string;23args?: Record<string | number, any>;24comment?: string | string[];25}2627function getMessage(details: IStringDetails): string {28const { message, args } = details;29return format2(message, (args ?? {}));30}3132const _format2Regexp = /{([^}]+)}/g;3334function format2(template: string, values: Record<string, unknown>): string {35return template.replace(_format2Regexp, (match, group) => (values[group] ?? match) as string);36}373839