From 1b468b8454ddf0edf95316e6237c76405209a218 Mon Sep 17 00:00:00 2001 From: kkzzhizhou <806508401@qq.com> Date: Sun, 21 Nov 2021 22:13:19 +0800 Subject: [PATCH] add i18n support --- .gitignore | 1 - .hotreload | 0 package.json | 5 +++- src/i18n.tsx | 27 +++++++++++++++++++ src/i18n/en-us.json | 10 +++++++ src/i18n/zh-cn.json | 10 +++++++ src/main.ts | 2 +- src/view/App.tsx | 3 ++- src/view/Components/Statsview/index.tsx | 8 +++--- .../Components/Taskview/Dailiesview/index.tsx | 7 ++--- .../Components/Taskview/Habitsview/index.tsx | 3 ++- .../Components/Taskview/Rewardview/index.tsx | 3 ++- .../Components/Taskview/Todoview/TodoItem.tsx | 1 - .../Components/Taskview/Todoview/index.tsx | 5 ++-- styles.css | 1 - tsconfig.json | 2 ++ 16 files changed, 72 insertions(+), 16 deletions(-) delete mode 100644 .hotreload create mode 100644 src/i18n.tsx create mode 100644 src/i18n/en-us.json create mode 100644 src/i18n/zh-cn.json diff --git a/.gitignore b/.gitignore index de7c3d1..5ae3397 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,6 @@ package-lock.json # yarn yarn.lock - *.js.map # obsidian diff --git a/.hotreload b/.hotreload deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index 174f5f6..8ca6194 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "dev": "rollup --config rollup.config.js -w", "build": "rollup --config rollup.config.js --environment BUILD:production", - "dev2": "obsidian-plugin dev src/main.ts" + "test": "obsidian-plugin dev src/main.ts" }, "keywords": [], "author": "Leonard and Ran", @@ -29,11 +29,14 @@ "typescript": "^4.2.4" }, "dependencies": { + "i18next": "^21.5.2", + "i18next-browser-languagedetector": "^6.1.2", "node": "^16.10.0", "node-fetch": "^3.0.0", "react": "^17.0.2", "react-dom": "^17.0.2", "react-emoji-render": "^1.2.4", + "react-i18next": "^11.14.2", "react-markdown": "^7.1.0", "react-tabs": "^3.2.2" } diff --git a/src/i18n.tsx b/src/i18n.tsx new file mode 100644 index 0000000..4abc913 --- /dev/null +++ b/src/i18n.tsx @@ -0,0 +1,27 @@ +import i18n from "i18next"; +import enUsTrans from "./i18n/en-us.json"; +import zhCnTrans from "./i18n/zh-cn.json"; +import { initReactI18next } from 'react-i18next'; + +const lang = window.localStorage.getItem('language'); +// console.log(lang) + +i18n.use(initReactI18next) +.init({ + //资源文件 + resources: { + en: { + translation: enUsTrans, + }, + zh: { + translation: zhCnTrans, + }, + }, + fallbackLng: lang, + debug: false, + interpolation: { + escapeValue: false, + }, +}) + +export default i18n; \ No newline at end of file diff --git a/src/i18n/en-us.json b/src/i18n/en-us.json new file mode 100644 index 0000000..1b720cc --- /dev/null +++ b/src/i18n/en-us.json @@ -0,0 +1,10 @@ +{ + "Active":"Active", + "Completed":"Completed", + "HP":"HP", + "LEVEL":"LEVEL", + "GOLD":"GOLD", + "No Dailies Present":"No Dailies Present", + "No habits present.":"No habits present.", + "No Rewards present.":"No Rewards present." +} \ No newline at end of file diff --git a/src/i18n/zh-cn.json b/src/i18n/zh-cn.json new file mode 100644 index 0000000..796318b --- /dev/null +++ b/src/i18n/zh-cn.json @@ -0,0 +1,10 @@ +{ + "Active":"进行中", + "Completed":"已完成", + "HP":"生命值", + "LEVEL":"等级", + "GOLD":"金币", + "No Dailies Present":"目前每日任务为空", + "No habits present.":"目前习惯为空", + "No Rewards present.":"目前奖励为空" +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 7fdc89b..7c0d817 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,7 +15,7 @@ export default class HabiticaSync extends Plugin { view: HabiticaSyncView; async onload() { - console.log("load plugin: habitica-sync") + // console.log("load plugin: habitica-sync") await this.loadSettings(); this.addSettingTab(new HabiticaSyncSettingsTab(this.app, this)); this.registerView( diff --git a/src/view/App.tsx b/src/view/App.tsx index 36879c9..eb9af90 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -3,6 +3,7 @@ import { Notice } from "obsidian"; import { getStats, scoreTask, makeCronReq, costReward } from "./habiticaAPI" import Statsview from "./Components/Statsview" import Taskview from "./Components/Taskview" +import "../i18n" class App extends React.Component { private _username = ""; @@ -56,7 +57,7 @@ class App extends React.Component { //
Welcome back! Please check your tasks for the last day and hit continue to get your daily rewards.
// // - // ); + // ); return (
) diff --git a/src/view/Components/Statsview/index.tsx b/src/view/Components/Statsview/index.tsx index c84d10e..8335466 100644 --- a/src/view/Components/Statsview/index.tsx +++ b/src/view/Components/Statsview/index.tsx @@ -1,13 +1,15 @@ import * as React from 'react'; +import { useTranslation, Trans, Translation } from 'react-i18next' export default function Index(props: any) { + let { t ,i18n} = useTranslation() return(
{/*
{props.user_data.profile.name}
*/} {console.log(props)} -
HP: {(props.user_data.stats.hp).toPrecision(3)}
-
LEVEL: {props.user_data.stats.lvl}
-
GOLD: {(props.user_data.stats.gp).toPrecision(3)}
+
{t('HP')}: {(props.user_data.stats.hp).toPrecision(3)}
+
{t('LEVEL')}: {props.user_data.stats.lvl}
+
{t('GOLD')}: {(props.user_data.stats.gp).toPrecision(3)}
); } \ No newline at end of file diff --git a/src/view/Components/Taskview/Dailiesview/index.tsx b/src/view/Components/Taskview/Dailiesview/index.tsx index e45c421..e19728c 100644 --- a/src/view/Components/Taskview/Dailiesview/index.tsx +++ b/src/view/Components/Taskview/Dailiesview/index.tsx @@ -1,10 +1,11 @@ import * as React from "react"; import DailyItem from "./DailyItem" import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; +import { useTranslation, Trans, Translation } from 'react-i18next' export default function Index(props: any){ if(props.dailys == undefined) { - return
No Dailies Present
+ return
No Dailies Present
} else { const incompleteDailies = props.dailys.map((daily: any) => { @@ -18,8 +19,8 @@ export default function Index(props: any){ const display =
- Active - Completed + Active + Completed
    {incompleteDailies}
diff --git a/src/view/Components/Taskview/Habitsview/index.tsx b/src/view/Components/Taskview/Habitsview/index.tsx index 1ee9eab..07d5549 100644 --- a/src/view/Components/Taskview/Habitsview/index.tsx +++ b/src/view/Components/Taskview/Habitsview/index.tsx @@ -1,10 +1,11 @@ import * as React from "react"; import HabitItem from "./HabitItem" +import { useTranslation, Trans, Translation } from 'react-i18next' export default function Index(props: any){ if(props.habits == undefined) { return (
- No habits present. + No habits present.
) } else { diff --git a/src/view/Components/Taskview/Rewardview/index.tsx b/src/view/Components/Taskview/Rewardview/index.tsx index ff60917..f6ae497 100644 --- a/src/view/Components/Taskview/Rewardview/index.tsx +++ b/src/view/Components/Taskview/Rewardview/index.tsx @@ -1,10 +1,11 @@ import * as React from "react"; import RewardItem from "./RewardItem" +import { useTranslation, Trans, Translation } from 'react-i18next' export default function Index(props: any){ if(props.rewards == undefined) { return (
- No Rewards present. + No Rewards present.
) } else { diff --git a/src/view/Components/Taskview/Todoview/TodoItem.tsx b/src/view/Components/Taskview/Todoview/TodoItem.tsx index 92b3d7f..d0ba9fc 100644 --- a/src/view/Components/Taskview/Todoview/TodoItem.tsx +++ b/src/view/Components/Taskview/Todoview/TodoItem.tsx @@ -6,7 +6,6 @@ function TodoItem(props: any) { return (
- {/*

*/}

diff --git a/src/view/Components/Taskview/Todoview/index.tsx b/src/view/Components/Taskview/Todoview/index.tsx index ec61448..aa091e0 100644 --- a/src/view/Components/Taskview/Todoview/index.tsx +++ b/src/view/Components/Taskview/Todoview/index.tsx @@ -1,6 +1,7 @@ import * as React from "react"; import TodoItem from "./TodoItem" import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; +import { useTranslation, Trans, Translation } from 'react-i18next' export default function Index(props: any){ if(props.todos == undefined) { @@ -18,8 +19,8 @@ export default function Index(props: any){ const display =
- Active - Completed + Active + Completed
    {incompleteTodos}
diff --git a/styles.css b/styles.css index 10a2bd6..f9390e3 100644 --- a/styles.css +++ b/styles.css @@ -1,5 +1,4 @@ * { - /* margin: 0; */ padding: 0; } diff --git a/tsconfig.json b/tsconfig.json index 48f3cf9..b905876 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,8 @@ "module": "ESNext", "target": "es6", "jsx": "react", + "resolveJsonModule": true, + "allowSyntheticDefaultImports": true, "noImplicitAny": true, "moduleResolution": "node", "importHelpers": true,