mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-23 21:06:22 +08:00
refactor(i18n): new hook api (#7273)
# NEW HOOK API
`useI18n`: same as `useAFFiNEI18N`, with additional APIs
```ts
import { useI18n } from '@affine/i18n'
const i18n = useI18n()
i18n['hello world']() -> 你好世界
```
# NEW GLOBAL i18n Instance
`I18n`: use i18n capabilities outside of React
```ts
import { I18n } from '@affine/i18n'
I18n['hello world']() -> 你好世界
```
# NEW TYPES
`I18nKeys` -> all i18n keys
`I18nString` -> An i18n message (key&options)
transfer and store i18n text outside of React
```ts
const msg: I18nString = {
key: 'helloworld',
options: {
arg1: '123'
}
}
I18n.t(msg) -> 你好世界123
```
before:
```ts
registerCommand('open-page', {
name: t('command.open-page')
// ^- translation happens here,
})
```
after:
```ts
registerCommand('open-page', {
name: { key: 'command.open-page' }
// ^- store I18nString here, translate when the command render to UI
})
```
This commit is contained in:
@@ -1,18 +1,12 @@
|
||||
import type { i18n, Resource } from 'i18next';
|
||||
import i18next from 'i18next';
|
||||
import type { I18nextProviderProps } from 'react-i18next';
|
||||
import {
|
||||
I18nextProvider,
|
||||
initReactI18next,
|
||||
Trans,
|
||||
useTranslation as useRootTranslation,
|
||||
} from 'react-i18next';
|
||||
import { I18nextProvider, initReactI18next, Trans } from 'react-i18next';
|
||||
|
||||
import { LOCALES } from './resources';
|
||||
import type en_US from './resources/en.json';
|
||||
|
||||
export * from './i18n';
|
||||
export * from './i18n-generated';
|
||||
export * from './utils';
|
||||
|
||||
declare module 'i18next' {
|
||||
@@ -44,12 +38,6 @@ declare module 'react-i18next' {
|
||||
const STORAGE_KEY = 'i18n_lng';
|
||||
|
||||
export { I18nextProvider, LOCALES, Trans };
|
||||
export function useI18N() {
|
||||
const { i18n } = useRootTranslation();
|
||||
return i18n;
|
||||
}
|
||||
|
||||
export { getI18n } from 'react-i18next';
|
||||
|
||||
const resources = LOCALES.reduce<Resource>((acc, { tag, res }) => {
|
||||
return Object.assign(acc, { [tag]: { translation: res } });
|
||||
@@ -99,6 +87,7 @@ export const createI18n = (): I18nextProviderProps['i18n'] => {
|
||||
}
|
||||
return i18n;
|
||||
};
|
||||
|
||||
export function setUpLanguage(i: i18n) {
|
||||
let language;
|
||||
const localStorageLanguage = localStorage.getItem(STORAGE_KEY);
|
||||
@@ -109,5 +98,3 @@ export function setUpLanguage(i: i18n) {
|
||||
}
|
||||
return i.changeLanguage(language);
|
||||
}
|
||||
|
||||
// const I18nProvider = I18nextProvider;
|
||||
|
||||
Reference in New Issue
Block a user