You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/ui-default/components/monaco/nls.js

52 lines
1.3 KiB
JavaScript

/* eslint-disable camelcase */
import en_GB from 'monaco-editor-nls/locale/en-gb.json';
function format(message, args) {
let result;
if (args.length === 0) {
result = message;
} else {
result = String(message).replace(/\{(\d+)\}/g, (match, rest) => {
const index = rest[0];
return typeof args[index] !== 'undefined' ? args[index] : match;
});
}
return result;
}
export const getConfiguredDefaultLocale = () => 'zh';
let CURRENT_LOCALE_DATA = {}; // eslint-disable-line @typescript-eslint/naming-convention
function find(path, message) {
for (const key of Object.keys(CURRENT_LOCALE_DATA)) {
if (!CURRENT_LOCALE_DATA[key] || !en_GB[key]) continue;
if (CURRENT_LOCALE_DATA[key][path] && en_GB[key][path] === message) {
return CURRENT_LOCALE_DATA[key][path];
}
}
for (const key of Object.keys(CURRENT_LOCALE_DATA)) {
if (!CURRENT_LOCALE_DATA[key]) continue;
if (CURRENT_LOCALE_DATA[key][path]) {
return CURRENT_LOCALE_DATA[key][path];
}
}
return message;
}
export function localize(path, message, ...args) {
return format(find(path.key || path, message), args);
}
export function setLocaleData(data) {
CURRENT_LOCALE_DATA = data;
}
export function loadMessageBundle() {
return localize;
}
export function config() {
return loadMessageBundle;
}