语言资源工具类,用以设置页面以及脚本中的多语言。
function r(key: string, defaultValue?: any): any
语言资源的关键字
资源的默认值,如无法获取该语言资源,则返回该值
const lang : {}
返回当前语言 id
未知错误的语言资源,默认为 'An unknown error occurred, please contact the administrator.'
保存成功的语言资源,默认为 'Saved successfully.'
function init(dom?: HTMLElement, options?: LgresOptions): Promise<LanguageResource>
待处理的元素,为空时处理整个页面
初始化参数,结构为
interface LgresOptions {
template?: string,
callback?: (result: any) => void
}
语言资源文件的后缀,资源文件 url 为 `language/${lgid}${template}`
资源初始化后的回调函数,可能在 DOM 加载完成之前触发。
返回一个包含资源结果的 Promise,将在 DOM 加载完成之后触发。
LanguageResource 结构为
interface LanguageResource {
r(key: string, defaultValue?: any): any
}
const lgres = window["lib-utility"];
lgres.init(document.body, {
template: '/res.json',
callback: res => document.title = res.r('title', 'Default Title')
}).then(res => {
document.querySelector('#header').innerText = res.r('header', 'My Header');
const msg = lgres.lang.unknownError;
document.querySelector('#message').innerText = lgres.lang.unknownError;
});