generated from SteamDeckHomebrew/decky-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
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 #63 from mengmeet/i18next
Update localization by i18next / 更新本地化库, 使用i18next实现
- Loading branch information
Showing
9 changed files
with
217 additions
and
135 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,21 +1,49 @@ | ||
import { ServerAPI } from "decky-frontend-lib"; | ||
import { localizeMap, localizeStrEnum } from "./localizeMap"; | ||
import { defaultLocale, localizeMap, localizeStrEnum } from "./localizeMap"; | ||
|
||
import i18n, { Resource } from "i18next"; | ||
|
||
export class localizationManager { | ||
private static language = "english" | ||
//private has_language = false | ||
public static async init(serverAPI: ServerAPI) { | ||
await serverAPI!.callPluginMethod<{},string>("get_language",{}).then(res=>{ | ||
if (res.success){ | ||
//console.log("language = " + res.result); | ||
this.language = res.result; | ||
//this.has_language = true; | ||
} | ||
}) | ||
private static language = "english"; | ||
|
||
public static async init() { | ||
const language = | ||
(await SteamClient.Settings.GetCurrentLanguage()) || "english"; | ||
this.language = language; | ||
console.log("Language: " + this.language); | ||
|
||
const resources: Resource = Object.keys(localizeMap).reduce((acc, key) => { | ||
acc[localizeMap[key].locale] = { | ||
translation: localizeMap[key].strings, | ||
}; | ||
return acc; | ||
}, {}); | ||
|
||
i18n.init({ | ||
// resources: { | ||
// "zh-CN": { | ||
// translation: localizeMap["tchinese"].strings, | ||
// } | ||
// }, | ||
// lng:"zh-CN", | ||
resources: resources, | ||
lng: this.getLocale(), // 目标语言 | ||
fallbackLng: defaultLocale, // 回落语言 | ||
returnEmptyString: false, // 空字符串不返回, 使用回落语言 | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
}); | ||
} | ||
public static getString(defaultString:localizeStrEnum){ | ||
var str = localizeMap[this.language]?.strings?.[defaultString]??localizeMap["english"]?.strings?.[defaultString]; | ||
return str==""?localizeMap["english"]?.strings?.[defaultString]:str | ||
|
||
private static getLocale() { | ||
return localizeMap[this.language]?.locale ?? defaultLocale; | ||
} | ||
} | ||
|
||
public static getString(defaultString: localizeStrEnum, variables?: Record<string, unknown>) { | ||
// var str = | ||
// localizeMap[this.language]?.strings?.[defaultString] ?? | ||
// localizeMap["english"]?.strings?.[defaultString]; | ||
// return str == "" ? localizeMap["english"]?.strings?.[defaultString] : str; | ||
return i18n.t(defaultString, variables); | ||
} | ||
} |
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.