index.ts 2.5 KB

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