-
Notifications
You must be signed in to change notification settings - Fork 10
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
7 changed files
with
89 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
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,15 @@ | ||
import type { VueMessageType } from 'vue-i18n' | ||
import { LocaleMessage } from '@intlify/core-base' | ||
|
||
/** | ||
* 配置语法:https://vue-i18n.intlify.dev/guide/essentials/syntax.html | ||
*/ | ||
export type LocaleConfig = LocaleMessage<VueMessageType> & { | ||
// 当前语言唯一标识 | ||
local: string | ||
// 嵌套配置示例 TODO 删除嵌套配置示例 | ||
example: { | ||
// 占位符配置示例 TODO 删除占位符配置示例 | ||
placeholder: string | ||
} | ||
} |
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,47 @@ | ||
import { createI18n } from 'vue-i18n' | ||
import { en } from '@/i18n/locales/en' | ||
import { zhCn } from '@/i18n/locales/zh-cn' | ||
import { zhTw } from '@/i18n/locales/zh-tw' | ||
import { LocaleConfig } from '@/i18n/config' | ||
|
||
/** | ||
* 获取所有语言 | ||
*/ | ||
function getMessages (): Record<string, LocaleConfig> { | ||
const messages: Record<string, LocaleConfig> = {} | ||
messages[zhCn.local] = zhCn | ||
messages[zhTw.local] = zhTw | ||
messages[en.local] = en | ||
return messages | ||
} | ||
|
||
/** | ||
* 获取默认语言 | ||
*/ | ||
function getDefaultLocale () { | ||
const languages = navigator.languages || [navigator.language] | ||
for (const language of languages) { | ||
if (/^zh(.?CN)?$/i.test(language)) { | ||
// 简体中文,包括 zh 和 zh-CN | ||
return zhCn.local | ||
} else if (/^zh\b/i.test(language)) { | ||
// 除简体中文外的所有中文默认使用繁体中文 | ||
return zhTw.local | ||
} else if (/^en\b/i.test(language)) { | ||
// 英语 | ||
return en.local | ||
} | ||
} | ||
// 默认使用简体中文 | ||
return zhCn.local | ||
} | ||
|
||
/** | ||
* 配置 Vue I18n,开发工具参见:https://vue-i18n.intlify.dev/ecosystem/tools.html | ||
*/ | ||
export default createI18n({ | ||
legacy: false, | ||
fallbackLocale: zhCn.local, | ||
locale: getDefaultLocale(), | ||
messages: getMessages(), | ||
}) |
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,8 @@ | ||
import { LocaleConfig } from '@/i18n/config' | ||
|
||
export const en: LocaleConfig = { | ||
local: 'en', | ||
example: { | ||
placeholder: 'Placeholder: {0}', | ||
}, | ||
} |
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,8 @@ | ||
import { LocaleConfig } from '@/i18n/config' | ||
|
||
export const zhCn: LocaleConfig = { | ||
local: 'zh-cn', | ||
example: { | ||
placeholder: '占位符: {0}', | ||
}, | ||
} |
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,8 @@ | ||
import { LocaleConfig } from '@/i18n/config' | ||
|
||
export const zhTw: LocaleConfig = { | ||
local: 'zh-tw', | ||
example: { | ||
placeholder: '佔位符: {0}', | ||
}, | ||
} |
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