@yiiamee/multilinguist 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,11 +4,17 @@ import { computed, watch } from "vue";
|
|
|
4
4
|
export default function useLocalization(supportedLanguages, defaultLocale) {
|
|
5
5
|
const userSelectedLocale = useCookie("multilinguist-locale", { default: () => defaultLocale });
|
|
6
6
|
const { locale: userBrowserLocale } = useLocale(supportedLanguages, defaultLocale);
|
|
7
|
+
const localeFiles = import.meta.glob("@/public/locales/*.json", { eager: true });
|
|
7
8
|
const loadedLanguages = useState("loaded-languages", () => ({}));
|
|
8
|
-
const
|
|
9
|
-
if (!loadedLanguages.value[
|
|
10
|
-
const
|
|
11
|
-
|
|
9
|
+
const loadLocaleMessages = async (locale2) => {
|
|
10
|
+
if (!loadedLanguages.value[locale2]) {
|
|
11
|
+
const fileKey = `/public/locales/${locale2}.json`;
|
|
12
|
+
const messages = localeFiles[fileKey];
|
|
13
|
+
if (messages) {
|
|
14
|
+
loadedLanguages.value[locale2] = messages?.default;
|
|
15
|
+
} else {
|
|
16
|
+
throw new Error(`Locale file ${fileKey} not found`);
|
|
17
|
+
}
|
|
12
18
|
}
|
|
13
19
|
};
|
|
14
20
|
const userPrefferableLocale = computed(() => {
|
|
@@ -23,18 +29,13 @@ export default function useLocalization(supportedLanguages, defaultLocale) {
|
|
|
23
29
|
};
|
|
24
30
|
const setLocale = async (newLocale) => {
|
|
25
31
|
if (!loadedLanguages.value[newLocale] && supportedLanguages.includes(newLocale)) {
|
|
26
|
-
|
|
27
|
-
if (!response.ok) {
|
|
28
|
-
throw new Error(`Locale ${newLocale} not found`);
|
|
29
|
-
}
|
|
30
|
-
const loadedLocale = await response.json();
|
|
31
|
-
loadedLanguages.value[newLocale] = loadedLocale;
|
|
32
|
+
await loadLocaleMessages(newLocale);
|
|
32
33
|
}
|
|
33
34
|
userSelectedLocale.value = newLocale;
|
|
34
35
|
locale.value = newLocale;
|
|
35
36
|
};
|
|
36
37
|
const initLocalization = async () => {
|
|
37
|
-
await
|
|
38
|
+
await loadLocaleMessages(defaultLocale);
|
|
38
39
|
await setLocale(userPrefferableLocale.value);
|
|
39
40
|
};
|
|
40
41
|
watch(
|