mirror of
https://github.com/drawdb-io/drawdb.git
synced 2025-05-25 11:09:11 +00:00
Opening automatic browser language recognition for the first time
Save user use language Delete useless comments
This commit is contained in:
parent
266c0f9190
commit
cac4c8964a
@ -1151,19 +1151,16 @@ export default function ControlPanel({
|
||||
},
|
||||
};
|
||||
|
||||
// log menu Translate JSON
|
||||
let lostTranslateItem = {}
|
||||
// 翻译菜单
|
||||
function translateMenu(item) {
|
||||
lostTranslateItem = {}
|
||||
// let { t ,i18n} = useTranslation()
|
||||
// let { t } = useTranslation()
|
||||
// console.log("==> t ==>", t)
|
||||
let translateResult = {};
|
||||
let keys = Object.keys(item);
|
||||
for (const key of keys) {
|
||||
let key2 = t(`Menu.${key}.val`);
|
||||
let val = item[key];
|
||||
// console.log("翻译", key, key2);
|
||||
|
||||
//二级菜单
|
||||
if (typeof val === "object"){
|
||||
@ -1172,15 +1169,11 @@ export default function ControlPanel({
|
||||
|
||||
if(`Menu.${key}.val` === key2) {
|
||||
//翻译缺失
|
||||
// lostTranslateItem[key] = key;
|
||||
logLostTranslate(`Menu.${key}.val`, key)
|
||||
}else{
|
||||
logLostTranslate(`Menu.${key}.val`, key2)
|
||||
}
|
||||
|
||||
// if(key===key2)continue;
|
||||
// item[key2] = item[key];
|
||||
// delete item[key];
|
||||
translateResult[key2] = val
|
||||
}
|
||||
return translateResult
|
||||
@ -1191,16 +1184,18 @@ export default function ControlPanel({
|
||||
let key2 = t(`${mainMenu}.son.${key}.val`);
|
||||
let val = item[key];
|
||||
|
||||
// 子菜单
|
||||
let childrens = val.children;
|
||||
if(childrens){
|
||||
for (let i = 0; i <childrens.length; i++) {
|
||||
childrens[i] = translateSubMenu(childrens[i], `${mainMenu}.son.${key}`, t)
|
||||
if("Language" !== key){// 跳过,不翻译 切换语言 的子菜单
|
||||
// 子菜单
|
||||
let childrens = val.children;
|
||||
if(childrens){
|
||||
for (let i = 0; i <childrens.length; i++) {
|
||||
childrens[i] = translateSubMenu(childrens[i], `${mainMenu}.son.${key}`, t)
|
||||
}
|
||||
val.children = childrens
|
||||
}
|
||||
val.children = childrens
|
||||
}
|
||||
|
||||
//翻译缺失
|
||||
//翻译记录
|
||||
if(`${mainMenu}.son.${key}.val` === key2) {
|
||||
logLostTranslate(`${mainMenu}.son.${key}.val`, key)
|
||||
}else{
|
||||
@ -1212,35 +1207,6 @@ export default function ControlPanel({
|
||||
return translateResult
|
||||
}
|
||||
function logLostTranslate(path, key) {
|
||||
// console.log("记录Path", path, key)
|
||||
// const paths = path.split(/\./);
|
||||
// let data = lostTranslateItem[paths[0]];
|
||||
|
||||
// switch (paths.length) {
|
||||
// case 1:
|
||||
// lostTranslateItem[paths[0]] = key;
|
||||
// break;
|
||||
// case 2:
|
||||
// if(!lostTranslateItem[paths[0]]){lostTranslateItem[paths[0]]={};}
|
||||
// if(!lostTranslateItem[paths[0]][paths[1]]){lostTranslateItem[paths[0]][paths[1]]=key;}
|
||||
// break;
|
||||
// case 3:
|
||||
// if(!lostTranslateItem[paths[0]]){lostTranslateItem[paths[0]]={};}
|
||||
// if(!lostTranslateItem[paths[0]][paths[1]]){lostTranslateItem[paths[0]][paths[1]]={};}
|
||||
// if(!lostTranslateItem[paths[0]][paths[1]][paths[2]]){lostTranslateItem[paths[0]][paths[1]][paths[2]]=key;}
|
||||
// break;
|
||||
// case 4:
|
||||
// if(!lostTranslateItem[paths[0]]){lostTranslateItem[paths[0]]={};}
|
||||
// if(!lostTranslateItem[paths[0]][paths[1]]){lostTranslateItem[paths[0]][paths[1]]={};}
|
||||
// if(!lostTranslateItem[paths[0]][paths[1]][paths[2]]){lostTranslateItem[paths[0]][paths[1]][paths[2]]={};}
|
||||
// if(!lostTranslateItem[paths[0]][paths[1]][paths[2]][paths[3]]){lostTranslateItem[paths[0]][paths[1]][paths[2]][paths[3]]=key;}
|
||||
// break;
|
||||
// case 5:
|
||||
// break;
|
||||
// default:
|
||||
// console.log("超出设计的路径", path, paths)
|
||||
// }
|
||||
|
||||
lostTranslateItem = setVal(lostTranslateItem, path, key);
|
||||
}
|
||||
function setVal(obj, path, val) {
|
||||
@ -1271,8 +1237,7 @@ export default function ControlPanel({
|
||||
}
|
||||
menu = translateMenu(menu);
|
||||
// console.log("菜单>", menu)
|
||||
// console.log("菜单翻译", lostTranslateItem)
|
||||
// console.log("菜单翻译txt", JSON.stringify(lostTranslateItem))
|
||||
// console.log("菜单翻译", lostTranslateItem) // Perhaps useful when modifying menu items in the future
|
||||
|
||||
useHotkeys("ctrl+i, meta+i", fileImport, { preventDefault: true });
|
||||
useHotkeys("ctrl+z, meta+z", undo, { preventDefault: true });
|
||||
@ -1479,7 +1444,6 @@ export default function ControlPanel({
|
||||
menu[t("Menu.View.val")][t("Menu.View.son.Theme.val")].children[1][t("Menu.View.son.Theme.son.Dark.val")]();
|
||||
} else {
|
||||
menu[t("Menu.View.val")][t("Menu.View.son.Theme.val")].children[0][t("Menu.View.son.Theme.son.Light.val")]();
|
||||
// menu["View"]["Theme"].children[0]["Light"]();
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
@ -1,20 +1,27 @@
|
||||
import i18n from 'i18next';
|
||||
import Backend from 'i18next-http-backend';
|
||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||
import {initReactI18next, useTranslation} from 'react-i18next';
|
||||
import {initReactI18next} from 'react-i18next';
|
||||
|
||||
const supportedLngs = ['en-US', 'zh-CN']
|
||||
const translateItem = [
|
||||
{name:"English",file:"en-US"}
|
||||
,{name:"简体中文",file:"zh-CN"}
|
||||
];
|
||||
export const getTranslateItem = ()=> {
|
||||
let result = []
|
||||
let {i18n} = useTranslation();
|
||||
// const useLanguage = localStorage.getItem("useLanguage");
|
||||
for (const item of translateItem) {
|
||||
let lngItem = {}
|
||||
lngItem[item.name] = ()=>{
|
||||
let name = item.name;
|
||||
//if(item.file === useLanguage){
|
||||
if(item.file === i18n.language){
|
||||
name = "[✔]" + name
|
||||
}
|
||||
lngItem[name] = ()=>{
|
||||
i18n.changeLanguage(item.file);
|
||||
console.log("切换语言", item)
|
||||
// console.log("切换语言", item)
|
||||
localStorage.setItem("useLanguage", item.file)
|
||||
}
|
||||
result.push(lngItem)
|
||||
}
|
||||
@ -38,8 +45,8 @@ i18n
|
||||
useSuspense: false
|
||||
},
|
||||
// 设置默认语言
|
||||
lng: 'zh-CN',
|
||||
fallbackLng: 'zh-CN',
|
||||
// lng: 'en-US',
|
||||
fallbackLng: 'en-US',
|
||||
// 是否启用调试模式
|
||||
debug: true,
|
||||
//
|
||||
@ -50,22 +57,7 @@ i18n
|
||||
* @param lngs 语言编码
|
||||
* @param namespaces 名称空间
|
||||
*/
|
||||
// loadPath: function (lngs: Array<string>, namespaces: Array<string>) {
|
||||
// console.log(lngs, namespaces)
|
||||
// return `http://localhost:8000/locales/${lngs[0]}.json`;
|
||||
// },
|
||||
//loadPath: '/locales/{{lng}}/{{ns}}.json',
|
||||
loadPath: '/locales/{{lng}}.json',
|
||||
/**
|
||||
* 用于对响应的结果进行结构转化
|
||||
* @param data 原始响应的字符串结果
|
||||
*/
|
||||
// parse: function (data) {
|
||||
// console.log("i18n-parse", data)
|
||||
// const obj = eval("(" + data + ")");
|
||||
// return obj.resp;
|
||||
// },
|
||||
|
||||
/**
|
||||
* 是否允许跨域
|
||||
*/
|
||||
@ -82,22 +74,17 @@ i18n
|
||||
interpolation: {
|
||||
escapeValue: false, // not needed for react as it escapes by default
|
||||
},
|
||||
|
||||
// resources: {
|
||||
// "en-US": {
|
||||
// translation: {
|
||||
// File: "File文件"
|
||||
// }
|
||||
// },
|
||||
// "zh-CN": {
|
||||
// translation: {
|
||||
// File: "文件"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
supportedLngs: supportedLngs
|
||||
}, function (err, t) {
|
||||
// i18n插件初始化完成或异常时的回调函数
|
||||
console.log('国际化插件初始化完毕!', err, t)
|
||||
//auto load last select Language
|
||||
console.log(">>>", this, i18n)
|
||||
if(!err && localStorage.getItem("useLanguage")){
|
||||
// let {i18n} = useTranslation();
|
||||
i18n.changeLanguage(localStorage.getItem("useLanguage"));
|
||||
// i18n.language = localStorage.getItem("useLanguage")
|
||||
console.log("auto load last select Language")
|
||||
}
|
||||
});
|
||||
export default i18n;
|
Loading…
Reference in New Issue
Block a user