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:
EYHN
2024-06-20 02:19:41 +00:00
parent 5b0f56399c
commit 7c0a686cd9
193 changed files with 553 additions and 575 deletions

View File

@@ -1,4 +1,4 @@
import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { useI18n } from '@affine/i18n';
import { ImportIcon, PlusIcon } from '@blocksuite/icons/rc';
import type { createStore } from 'jotai';
@@ -11,7 +11,7 @@ export function registerAffineCreationCommands({
pageHelper,
t,
}: {
t: ReturnType<typeof useAFFiNEI18N>;
t: ReturnType<typeof useI18n>;
store: ReturnType<typeof createStore>;
pageHelper: ReturnType<typeof usePageHelper>;
}) {

View File

@@ -1,4 +1,4 @@
import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { useI18n } from '@affine/i18n';
import { ContactWithUsIcon, NewIcon } from '@blocksuite/icons/rc';
import type { createStore } from 'jotai';
@@ -10,7 +10,7 @@ export function registerAffineHelpCommands({
t,
store,
}: {
t: ReturnType<typeof useAFFiNEI18N>;
t: ReturnType<typeof useI18n>;
store: ReturnType<typeof createStore>;
}) {
const unsubs: Array<() => void> = [];

View File

@@ -1,4 +1,4 @@
import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { useI18n } from '@affine/i18n';
import { SidebarIcon } from '@blocksuite/icons/rc';
import type { createStore } from 'jotai';
@@ -9,7 +9,7 @@ export function registerAffineLayoutCommands({
t,
store,
}: {
t: ReturnType<typeof useAFFiNEI18N>;
t: ReturnType<typeof useI18n>;
store: ReturnType<typeof createStore>;
}) {
const unsubs: Array<() => void> = [];

View File

@@ -1,5 +1,5 @@
import { WorkspaceSubPath } from '@affine/core/shared';
import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { useI18n } from '@affine/i18n';
import { ArrowRightBigIcon } from '@blocksuite/icons/rc';
import type { DocCollection } from '@blocksuite/store';
import type { createStore } from 'jotai';
@@ -15,7 +15,7 @@ export function registerAffineNavigationCommands({
docCollection,
navigationHelper,
}: {
t: ReturnType<typeof useAFFiNEI18N>;
t: ReturnType<typeof useI18n>;
store: ReturnType<typeof createStore>;
navigationHelper: ReturnType<typeof useNavigateHelper>;
docCollection: DocCollection;

View File

@@ -1,4 +1,4 @@
import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { useI18n } from '@affine/i18n';
import { SettingsIcon } from '@blocksuite/icons/rc';
import type { AffineEditorContainer } from '@blocksuite/presets';
import { appSettingAtom } from '@toeverything/infra';
@@ -14,7 +14,7 @@ export function registerAffineSettingsCommands({
theme,
languageHelper,
}: {
t: ReturnType<typeof useAFFiNEI18N>;
t: ReturnType<typeof useI18n>;
store: ReturnType<typeof createStore>;
theme: ReturnType<typeof useTheme>;
languageHelper: ReturnType<typeof useLanguageHelper>;

View File

@@ -1,6 +1,6 @@
import { updateReadyAtom } from '@affine/core/hooks/use-app-updater';
import { apis } from '@affine/electron-api';
import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import type { useI18n } from '@affine/i18n';
import { ResetIcon } from '@blocksuite/icons/rc';
import type { createStore } from 'jotai';
@@ -10,7 +10,7 @@ export function registerAffineUpdatesCommands({
t,
store,
}: {
t: ReturnType<typeof useAFFiNEI18N>;
t: ReturnType<typeof useI18n>;
store: ReturnType<typeof createStore>;
}) {
const unsubs: Array<() => void> = [];