index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { isNull, forInObject, isEmpty, storage, router } from "@/cool";
  2. import { ref } from "vue";
  3. import zhcn from "./zh-cn.json";
  4. import zhtw from "./zh-tw.json";
  5. import en from "./en.json";
  6. import es from "./es.json";
  7. import ja from "./ja.json";
  8. import ko from "./ko.json";
  9. import fr from "./fr.json";
  10. import { config } from "@/config";
  11. // 解析语言包
  12. function parse(val: string[][]) {
  13. return val[0][0].split("<__&__>").map((e) => e.split("<__=__>"));
  14. }
  15. /**
  16. * 语言包映射对象,包含所有已支持的语言。
  17. * 如需新增语言,只需新建对应的 xx.json 文件并在此处引入即可。
  18. */
  19. const messages = {
  20. "zh-cn": parse(zhcn),
  21. "zh-tw": parse(zhtw),
  22. en: parse(en),
  23. es: parse(es),
  24. ja: parse(ja),
  25. ko: parse(ko),
  26. fr: parse(fr)
  27. };
  28. // 当前语言,默认中文
  29. export const locale = ref<string>("");
  30. // 设置当前语言
  31. export const setLocale = (value: string) => {
  32. locale.value = value;
  33. // #ifdef APP
  34. // APP 环境下,存储语言到本地
  35. storage.set("locale", value, 0);
  36. // #endif
  37. // #ifndef APP
  38. // 其他环境下,直接设置全局语言
  39. uni.setLocale(value);
  40. // #endif
  41. };
  42. // 获取当前语言
  43. export const getLocale = (): string => {
  44. let value: string;
  45. // #ifdef APP
  46. // APP 环境下,优先从本地存储获取
  47. const _locale = storage.get("locale") as string | null;
  48. if (_locale != null && !isEmpty(_locale)) {
  49. value = _locale;
  50. } else {
  51. // @ts-ignore
  52. value = uni.getDeviceInfo().osLanguage as string;
  53. }
  54. // #endif
  55. // #ifndef APP
  56. // 其他环境下,直接获取全局语言
  57. value = uni.getLocale();
  58. // #endif
  59. if (isNull(value) || isEmpty(value)) {
  60. value = config.locale;
  61. }
  62. return value;
  63. };
  64. // 追加数据
  65. export const appendLocale = (name: string, data: string[][]) => {
  66. if (messages[name] != null) {
  67. (messages[name] as string[][]).push(...parse(data));
  68. }
  69. };
  70. // 不带参数的翻译方法
  71. export const t = (name: string) => {
  72. let data = messages[locale.value] as string[][] | null;
  73. if (data == null) {
  74. return name;
  75. }
  76. let text = data.find((e) => e[0] == name)?.[1];
  77. if (text == null || text == "") {
  78. text = name;
  79. }
  80. return text;
  81. };
  82. // 带参数的翻译方法
  83. export const $t = (name: string, data: any) => {
  84. let text = t(name);
  85. // 替换参数
  86. if (!isNull(data)) {
  87. forInObject(data, (value, key) => {
  88. if (typeof value === "number") {
  89. value = value.toString();
  90. }
  91. text = text.replaceAll(`{${key}}`, value as string);
  92. });
  93. }
  94. return text;
  95. };
  96. // 初始化语言设置
  97. export const initLocale = () => {
  98. locale.value = getLocale();
  99. // #ifndef APP
  100. // 监听语言切换事件,自动更新 locale
  101. uni.onLocaleChange((res) => {
  102. setLocale(res.locale!);
  103. });
  104. // #endif
  105. };
  106. // 更新标题
  107. export function updateTitle() {
  108. const style = router.route()?.style;
  109. if (style != null) {
  110. if (style.navigationBarTitleText != null) {
  111. uni.setNavigationBarTitle({
  112. title: t(style.navigationBarTitleText as string)
  113. });
  114. }
  115. }
  116. }