feat(i18n): static type on i18n (#2225)

This commit is contained in:
Himself65 authored and GitHub committed 2023-05-04 05:35:09 +00:00
1 parent 66c3b09c67
commit 3d43e61087
80 files changed
+585 -444

No files matched your search

+4 -8
View File
@@ -6,7 +6,7 @@
- Replace literal text with translation keys
```tsx
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
// src/resources/en.json
// {
@@ -15,7 +15,7 @@ import { useTranslation } from '@affine/i18n';
// };
const App = () => {
const { t } = useTranslation();
const t = useAFFiNEI18N();
const changeLanguage = (language: string) => {
i18n.changeLanguage(language);
@@ -25,12 +25,8 @@ const App = () => {
<div>
<div>{t('Text')}</div>
<button onClick={() => changeLanguage('en')}>
{t('Switch to language', { language: 'en' })}
</button>
<button onClick={() => changeLanguage('zh-Hans')}>
{t('Switch to language', { language: 'zh-Hans' })}
</button>
<button onClick={() => changeLanguage('en')}>{t('Switch to language', { language: 'en' })}</button>
<button onClick={() => changeLanguage('zh-Hans')}>{t('Switch to language', { language: 'zh-Hans' })}</button>
</div>
);
};
+2 -1
View File
@@ -4,7 +4,8 @@
"type": "module",
"main": "src/index.ts",
"exports": {
".": "./src/index.ts"
".": "./src/index.ts",
"./hooks": "./src/i18n_generated"
},
"publishConfig": {
"main": "dist/src/index.js",
+6 -2
View File
@@ -4,7 +4,7 @@ import {
I18nextProvider,
initReactI18next,
Trans,
useTranslation,
useTranslation as useRootTranslation,
} from 'react-i18next';
import { LOCALES } from './resources';
@@ -38,7 +38,11 @@ declare module 'react-i18next' {
const STORAGE_KEY = 'i18n_lng';
export { I18nextProvider, LOCALES, Trans, useTranslation };
export { I18nextProvider, LOCALES, Trans };
export function useI18N() {
const { i18n } = useRootTranslation();
return i18n;
}
const resources = LOCALES.reduce<Resource>(
(acc, { tag, res }) => ({ ...acc, [tag]: { translation: res } }),
+3
View File
@@ -8,6 +8,8 @@
"It takes up more space on your device": {
"": "It takes up more space on your device."
},
"Synced with AFFiNE Cloud": "Synced with AFFiNE Cloud",
"Successfully deleted": "Successfully deleted",
"Export AFFiNE backup file": "Export AFFiNE backup file",
"Saved then enable AFFiNE Cloud": "All changes are saved locally, click to enable AFFiNE Cloud.",
"Not now": "Not now",
@@ -95,6 +97,7 @@
"Quick search placeholder": "Quick Search...",
"Quick search placeholder2": "Search in {{workspace}}",
"Settings": "Settings",
"Recent": "Recent",
"recommendBrowser": " We recommend the <1>Chrome</1> browser for optimal experience.",
"upgradeBrowser": "Please upgrade to the latest version of Chrome for the best experience.",
"Invite Members": "Invite Members",