monaco-editor/build/fillers/vscode-nls.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-06-23 02:53:52 +08:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
2017-07-03 20:23:26 +08:00
export interface Options {
locale?: string;
cacheLanguageResolution?: boolean;
}
export interface LocalizeInfo {
key: string;
comment: string[];
}
export interface LocalizeFunc {
(info: LocalizeInfo, message: string, ...args: any[]): string;
(key: string, message: string, ...args: any[]): string;
}
export interface LoadFunc {
(file?: string): LocalizeFunc;
}
2016-06-23 02:53:52 +08:00
function format(message: string, args: any[]): string {
2019-12-17 17:51:39 +08:00
let result: string;
2016-06-23 02:53:52 +08:00
if (args.length === 0) {
result = message;
} else {
result = message.replace(/\{(\d+)\}/g, (match, rest) => {
let index = rest[0];
return typeof args[index] !== 'undefined' ? args[index] : match;
});
}
return result;
}
2020-09-19 07:42:59 +08:00
function localize(key: string | LocalizeInfo, message: string, ...args: any[]): string {
2016-06-23 02:53:52 +08:00
return format(message, args);
}
export function loadMessageBundle(file?: string): LocalizeFunc {
2017-07-03 20:23:26 +08:00
return localize;
2016-06-23 02:53:52 +08:00
}
export function config(opt?: Options | string): LoadFunc {
2017-07-03 20:23:26 +08:00
return loadMessageBundle;
2020-09-07 21:48:33 +08:00
}