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,10 +1,10 @@
import { describe, expect, test } from 'vitest';
import { createI18n, getI18n } from '../../';
import { createI18n, I18n } from '../../';
import { i18nTime } from '../time';
// Intl api is not available in github action, skip the test
describe.skip('humanTime', () => {
describe('humanTime', () => {
test('absolute', async () => {
createI18n();
expect(i18nTime('2024-10-10 13:30:28')).toBe('Oct 10, 2024, 1:30:28 PM');
@@ -350,7 +350,7 @@ describe.skip('humanTime', () => {
test('chinese', () => {
createI18n();
getI18n().changeLanguage('zh-Hans');
I18n.changeLanguage('zh-Hans');
expect(i18nTime('2024-10-10 13:30:28.005')).toBe('2024年10月10日 13:30:28');
expect(
i18nTime('2024-10-10 13:30:28.005', {