From 6433c4c72a625e9ba3f1bc63d782e85d38baa02f Mon Sep 17 00:00:00 2001 From: kkzzhizhou <806508401@qq.com> Date: Mon, 29 Nov 2021 13:56:44 +0800 Subject: [PATCH] feature: full i18n support --- .gitignore | 1 + package.json | 3 - src/i18n/zh-cn.json | 14 +++- src/main.ts | 3 - src/view/App.tsx | 68 ++++++++++--------- src/view/Components/Statsview/index.css | 0 .../Components/Taskview/Dailiesview/index.tsx | 8 +-- .../Components/Taskview/Habitsview/index.tsx | 5 +- .../Components/Taskview/Rewardview/index.tsx | 7 +- .../Components/Taskview/Todoview/index.tsx | 7 +- 10 files changed, 64 insertions(+), 52 deletions(-) delete mode 100644 src/view/Components/Statsview/index.css diff --git a/.gitignore b/.gitignore index 5ae3397..8e09aa7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ package-lock.json # yarn yarn.lock +yarn-error.log *.js.map diff --git a/package.json b/package.json index 64e5917..9033a89 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ }, "dependencies": { "@mui/icons-material": "^5.2.0", - "@react-navigation/native": "^6.0.6", "i18next": "^21.5.2", "i18next-browser-languagedetector": "^6.1.2", "node": "^16.10.0", @@ -38,10 +37,8 @@ "react": "^17.0.2", "react-dom": "^17.0.2", "react-emoji-render": "^1.2.4", - "react-hook-form": "^7.20.2", "react-i18next": "^11.14.2", "react-markdown": "^7.1.0", - "react-navigation": "^4.4.4", "react-tabs": "^3.2.2" } } diff --git a/src/i18n/zh-cn.json b/src/i18n/zh-cn.json index 0a8cf93..e919653 100644 --- a/src/i18n/zh-cn.json +++ b/src/i18n/zh-cn.json @@ -17,5 +17,17 @@ "API Error: Please check credentials": "API错误,请检查凭据", "Resyncing, please try again": "重新同步失败,请稍后再试", "Add!": "已添加", - "Loading....": "加载中...." + "Loading....": "加载中....", + "Deleted!": "已删除!", + "Edit!": "已修改!", + "running cron": "正在运行定时任务", + "Checked!": "已完成", + "Un-Checked!": "取消完成状态", + "Plus!": "+1", + "Minus :(": "-1", + "Cost!": "消费成功", + "Add Daily Task": "添加每日任务", + "Add Todo": "添加待办事项", + "Add Habit": "添加习惯", + "Add Reward": "添加奖励" } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index a68a6f1..77e920a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,7 +33,6 @@ export default class HabiticaSync extends Plugin { } }); this.activateView(); - } async loadSettings() { this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData()) @@ -52,12 +51,10 @@ export default class HabiticaSync extends Plugin { } async activateView() { this.app.workspace.detachLeavesOfType(VIEW_TYPE); - await this.app.workspace.getRightLeaf(false).setViewState({ type: VIEW_TYPE, active: true, }); - this.app.workspace.revealLeaf( this.app.workspace.getLeavesOfType(VIEW_TYPE)[0] ); diff --git a/src/view/App.tsx b/src/view/App.tsx index 05ba310..f93b048 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -3,7 +3,9 @@ import { Notice } from "obsidian"; import { getStats, scoreTask, makeCronReq, costReward, addTask, deleteTask, updateTask } from "./habiticaAPI" import Statsview from "./Components/Statsview" import Taskview from "./Components/Taskview" -import "../i18n" +import { Trans } from 'react-i18next'; +import "src/i18n" +import i18next from "i18next"; class App extends React.Component { private _username = ""; @@ -63,7 +65,7 @@ class App extends React.Component { }; } async runCron() { - console.log("running cron"); + console.log(i18next.t('running cron')); try { let response = await makeCronReq(this.username, this.credentials); this.setState({ @@ -71,7 +73,7 @@ class App extends React.Component { }) } catch (error) { console.log(error); - new Notice("There was an error running the cron. Please try again later."); + new Notice(i18next.t("There was an error running the cron. Please try again later.")); } this.reloadData(); } @@ -80,7 +82,7 @@ class App extends React.Component { let response = await getStats(this.username, this.credentials); let result = await response.json(); if (result.success === false) { - new Notice('Login Failed, Please check credentials and try again!'); + new Notice(i18next.t('Login Failed, Please check credentials and try again!')); } else { this.setState({ @@ -91,7 +93,7 @@ class App extends React.Component { } } catch (e) { console.log(e); - new Notice("API Error: Please check credentials") + new Notice(i18next.t('API Error: Please check credentials')) } } componentDidMount() { @@ -106,12 +108,12 @@ class App extends React.Component { new Notice(message); this.reloadData(); } else { - new Notice("Resyncing, please try again"); + new Notice(i18next.t('Resyncing, please try again')); this.reloadData(); } } catch (e) { console.log(e); - new Notice("API Error: Please check credentials") + new Notice(i18next.t('API Error: Please check credentials')) } } @@ -123,12 +125,12 @@ class App extends React.Component { new Notice(message); this.reloadData(); } else { - new Notice("Resyncing, please try again"); + new Notice(i18next.t('Resyncing, please try again')); this.reloadData(); } } catch (e) { console.log(e); - new Notice("API Error: Please check credentials") + new Notice(i18next.t('API Error: Please check credentials')) } } @@ -140,12 +142,12 @@ class App extends React.Component { new Notice(message); this.reloadData(); } else { - new Notice("Resyncing, please try again"); + new Notice(i18next.t('Resyncing, please try again')); this.reloadData(); } } catch (e) { console.log(e); - new Notice("API Error: Please check credentials") + new Notice(i18next.t('API Error: Please check credentials')) } } @@ -157,12 +159,12 @@ class App extends React.Component { new Notice(message); this.reloadData(); } else { - new Notice("Resyncing, please try again"); + new Notice(i18next.t('Resyncing, please try again')); this.reloadData(); } } catch (e) { console.log(e); - new Notice("API Error: Please check credentials") + new Notice(i18next.t('API Error: Please check credentials')) } } @@ -174,12 +176,12 @@ class App extends React.Component { new Notice(message); this.reloadData(); } else { - new Notice("Resyncing, please try again"); + new Notice(i18next.t('Resyncing, please try again')); this.reloadData(); } } catch (e) { console.log(e); - new Notice("API Error: Please check credentials") + new Notice(i18next.t('API Error: Please check credentials')) } // console.log(id, type,title,notes) } @@ -187,20 +189,20 @@ class App extends React.Component { handleChangeTodos(event: any) { if (event.target.id == "add-todo") { const title = event.target.name - this.sendAddTask("todo", title, "Add!") + this.sendAddTask("todo", title, i18next.t('Add!')) } else { this.state.tasks.todos.forEach((element: any) => { if (element.id == event.target.id) { if (event.type == "click") { console.log(event) if (event.target.innerText == 'clear') { - this.sendDeleteTask(event.target.id, "Deleted!") + this.sendDeleteTask(event.target.id, i18next.t('Deleted!')) } } else { if (!element.completed) { - this.sendScore(event.target.id, "up", "Checked!") + this.sendScore(event.target.id, "up", i18next.t('Checked!')) } else { - this.sendScore(event.target.id, "down", "Un-Checked!") + this.sendScore(event.target.id, "down", i18next.t('Un-Checked!')) } } } @@ -212,7 +214,7 @@ class App extends React.Component { handleChangeDailys(event: any) { if (event.target.id == "add-daily") { const title = event.target.name - this.sendAddTask("daily", title, "Add!") + this.sendAddTask("daily", title, i18next.t('Add!')) } else { this.state.tasks.dailys.forEach((element: any) => { if (element.id == event.target.id) { @@ -222,11 +224,11 @@ class App extends React.Component { const task_notes = event.target.attributes['data-notes'].value ? event.target.attributes['data-notes'].value : element.notes this.sendUpdateTask(event.target.id, 'daily', "Update!", task_title, task_notes) } else if (event.target.attributes.title.value == 'delete') { - this.sendDeleteTask(event.target.id, "Deleted!") + this.sendDeleteTask(event.target.id, i18next.t('Deleted!')) } else if (!element.completed) { - this.sendScore(event.target.id, "up", "Checked!") + this.sendScore(event.target.id, "up", i18next.t('Checked!')) } else { - this.sendScore(event.target.id, "down", "Un-Checked!") + this.sendScore(event.target.id, "down", i18next.t('Un-Checked!')) } } } @@ -237,23 +239,23 @@ class App extends React.Component { handleChangeHabits(event: any) { if (event.target.id == "add-habit") { const title = event.target.name - this.sendAddTask("habit", title, "Add!") + this.sendAddTask("habit", title, i18next.t('Add!')) } else { if (event.target.innerText == 'clear') { - this.sendDeleteTask(event.target.id, "Deleted!") + this.sendDeleteTask(event.target.id, i18next.t('Deleted!')) } else { const target_id = event.target.id.slice(4) if (event.target.id.slice(0, 4) == "plus") { this.state.tasks.habits.forEach((element: any) => { if (element.id == target_id) { - this.sendScore(target_id, "up", "Plus!") + this.sendScore(target_id, "up", i18next.t('Plus!')) } }) } else { this.state.tasks.habits.forEach((element: any) => { if (element.id == target_id) { - this.sendScore(target_id, "down", "Minus :(") + this.sendScore(target_id, "down", i18next.t("Minus :(")) } }) } @@ -265,17 +267,17 @@ class App extends React.Component { console.log(event) if (event.target.id == "add-reward") { const title = event.target.name - this.sendAddTask("reward", title, "Add!") + this.sendAddTask("reward", title, i18next.t('Add!')) } else { const target_id = event.target.id this.state.tasks.rewards.forEach((element: any) => { if (element.id == target_id) { if (event.target.innerText == 'clear') { - this.sendDeleteTask(event.target.id, "Deleted!") + this.sendDeleteTask(event.target.id, i18next.t('Deleted!')) } else if (event.target.innerText == 'create') { - this.sendUpdateTask(event.target.id, 'reward', "Edit!", "1", "1") + this.sendUpdateTask(event.target.id, 'reward', i18next.t('Edit!'), "1", "1") } else { - this.sendReward(target_id, "down", "Cost!") + this.sendReward(target_id, "down", i18next.t('Cost!')) } } }) @@ -284,9 +286,9 @@ class App extends React.Component { render() { let content = this.CheckCron(this.state.user_data.lastCron); if (this.state.error) - return (
Loading....
) + return (
Loading....
) else if (!this.state.isLoaded) - return
Loading....
+ return
Loading....
else { return (
diff --git a/src/view/Components/Statsview/index.css b/src/view/Components/Statsview/index.css deleted file mode 100644 index e69de29..0000000 diff --git a/src/view/Components/Taskview/Dailiesview/index.tsx b/src/view/Components/Taskview/Dailiesview/index.tsx index 6bef3a1..6149711 100644 --- a/src/view/Components/Taskview/Dailiesview/index.tsx +++ b/src/view/Components/Taskview/Dailiesview/index.tsx @@ -1,14 +1,14 @@ import * as React from "react"; import DailyItem from "./DailyItem" import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; -import { Trans } from 'react-i18next'; +import { Trans, useTranslation } from 'react-i18next'; export default function Index(props: any) { const [title, setTitle] = React.useState('') - + let { t ,i18n} = useTranslation() if (props.dailys == undefined) { return
- setTitle(event.target.value)} /> + setTitle(event.target.value)} /> No Dailies Present
@@ -32,7 +32,7 @@ export default function Index(props: any) { Completed - setTitle(event.target.value)} value={title} /> + setTitle(event.target.value)} value={title} />
    {incompleteDailies}
diff --git a/src/view/Components/Taskview/Habitsview/index.tsx b/src/view/Components/Taskview/Habitsview/index.tsx index 42ae42c..d4619ed 100644 --- a/src/view/Components/Taskview/Habitsview/index.tsx +++ b/src/view/Components/Taskview/Habitsview/index.tsx @@ -4,9 +4,10 @@ import { useTranslation, Trans, Translation } from 'react-i18next' export default function Index(props: any){ const [title, setTitle] = React.useState('') + let { t ,i18n} = useTranslation() if(props.habits == undefined) { return (
- setTitle(event.target.value)} /> + setTitle(event.target.value)} /> No habits present.
) @@ -18,7 +19,7 @@ export default function Index(props: any){ const display =
- setTitle(event.target.value)} /> + setTitle(event.target.value)} />
    {allHabits}
