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.
53 lines
1.1 KiB
53 lines
1.1 KiB
import chinese from './chinese'
|
|
|
|
const TRADITIONAL = chinese.Traditional //繁体
|
|
const SIMPLIFIED = chinese.Simplified //简体
|
|
|
|
export const chineseLanguageLoader = (loaderContext: string, options?: any) => {
|
|
const modulesOptions = {
|
|
language: 'zh-TW'
|
|
}
|
|
if (options && options.language && options.language === 'zh-CN') {
|
|
modulesOptions.language = 'zh-CN'
|
|
}
|
|
return _transitionStr(loaderContext, modulesOptions.language)
|
|
}
|
|
|
|
function _transitionStr(str: string, language: string) {
|
|
let i,
|
|
letter,
|
|
code,
|
|
isChinese,
|
|
index,
|
|
src,
|
|
des,
|
|
result = ''
|
|
if (language === 'zh-TW') {
|
|
src = SIMPLIFIED
|
|
des = TRADITIONAL
|
|
} else {
|
|
src = TRADITIONAL
|
|
des = SIMPLIFIED
|
|
}
|
|
if (typeof str !== 'string') {
|
|
return str
|
|
}
|
|
for (i = 0; i < str.length; i++) {
|
|
letter = str.charAt(i)
|
|
code = str.charCodeAt(i)
|
|
isChinese = (code > 0x3400 && code < 0x9fc3) || (code > 0xf900 && code < 0xfa6a)
|
|
if (!isChinese) {
|
|
result += letter
|
|
continue
|
|
}
|
|
index = src.indexOf(letter)
|
|
|
|
if (index !== -1) {
|
|
result += des.charAt(index)
|
|
} else {
|
|
result += letter
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|