index.ts 3.0 KB

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