11<script setup lang="ts">
22import Layout from " @/components/layout/Layout.vue" ;
33import { onAfterRouteLeave , RouterView } from " @kitbag/router" ;
4- import { provide , readonly , type Ref , ref , shallowRef , useTemplateRef , watchEffect } from " vue" ;
4+ import { provide , readonly , ref , shallowRef , useTemplateRef , watchEffect } from " vue" ;
55import {
66 TranslationsKey ,
7- LocaleSelectorContextKey ,
7+ TranslationsSelectorContextKey ,
88 PageWrapperContextKey , TranslationsContextKey ,
99} from " @/constants/application.ts" ;
1010import type { TranslationsSelectorType } from " @/types/translations-selector.type.ts" ;
@@ -13,17 +13,33 @@ import { useAccentAnimation } from "@/lib/stores/misc/accent-animations.ts";
1313import English from " @/locales/en.json" ;
1414import { shallowValidateTranslations } from " @/lib/translations/shallow-validate-translations.ts" ;
1515import type { TranslationsType } from " @/types/translations.type.ts" ;
16+ import { Locales } from " @/constants/locales.ts" ;
17+ import type { LocaleType } from " @/types/locale.type.ts" ;
18+ import FetchTranslations from " @/components/general/FetchTranslations.vue" ;
19+ import type { TranslationsReferenceType } from " @/types/translations-reference.type.ts" ;
1620
1721const scrollLocked = ref <boolean >(false );
1822const scrollTarget = useTemplateRef (" scrollTarget" );
1923
20- const storedTranslations = localStorage .getItem (TranslationsKey )
21- ?? JSON .stringify (English );
22- // const storedLocale: string = localStorage.getItem(LocaleKey) ?? navigator.language.slice(0, 2);
24+ const shouldFetchTranslations = ref <boolean >(false );
25+
26+ const navigatorLocale: string = navigator .language .slice (0 , 2 );
27+ const storedTranslations: string | null = localStorage .getItem (TranslationsKey );
2328let parsedTranslations: unknown ;
2429
30+ if (storedTranslations === null && navigatorLocale !== " en" ) {
31+ const doesUserLocaleExist = Locales
32+ .includes (navigatorLocale as LocaleType );
33+
34+ if (doesUserLocaleExist ) {
35+ shouldFetchTranslations .value = true ;
36+ }
37+ }
38+
2539try {
26- parsedTranslations = JSON .parse (storedTranslations );
40+ parsedTranslations = storedTranslations === null
41+ ? English
42+ : JSON .parse (storedTranslations );
2743} catch (error : unknown ) {
2844 console .error (" Couldn't parse stored translations" , error );
2945 parsedTranslations = English ;
@@ -33,8 +49,6 @@ const translations = shallowRef<TranslationsType>(
3349 shallowValidateTranslations (parsedTranslations ),
3450);
3551
36- document .getElementById (" __html-tag" )?.setAttribute ?.(" lang" , translations .value .Info .Code );
37-
3852function selectTranslations(selected : TranslationsType ): void {
3953 translations .value = selected ;
4054 localStorage .setItem (TranslationsKey , JSON .stringify (selected ));
@@ -44,8 +58,8 @@ function lockScroll(state: boolean): void {
4458}
4559
4660provide <(state : boolean ) => void >(PageWrapperContextKey , lockScroll );
47- provide <Ref < TranslationsType , TranslationsType > >(TranslationsContextKey , readonly (translations ));
48- provide <TranslationsSelectorType >(LocaleSelectorContextKey , selectTranslations );
61+ provide <TranslationsReferenceType >(TranslationsContextKey , readonly (translations ));
62+ provide <TranslationsSelectorType >(TranslationsSelectorContextKey , selectTranslations );
4963
5064const accentAnimationStore = useAccentAnimation ();
5165
@@ -74,6 +88,8 @@ watchEffect(() => {
7488 pause ();
7589});
7690watchEffect (() => {
91+ document .getElementById (" __html-tag" )?.setAttribute ?.(" lang" , translations .value .Info .Code );
92+
7793 if (translations .value .Info .RTL ) {
7894 document .body .dataset .rtl = " yes" ;
7995
@@ -95,6 +111,7 @@ onAfterRouteLeave(() => {
95111 </script >
96112
97113<template >
114+ <FetchTranslations v-if =" shouldFetchTranslations" />
98115 <div v-show =" false" >
99116 <!-- This block contains UnoCSS classes that are not included in the bundle -->
100117 <span class =" i-mdi-github i-mdi-telegram i-fluent-add-square-24-regular i-fluent-folder-24-regular i-fluent-settings-24-regular i-fluent-chat-help-24-regular i-fluent-phone-update-24-regular i-fluent-animal-cat-24-regular i-fluent-people-16-regular i-fluent-edit-16-regular i-fluent-triangle-right-16-regular i-fluent-dismiss-circle-16-regular i-fluent-settings-16-regular i-fluent-tag-16-regular i-fluent-folder-16-regular i-fluent-folder-arrow-right-16-regular i-fluent-copy-arrow-right-16-regular i-fluent-delete-16-regular i-fluent-link-16-regular i-simple-icons-nixos i-simple-icons-archlinux i-simple-icons-debian i-simple-icons-flatpak i-simple-icons-linux i-simple-icons-gentoo i-mdi-microsoft-windows i-simple-icons-arm i-simple-icons-apple i-lucide-bolt i-lucide-square-mouse-pointer i-lucide-text-cursor-input i-lucide-ellipsis text-red-500 underline" />
0 commit comments