You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
import { extend } from "../2.base"; |
|
import { replaceAll } from "../func"; |
|
|
|
let i18nStore = {}; |
|
const i18nFormatters = {}; |
|
|
|
export function changeI18n(i18n) { |
|
if (i18n) { |
|
i18nStore = i18n; |
|
} |
|
} |
|
|
|
export function addI18n(i18n) { |
|
extend(i18nStore, i18n); |
|
} |
|
|
|
export function i18nText(key) { |
|
let localeText = i18nStore[key] || (BI.i18n && BI.i18n[key]) || ""; |
|
if (!localeText) { |
|
localeText = key; |
|
} |
|
const len = arguments.length; |
|
if (len > 1) { |
|
if (localeText.indexOf("{R1") > -1) { |
|
for (let i = 1; i < len; i++) { |
|
const reg = new RegExp(`{R${i},(.*?)}`, "g"); |
|
|
|
const result = reg.exec(localeText); |
|
|
|
if (result) { |
|
const formatName = result[1]; |
|
localeText = replaceAll(localeText, reg, i18nFormatters[formatName](key, arguments[i])); |
|
} else { |
|
localeText = replaceAll(localeText, `{R${i}}`, `${arguments[i]}`); |
|
} |
|
} |
|
} else { |
|
const args = Array.prototype.slice.call(arguments); |
|
let count = 1; |
|
|
|
return replaceAll(localeText, "\\{\\s*\\}", () => `${args[count++]}`); |
|
} |
|
} |
|
|
|
return localeText; |
|
} |
|
|
|
export function addI18nFormatter(formatName, fn) { |
|
i18nFormatters[formatName] = fn; |
|
}
|
|
|