Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/test/shims/l10n.ts
13405 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
export function t(message: string, args: Record<string, any>): string;
7
export function t(message: string, ...args: Array<string | number | boolean>): string;
8
export function t(options: { message: string; args?: Array<string | number | boolean> | Record<string, any>; comment: string | string[] }): string;
9
export 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 {
10
if (typeof params[0] === 'string') {
11
const key = params.shift() as string;
12
13
// We have either rest args which are Array<string | number | boolean> or an array with a single Record<string, any>.
14
// This ensures we get a Record<string | number, any> which will be formatted correctly.
15
const argsFormatted = !params || typeof params[0] !== 'object' ? params : params[0];
16
return getMessage({ message: key, args: argsFormatted as Record<string | number, any> | undefined });
17
}
18
19
return getMessage(params[0]);
20
}
21
22
interface IStringDetails {
23
message: string;
24
args?: Record<string | number, any>;
25
comment?: string | string[];
26
}
27
28
function getMessage(details: IStringDetails): string {
29
const { message, args } = details;
30
return format2(message, (args ?? {}));
31
}
32
33
const _format2Regexp = /{([^}]+)}/g;
34
35
function format2(template: string, values: Record<string, unknown>): string {
36
return template.replace(_format2Regexp, (match, group) => (values[group] ?? match) as string);
37
}
38
39