forked from goplus/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# i18n | ||
|
||
Simple i18n tool for vue. | ||
|
||
## Usage | ||
|
||
### Init & set language | ||
|
||
```ts | ||
import { createI18n, useI18n } from './utils/i18n' | ||
|
||
// Install in Vue app | ||
app.use( | ||
createI18n({ | ||
lang: 'en' | ||
}) | ||
) | ||
|
||
// set language | ||
const i18n = useI18n() | ||
i18n.setLang('zh') | ||
``` | ||
|
||
### Do translation in template of SFC | ||
|
||
```vue | ||
<button> | ||
{{_t({ en: 'Sign in', zh: '登录' })}} | ||
</button> | ||
``` | ||
|
||
P.S. To avoid naming conflict with vue-i18n, we named the global property as `_t`. After vue-i18n removed from the project, we will rename it as `$t`. | ||
|
||
### Do translation in setup script | ||
|
||
```ts | ||
import { useI18n } from '@/utils/i18n' | ||
|
||
const { t } = useI18n() | ||
|
||
const signoutText = t({ en: 'Sign out', zh: '登出' }) | ||
``` | ||
|
||
### Function Locale Message | ||
|
||
Function-locale-messages are messages that extra information are needed when translating. For example: | ||
|
||
```ts | ||
const projectSummaryMessage: FunctionLocaleMessage<[num: number]> = { | ||
en: num => `You have ${num} project${num > 1 ? 's' : ''}`, | ||
zh: num => `你有 ${num} 个项目` | ||
} | ||
|
||
const projectSummary = t(projectSummaryMessage, 3) // "You have 3 projects" / "你有 3 个项目" | ||
``` | ||
|
||
It's like [interpolations](https://vue-i18n.intlify.dev/guide/essentials/syntax.html#interpolations) in vue-i18n, but simpler & more powerful. | ||
|
||
### `mapMessage` | ||
|
||
Messages are more complex than texts, which makes messages operation more difficult that text operation. Fo example, we can join texts simply with string `+` or template strings (or even array `join`): | ||
|
||
```ts | ||
const helloText = 'Hello' | ||
console.log(helloText + ' foo') | ||
``` | ||
|
||
But it is not easy to do similar thing with messages. `mapMessage` aims to help: | ||
|
||
```ts | ||
const helloMessage = { | ||
en: 'Hello', | ||
zh: '你好' | ||
} | ||
const resultMessage = mapMessage(helloMessage, hello => hello + ' foo') | ||
console.log(t(resultMessage)) // "Hello foo" / "你好 foo" | ||
``` | ||
|
||
We can also use `mapMessage` with function-locale-messages: | ||
|
||
```ts | ||
const projectSummaryMessage: FunctionLocaleMessage<[num: number]> = { | ||
en: num => `You have ${num} project${num > 1 ? 's' : ''}`, | ||
zh: num => `你有 ${num} 个项目` | ||
} | ||
|
||
const resultMessage = mapMessage(projectSummaryMessage, f => f(3)) | ||
console.log(t(resultMessage)) // "You have 3 projects" / "你有 3 个项目" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters