useTranslationLang.ts 863 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { useNav } from "./useNav";
  2. import { useI18n } from "vue-i18n";
  3. import { useRoute } from "vue-router";
  4. import { watch, onBeforeMount, type Ref } from "vue";
  5. export function useTranslationLang(ref?: Ref) {
  6. const { $storage, changeTitle, handleResize } = useNav();
  7. const { locale, t } = useI18n();
  8. const route = useRoute();
  9. function translationCh() {
  10. $storage.locale = { locale: "zh" };
  11. locale.value = "zh";
  12. ref && handleResize(ref.value);
  13. }
  14. function translationEn() {
  15. $storage.locale = { locale: "en" };
  16. locale.value = "en";
  17. ref && handleResize(ref.value);
  18. }
  19. watch(
  20. () => locale.value,
  21. () => {
  22. changeTitle(route.meta);
  23. }
  24. );
  25. onBeforeMount(() => {
  26. locale.value = $storage.locale?.locale ?? "zh";
  27. });
  28. return {
  29. t,
  30. route,
  31. locale,
  32. translationCh,
  33. translationEn
  34. };
  35. }