diff --git a/src/view/Components/Taskview/Rewardview/index.tsx b/src/view/Components/Taskview/Rewardview/index.tsx index 182726e..39a5afd 100644 --- a/src/view/Components/Taskview/Rewardview/index.tsx +++ b/src/view/Components/Taskview/Rewardview/index.tsx @@ -1,12 +1,13 @@ import * as React from "react"; import RewardItem from "./RewardItem" -import { Trans } from 'react-i18next' +import { Trans,useTranslation } from 'react-i18next' export default function Index(props: any){ const [title, setTitle] = React.useState('') + let { t ,i18n} = useTranslation() if(props.rewards == undefined) { return (
- setTitle(event.target.value)} /> + setTitle(event.target.value)} /> No Rewards present.
) @@ -16,7 +17,7 @@ export default function Index(props: any){ return }) const display =
- setTitle(event.target.value)} /> + setTitle(event.target.value)} />
    {allRewards}
diff --git a/src/view/Components/Taskview/Todoview/index.tsx b/src/view/Components/Taskview/Todoview/index.tsx index 2db970f..46f2966 100644 --- a/src/view/Components/Taskview/Todoview/index.tsx +++ b/src/view/Components/Taskview/Todoview/index.tsx @@ -1,13 +1,14 @@ import * as React from "react"; import TodoItem from "./TodoItem" import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; -import { Trans } from 'react-i18next' +import { Trans,useTranslation } from 'react-i18next' export default function Index(props: any){ const [title, setTitle] = React.useState('') + let { t ,i18n} = useTranslation() if(props.todos == undefined) { return
- setTitle(event.target.value)} /> + setTitle(event.target.value)} /> No Todos present.
@@ -28,7 +29,7 @@ export default function Index(props: any){ Completed - setTitle(event.target.value)} /> + setTitle(event.target.value)} />
    {incompleteTodos}