-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from libondev/dev-d
feat: 增加多语言支持
- Loading branch information
Showing
13 changed files
with
222 additions
and
30 deletions.
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 |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
- [ ] 彩色方块 | ||
- [x] 游戏音效 | ||
- [x] 游戏界面增加键盘操作 | ||
- [ ] i18n | ||
- [x] i18n | ||
- [ ] 积分商店,积分收集、兑换 | ||
- [ ] 道具系统 | ||
|
||
|
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
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,60 @@ | ||
import type { ShallowRef } from 'vue' | ||
import { type Language, getLanguage, setLanguage } from './use-local-cache' | ||
import zhCN from '@/locale/zh-CN.json' | ||
|
||
interface I18N { | ||
lang: ShallowRef<Language> | ||
setLanguage: (language: Language) => void | ||
$t: (key: string, fallback?: string) => string | ||
} | ||
|
||
export const i18NInjectionKey = Symbol('i18n') as InjectionKey<I18N> | ||
|
||
function loadLanguage(lang: Language) { | ||
return import.meta.glob(`@/locale/*.json`)[`/src/locale/${lang}.json`]() | ||
} | ||
|
||
export function useI18N() { | ||
const lang = shallowRef<Language>('zh-CN') | ||
|
||
const messages = ref<Record<Language, null | Record<string, string>>>({ | ||
'zh-CN': zhCN, | ||
'en-US': null, | ||
'ja-JP': null, | ||
}) | ||
|
||
const msg = computed(() => messages.value[lang.value] ?? messages.value['zh-CN']) | ||
|
||
const _setLang = (language: Language) => { | ||
lang.value = language | ||
setLanguage(language) | ||
|
||
if (!messages.value[language]) { | ||
loadLanguage(language).then((res: any) => { | ||
messages.value[language] = res.default as Record<string, string> | ||
}) | ||
} | ||
} | ||
|
||
getLanguage().then((val) => { | ||
if (val) { | ||
_setLang(val) | ||
} | ||
}) | ||
|
||
const $t = (key: string, fallback: string = ''): string => { | ||
return msg.value?.[key] ?? fallback | ||
} | ||
|
||
provide(i18NInjectionKey, { | ||
lang, | ||
setLanguage: _setLang, | ||
$t, | ||
}) | ||
|
||
return { | ||
lang, | ||
setLanguage: _setLang, | ||
$t, | ||
} | ||
} |
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
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,28 @@ | ||
|
||
{ | ||
"memory-block": "Memory block", | ||
"custom-levels": "Custom levels", | ||
"number-of-grids": "Number of grids", | ||
"minimum-blocks": "Minimum number of generated blocks", | ||
"maximum-blocks": "Maximum number of generated blocks", | ||
"hp": "HP", | ||
"second": "second", | ||
"setup-completed": "Setup completed, start the game", | ||
|
||
"score": "Score", | ||
"start-time": "Start time", | ||
"end-time": "Ends time", | ||
"using-time": "Using time", | ||
|
||
"game-over": "Game over", | ||
"start": "Start", | ||
"again": "Again", | ||
"clear": "Clear", | ||
"selected": "Selected", | ||
"continue": "Continue", | ||
|
||
"memory-time": "Memory time before the start of each round", | ||
"configuration-integer-gt": "Configuration can only be an integer greater than 1", | ||
"select-one-first": "Please select at least one block first", | ||
"remember-block-locations": "Please remember the following block locations" | ||
} |
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,27 @@ | ||
{ | ||
"memory-block": "メモリ ブロック", | ||
"custom-levels": "カスタム レベル", | ||
"number-of-grids": "グリッドの数", | ||
"minimum-blocks": "生成されるブロックの最小数", | ||
"maximum-blocks": "生成されるブロックの最大数", | ||
"hp": "HP", | ||
"second": "秒", | ||
"setup-completed": "セットアップが完了しました。ゲームを開始します", | ||
|
||
"score": "スコア", | ||
"start-time": "開始時刻", | ||
"end-time": "終了時刻", | ||
"using-time": "使用時間", | ||
|
||
"game-over": "ゲームオーバー", | ||
"start": "ゲーム開始", | ||
"again": "また", | ||
"clear": "選択をクリアします", | ||
"selected": "選択済み", | ||
"continue": "継続", | ||
|
||
"memory-time": "各ラウンド開始前の記憶時間", | ||
"configuration-integer-gt": "構成には 1 より大きい整数のみを指定できます", | ||
"select-one-first": "最初に少なくとも 1 つのブロックを選択してください", | ||
"remember-block-locations": "次のブロックの場所を覚えておいてください" | ||
} |
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,27 @@ | ||
{ | ||
"memory-block": "记忆方块", | ||
"custom-levels": "自定义关卡", | ||
"number-of-grids": "网格数量", | ||
"minimum-blocks": "最小生成方块数", | ||
"maximum-blocks": "最大生成方块数", | ||
"hp": "生命值", | ||
"second": "秒", | ||
"setup-completed": "设置完成,开始游戏", | ||
|
||
"score": "得分", | ||
"start-time": "开始于", | ||
"end-time": "结束于", | ||
"using-time": "用时", | ||
|
||
"game-over": "游戏结束", | ||
"start": "游戏开始", | ||
"again": "再来一次", | ||
"clear": "清空选中", | ||
"selected": "选好了", | ||
"continue": "继续", | ||
|
||
"memory-time": "每回合开始前的记忆时间", | ||
"configuration-integer-gt": "配置只能为大于 1 的整数", | ||
"select-one-first": "请先选择至少一个方块", | ||
"remember-block-locations": "请记住以下方块位置" | ||
} |
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
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
Oops, something went wrong.