Skip to content

Commit 11ee2b1

Browse files
committed
feat: adapt new translations system in some places
1 parent 4ca35b9 commit 11ee2b1

File tree

13 files changed

+529
-478
lines changed

13 files changed

+529
-478
lines changed

public/translations/fa.json

Lines changed: 114 additions & 106 deletions
Large diffs are not rendered by default.

public/translations/pl.json

Lines changed: 114 additions & 106 deletions
Large diffs are not rendered by default.

public/translations/ru.json

Lines changed: 114 additions & 106 deletions
Large diffs are not rendered by default.

public/translations/ua.json

Lines changed: 114 additions & 106 deletions
Large diffs are not rendered by default.

src/App.vue

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<script setup lang="ts">
22
import Layout from "@/components/layout/Layout.vue";
33
import { 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";
55
import {
66
TranslationsKey,
7-
LocaleSelectorContextKey,
7+
TranslationsSelectorContextKey,
88
PageWrapperContextKey, TranslationsContextKey,
99
} from "@/constants/application.ts";
1010
import type { TranslationsSelectorType } from "@/types/translations-selector.type.ts";
@@ -13,17 +13,33 @@ import { useAccentAnimation } from "@/lib/stores/misc/accent-animations.ts";
1313
import English from "@/locales/en.json";
1414
import { shallowValidateTranslations } from "@/lib/translations/shallow-validate-translations.ts";
1515
import 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
1721
const scrollLocked = ref<boolean>(false);
1822
const 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);
2328
let 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+
2539
try {
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-
3852
function 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
4660
provide<(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
5064
const accentAnimationStore = useAccentAnimation();
5165
@@ -74,6 +88,8 @@ watchEffect(() => {
7488
pause();
7589
});
7690
watchEffect(() => {
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" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script setup lang="ts">
2+
import { useQuery } from "@tanstack/vue-query";
3+
import type { TranslationsType } from "@/types/translations.type.ts";
4+
import { shallowValidateTranslations } from "@/lib/translations/shallow-validate-translations.ts";
5+
import { TranslationsKey, TranslationsSelectorContextKey } from "@/constants/application.ts";
6+
import { inject } from "vue";
7+
import type { TranslationsSelectorType } from "@/types/translations-selector.type.ts";
8+
9+
const setTranslations = inject<TranslationsSelectorType>(TranslationsSelectorContextKey);
10+
const locale: string = navigator.language.slice(0, 2);
11+
12+
useQuery({
13+
"queryKey": ["translations", "background", locale],
14+
"queryFn" : async () => {
15+
const response = await fetch(`/translations/${locale}.json`);
16+
const data: unknown = await response.json();
17+
const validated: TranslationsType = shallowValidateTranslations(data);
18+
19+
setTranslations?.(validated);
20+
localStorage.setItem(TranslationsKey, JSON.stringify(validated));
21+
},
22+
});
23+
</script>

src/constants/application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { GithubReleasesType } from "@/types/github-releases.type.ts";
44
export const ApplicationName = "Freesm Launcher";
55

66
export const TranslationsContextKey = Symbol();
7-
export const LocaleSelectorContextKey = Symbol();
7+
export const TranslationsSelectorContextKey = Symbol();
88
export const LauncherContextKey = Symbol();
99
export const LauncherModalContextKey = Symbol();
1010
export const LauncherModalEmbedContextKey = Symbol();

src/constants/locales.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1 @@
1-
export const Locales = [
2-
{
3-
"Code": "en",
4-
"Name": "English",
5-
"Flag": "\uD83C\uDDEC\uD83C\uDDE7",
6-
"RTL" : false,
7-
},
8-
{
9-
"Code": "ru",
10-
"Name": "Русский",
11-
"Flag": "\uD83C\uDDF7\uD83C\uDDFA",
12-
"RTL" : false,
13-
},
14-
{
15-
"Code": "ua",
16-
"Name": "Українська",
17-
"Flag": "\uD83C\uDDFA\uD83C\uDDE6",
18-
"RTL" : false,
19-
},
20-
{
21-
"Code": "fa",
22-
"Name": "فارسی",
23-
"Flag": "\uD83C\uDDEE\uD83C\uDDF7",
24-
"RTL" : true,
25-
},
26-
{
27-
"Code": "pl",
28-
"Name": "Polski",
29-
"Flag": "\uD83C\uDDF5\uD83C\uDDF1",
30-
"RTL" : false,
31-
},
32-
] as const;
33-
export const LocalesArray = Locales.map(({ Code }) => Code);
34-
export const DefaultLocale = Locales[0].Code;
1+
export const Locales = ["en", "ru", "ua", "fa", "pl"] as const;

src/lib/translations/shallow-validate-translations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function shallowValidateTranslations(parsed: unknown): TranslationsType {
1212
typeof parsed?.Messages !== "object" ||
1313
parsed?.Messages === null
1414
) {
15-
return Translations;
15+
return Translations as TranslationsType;
1616
}
1717

1818
return parsed as TranslationsType;

src/pages/Themes.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<script setup lang="ts">
22
import Page from "@/components/layout/Page.vue";
3-
import { translate } from "@/lib/translations/translate.ts";
43
import { inject } from "vue";
5-
import type { ContextLocaleType } from "@/types/context-locale.type.ts";
6-
import { LocaleContextKey } from "@/constants/application.ts";
74
import ThemeGenerator from "@/components/themes/ThemeGenerator.vue";
5+
import { TranslationsContextKey } from "@/constants/application.ts";
6+
import type { TranslationsReferenceType } from "@/types/translations-reference.type.ts";
87
98
document.title = "Themes - Freesm Launcher";
109
document
@@ -14,7 +13,7 @@ document
1413
"Easily create and preview your Freesm Launcher theme in real-time.",
1514
);
1615
17-
const locale = inject<ContextLocaleType>(LocaleContextKey);
16+
const translations = inject<TranslationsReferenceType>(TranslationsContextKey);
1817
1918
const themeJson = JSON.stringify({
2019
"colors": {
@@ -89,10 +88,10 @@ console.log(themeJson, themeStyle);
8988
<Page>
9089
<div class="mx-auto max-w-240 flex flex-col gap-8 px-4 pt-12">
9190
<p class="select-text text-center text-balance text-5xl text-white font-bold sm:text-7xl">
92-
{{ translate("pages.themes.title", locale) }}
91+
{{ translations?.Messages?.["pages.themes.title"] }}
9392
</p>
9493
<p class="select-text text-center text-balance text-lg text-gray-400 sm:text-2xl">
95-
{{ translate("pages.themes.description", locale) }}
94+
{{ translations?.Messages?.["pages.themes.description"] }}
9695
</p>
9796
</div>
9897
<ThemeGenerator />

0 commit comments

Comments
 (0)