| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import { isNull, forInObject, isEmpty, storage, router } from "@/cool";
- import { ref } from "vue";
- import zhcn from "./zh-cn.json";
- import zhtw from "./zh-tw.json";
- import en from "./en.json";
- import es from "./es.json";
- import ja from "./ja.json";
- import ko from "./ko.json";
- import fr from "./fr.json";
- import { config } from "@/config";
- // 解析语言包
- function parse(val: string[][]) {
- return val[0][0].split("<__&__>").map((e) => e.split("<__=__>"));
- }
- /**
- * 语言包映射对象,包含所有已支持的语言。
- * 如需新增语言,只需新建对应的 xx.json 文件并在此处引入即可。
- */
- const messages = {
- "zh-cn": parse(zhcn),
- "zh-tw": parse(zhtw),
- en: parse(en),
- es: parse(es),
- ja: parse(ja),
- ko: parse(ko),
- fr: parse(fr)
- };
- // 当前语言,默认中文
- export const locale = ref<string>("");
- // 设置当前语言
- export const setLocale = (value: string) => {
- locale.value = value;
- // #ifdef APP
- // APP 环境下,存储语言到本地
- storage.set("locale", value, 0);
- // #endif
- // #ifndef APP
- // 其他环境下,直接设置全局语言
- uni.setLocale(value);
- // #endif
- };
- // 获取当前语言
- export const getLocale = (): string => {
- let value: string;
- // #ifdef APP
- // APP 环境下,优先从本地存储获取
- const _locale = storage.get("locale") as string | null;
- if (_locale != null && !isEmpty(_locale)) {
- value = _locale;
- } else {
- // @ts-ignore
- value = uni.getDeviceInfo().osLanguage as string;
- }
- // #endif
- // #ifndef APP
- // 其他环境下,直接获取全局语言
- value = uni.getLocale();
- // #endif
- if (isNull(value) || isEmpty(value)) {
- value = config.locale;
- }
- return value;
- };
- // 追加数据
- export const appendLocale = (name: string, data: string[][]) => {
- if (messages[name] != null) {
- (messages[name] as string[][]).push(...parse(data));
- }
- };
- // 不带参数的翻译方法
- export const t = (name: string) => {
- let data = messages[locale.value] as string[][] | null;
- if (data == null) {
- return name;
- }
- let text = data.find((e) => e[0] == name)?.[1];
- if (text == null || text == "") {
- text = name;
- }
- return text;
- };
- // 带参数的翻译方法
- export const $t = (name: string, data: any) => {
- let text = t(name);
- // 替换参数
- if (!isNull(data)) {
- forInObject(data, (value, key) => {
- if (typeof value === "number") {
- value = value.toString();
- }
- text = text.replaceAll(`{${key}}`, value as string);
- });
- }
- return text;
- };
- // 初始化语言设置
- export const initLocale = () => {
- locale.value = getLocale();
- // #ifndef APP
- // 监听语言切换事件,自动更新 locale
- uni.onLocaleChange((res) => {
- setLocale(res.locale!);
- });
- // #endif
- };
- // 更新标题
- export function updateTitle() {
- const style = router.route()?.style;
- if (style != null) {
- if (style.navigationBarTitleText != null) {
- uni.setNavigationBarTitle({
- title: t(style.navigationBarTitleText as string)
- });
- }
- }
- }
|