feat: implemented new i18n system

This commit is contained in:
Benjamin Singleton 2025-06-14 11:57:28 -05:00
parent 4b4e8b7aa8
commit 032cfb2bfd
11 changed files with 254 additions and 124 deletions

View file

@ -2,6 +2,7 @@
import "./assets/style.scss"; import "./assets/style.scss";
import { ref, type Ref } from "vue"; import { ref, type Ref } from "vue";
import { SAMPLE } from "@repo/common/sample"; import { SAMPLE } from "@repo/common/sample";
import LocalePicker from "./components/organisms/LocalePicker.vue";
const burgerOpen: Ref<boolean> = ref<boolean>(false); const burgerOpen: Ref<boolean> = ref<boolean>(false);
@ -45,6 +46,7 @@ const toggleBurger = (): void => {
<RouterLink class="navbar-item" to="/resources">{{ <RouterLink class="navbar-item" to="/resources">{{
SAMPLE SAMPLE
}}</RouterLink> }}</RouterLink>
<LocalePicker />
</div> </div>
</div> </div>
</nav> </nav>

View file

@ -0,0 +1,43 @@
<script setup lang="ts">
import { LOCALE_IDS, localeId, useLocale, type LocaleId } from "@/i18n";
import { ref } from "vue";
const isOpen = ref<boolean>(false);
const toggleOpen = (): void => {
isOpen.value = !isOpen.value;
};
const setLocaleId = (id: LocaleId): void => {
localeId.value = id;
};
</script>
<template>
<div :class="['dropdown', isOpen && 'is-active']">
<div class="dropdown-trigger">
<button
class="button"
aria-haspopup="true"
aria-controls="dropdown-menu"
@click="toggleOpen()">
<span>{{ useLocale().value.localeName }}</span>
<span class="icon is-small">
<i class="fas fa-angle-down" aria-hidden="true"></i>
</span>
</button>
</div>
<div class="dropdown-menu" id="dropdown-menu" role="menu">
<div class="dropdown-content">
<a
v-for="(id, index) in LOCALE_IDS"
:key="index"
href="#"
:class="['dropdown-item', localeId === id && 'is-active']"
@click="setLocaleId(id)">
{{ useLocale({ locale: id }).value.localeName }}
</a>
</div>
</div>
</div>
</template>

View file

@ -1,25 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import HomeSectionWrapper from "@/components/molecules/HomeSectionWrapper.vue"; import HomeSectionWrapper from "@/components/molecules/HomeSectionWrapper.vue";
import "@/assets/style.scss"; import { useLocale } from "@/i18n";
import { useI18n } from "vue-i18n"; import { localizeLayout } from "@/utils/localizeLayout";
import type { MessageSchema } from "@/i18n/types";
import { computed } from "vue";
const { tm } = useI18n(); const locale = useLocale();
const sectionList = computed<MessageSchema["sections"]>(() => tm("sections"));
const sectionsWithImages = computed(() =>
sectionList.value.map((section) => {
if (!section.image) return section;
return {
...section,
image: new URL(`../../assets/${section.image}`, import.meta.url)
.href,
};
}),
);
console.log(sectionList.value);
</script> </script>
<template> <template>
@ -35,12 +19,12 @@ console.log(sectionList.value);
<section class="section container"> <section class="section container">
<HomeSectionWrapper <HomeSectionWrapper
v-for="(section, index) in sectionsWithImages" v-for="(section, index) in localizeLayout(locale.home)"
:key="index" :key="index"
:title="section.title" :title="section.title"
:text="section.text" :text="section.text"
:image="section.image" :image="section.image ?? undefined"
:alt="section.alt" :alt="section.alt ?? undefined"
:reverse="index % 2 !== 0" /> :reverse="index % 2 !== 0" />
</section> </section>
</div> </div>

View file

@ -1,25 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import LearningResourceWrapper from "@/components/molecules/LearningResourceWrapper.vue"; import LearningResourceWrapper from "@/components/molecules/LearningResourceWrapper.vue";
import "@/assets/style.scss"; import { useLocale } from "@/i18n";
import { useI18n } from "vue-i18n"; import { localizeLayout } from "@/utils/localizeLayout";
import type { MessageSchema } from "@/i18n/types";
import { computed } from "vue";
const { tm } = useI18n(); const locale = useLocale();
const resourceList = computed<MessageSchema["resources"]>(() =>
tm("resources"),
);
const resourcesWithImages = computed(() =>
resourceList.value.map((resource) => {
if (!resource.image) return resource;
return {
...resource,
image: new URL(`../../assets/${resource.image}`, import.meta.url)
.href,
};
}),
);
</script> </script>
<template> <template>
@ -30,7 +14,7 @@ const resourcesWithImages = computed(() =>
<section class="section container"> <section class="section container">
<LearningResourceWrapper <LearningResourceWrapper
v-for="(resource, index) in resourcesWithImages" v-for="(resource, index) in localizeLayout(locale.resources)"
:key="index" :key="index"
:title="resource.title" :title="resource.title"
:subtitle="resource.subtitle" :subtitle="resource.subtitle"

View file

@ -1,17 +1,56 @@
import { createI18n } from "vue-i18n";
import en_US from "../locales/en_US"; import en_US from "../locales/en_US";
import vp_VL from "../locales/vp_VL"; import vp_VL from "../locales/vp_VL";
import type { MessageSchema } from "./types"; import { computed, reactive, readonly, ref } from "vue";
import type { Locale } from "./locale";
const locales = { en_US, vp_VL } as const; export const LOCALE_IDS = ["en_US", "vp_VL"] as const;
export type LocaleId = (typeof LOCALE_IDS)[number];
export type Locale = keyof typeof locales; const locales = { en_US, vp_VL } as const satisfies Record<LocaleId, Locale>;
const i18n = createI18n<[MessageSchema], Locale>({ export const localeId = ref<LocaleId>("en_US");
legacy: false,
locale: "en_US" satisfies Locale,
fallbackLocale: "en_US" satisfies Locale,
messages: locales,
});
export default i18n; export function useLocale(opt: UseLocaleOptions = {}) {
const locale = computed<Locale>(() => {
return fallbackProxy(
locales[opt.locale ?? localeId.value],
locales["en_US"],
);
});
return readonly(reactive(locale));
}
export interface UseLocaleOptions {
locale?: LocaleId;
}
function fallbackProxy(obj: any, fallback: any) {
return new Proxy(obj, {
get: (target, key) => {
const value = target[key];
const fallbackValue = fallback[key];
if (value === undefined) {
return fallbackValue;
}
if (value === null && fallbackValue !== null) {
return fallbackValue;
}
if (
value === null
|| fallbackValue === null
|| typeof value !== "object"
|| typeof fallbackValue !== "object"
) {
return value;
}
return fallbackProxy(value, fallbackValue);
},
set: () => {
throw new Error("Cannot mutate locale at runtime");
},
});
}

View file

@ -0,0 +1,39 @@
export interface Locale {
localeName: string;
home: Layout<HomeSections>;
resources: Layout<Resources>;
}
export interface Layout<T> {
layout: (keyof T)[];
data: { [K in keyof T]: T[K] | null };
}
export interface HomeSections {
whatIsViossa: HomeSection;
historyOfViossa: HomeSection;
community: HomeSection;
}
export interface HomeSection {
title: string;
text: string;
image: string | null;
alt: string | null;
}
export interface Resources {
discord: Resource;
}
export interface Resource {
title: string;
subtitle: string;
desc: string;
link: string;
rulesLink: string;
image: string;
alt: string;
joinText: string;
rulesText: string;
}

View file

@ -1,8 +0,0 @@
import en_US from "../locales/en_US";
export interface MessageSchema extends Broaden<typeof en_US> {}
type Broaden<T> = {
[K in keyof T]: T[K] extends object ? Broaden<T[K]>
: T[K] extends string ? string
: T[K];
} & {};

View file

@ -1,33 +1,47 @@
import type { Locale } from "@/i18n/locale";
import flakkaImg from "@/assets/flakka.png";
import discordImg from "@/assets/discord.png";
export default { export default {
sections: [ localeName: "English",
{ home: {
layout: ["whatIsViossa", "historyOfViossa", "community"],
data: {
whatIsViossa: {
title: "What is Viossa?", title: "What is Viossa?",
text: "Viossa is a community-created artificial pidgin language, created to simulate the formation of natural pidgin languages. Viossa is characterized by its lack of standardization, with each speaker developing a personal idiolect. Spelling and pronunciation can vary greatly, and serve as a form of personal self-expression. Viossa is learnt and taught entirely by immersion — translation is prohibited while learning.", text: "Viossa is a community-created artificial pidgin language, created to simulate the formation of natural pidgin languages. Viossa is characterized by its lack of standardization, with each speaker developing a personal idiolect. Spelling and pronunciation can vary greatly, and serve as a form of personal self-expression. Viossa is learnt and taught entirely by immersion — translation is prohibited while learning.",
image: "flakka.png", image: flakkaImg,
alt: "Flag of the Viossa Language", alt: "Flag of the Viossa Language",
}, },
{ historyOfViossa: {
title: "History of Viossa", title: "History of Viossa",
text: "Viossa began as a Skype group in 2014, created by members of the r/conlangs community on Reddit, as an experiment to simulate the formation of a pidgin language. Pidgins are simplified languages resulting from contact between populations with no shared common language. Unlike most pidgins, which usually have two to three contributor languages, Viossa comes from many diverse languages. This is because people from all around the world helped to contribute to Viossa's vocabulary.", text: "Viossa began as a Skype group in 2014, created by members of the r/conlangs community on Reddit, as an experiment to simulate the formation of a pidgin language. Pidgins are simplified languages resulting from contact between populations with no shared common language. Unlike most pidgins, which usually have two to three contributor languages, Viossa comes from many diverse languages. This is because people from all around the world helped to contribute to Viossa's vocabulary.",
image: "flakka.png", image: flakkaImg,
alt: "Flag of the Viossa Language", alt: "Flag of the Viossa Language",
}, },
{ community: {
title: "Community", title: "Community",
text: "The Viossa community is rich and colourful, drawing from many global traditions due to its worldwide online membership. Since the teaching culture puts an emphasis on linguistic immersion, and discourages prescriptivism, the culture of Viossa is as diverse and varied as the language and the people who speak it. For many, their personal dialect is a key form of identity and expression. The fluid nature of Viossa and lack of defined meanings makes Viossa popular for creative purposes, such as poetry and songwriting.", text: "The Viossa community is rich and colourful, drawing from many global traditions due to its worldwide online membership. Since the teaching culture puts an emphasis on linguistic immersion, and discourages prescriptivism, the culture of Viossa is as diverse and varied as the language and the people who speak it. For many, their personal dialect is a key form of identity and expression. The fluid nature of Viossa and lack of defined meanings makes Viossa popular for creative purposes, such as poetry and songwriting.",
image: null,
alt: null,
}, },
], },
resources: [ },
{ resources: {
layout: ["discord"],
data: {
discord: {
title: "Discord Server", title: "Discord Server",
subtitle: "This is where most of the action happens! Hop on in!", subtitle:
"This is where most of the action happens! Hop on in!",
desc: "Originally started in 2015 something something read the rules here, then click the link below to join!", desc: "Originally started in 2015 something something read the rules here, then click the link below to join!",
link: "https://discord.gg/g3mG2gYjZD", link: "https://discord.gg/g3mG2gYjZD",
rulesLink: "https://viossadiskordserver.github.io/rules", rulesLink: "https://viossadiskordserver.github.io/rules",
image: "discord.png", image: discordImg,
alt: "Discord logo", alt: "Discord logo",
joinText: "Join", joinText: "Join",
rulesText: "Rules", rulesText: "Rules",
}, },
], },
}; },
} as const satisfies Locale;

View file

@ -1,35 +1,53 @@
import type { MessageSchema } from "@/i18n/types"; import type { Locale } from "@/i18n/locale";
import flakkaImg from "@/assets/flakka.png";
import discordImg from "@/assets/discord.png";
export default { export default {
sections: [ localeName: "Viossa",
{ home: {
layout: [
"whatIsViossa",
"community",
"historyOfViossa",
"community",
"community",
],
data: {
whatIsViossa: {
title: "Kafaen afto Viossa", title: "Kafaen afto Viossa",
text: "Viossa tte glossa mahena grun vi nai vil fshtojena na bakadjin, grun vi svinnur ja! De aldjin zovti lera ne", text: "Viossa tte glossa mahena grun vi nai vil fshtojena na bakadjin, grun vi svinnur ja! De aldjin zovti lera ne",
image: "flakka.png", image: flakkaImg,
alt: "Flag of the Viossa Language", alt: "Flag of the Viossa Language",
}, },
{ historyOfViossa: {
title: "History of Viossa", title: "History of Viossa",
text: "Viossa began as a Skype group in 2014, created by members of the r/conlangs community on Reddit, as an experiment to simulate the formation of a pidgin language. Pidgins are simplified languages resulting from contact between populations with no shared common language. Unlike most pidgins, which usually have two to three contributor languages, Viossa comes from many diverse languages. This is because people from all around the world helped to contribute to Viossa's vocabulary.", text: "Viossa began as a Skype group in 2014, created by members of the r/conlangs community on Reddit, as an experiment to simulate the formation of a pidgin language. Pidgins are simplified languages resulting from contact between populations with no shared common language. Unlike most pidgins, which usually have two to three contributor languages, Viossa comes from many diverse languages. This is because people from all around the world helped to contribute to Viossa's vocabulary.",
image: "flakka.png", image: flakkaImg,
alt: "Flag of the Viossa Language", alt: "Flag of the Viossa Language",
}, },
{ community: {
title: "Community", title: "Community",
text: "The Viossa community is rich and colourful, drawing from many global traditions due to its worldwide online membership. Since the teaching culture puts an emphasis on linguistic immersion, and discourages prescriptivism, the culture of Viossa is as diverse and varied as the language and the people who speak it. For many, their personal dialect is a key form of identity and expression. The fluid nature of Viossa and lack of defined meanings makes Viossa popular for creative purposes, such as poetry and songwriting.", text: "The Viossa community is rich and colourful, drawing from many global traditions due to its worldwide online membership. Since the teaching culture puts an emphasis on linguistic immersion, and discourages prescriptivism, the culture of Viossa is as diverse and varied as the language and the people who speak it. For many, their personal dialect is a key form of identity and expression. The fluid nature of Viossa and lack of defined meanings makes Viossa popular for creative purposes, such as poetry and songwriting.",
image: null,
alt: null,
}, },
], },
resources: [ },
{ resources: {
layout: ["discord"],
data: {
discord: {
title: "Discord Server", title: "Discord Server",
subtitle: "This is where most of the action happens! Hop on in!", subtitle:
"This is where most of the action happens! Hop on in!",
desc: "Originally started in 2015 something something read the rules here, then click the link below to join!", desc: "Originally started in 2015 something something read the rules here, then click the link below to join!",
link: "https://discord.gg/g3mG2gYjZD", link: "https://discord.gg/g3mG2gYjZD",
rulesLink: "https://viossadiskordserver.github.io/rules", rulesLink: "https://viossadiskordserver.github.io/rules",
image: "discord.png", image: discordImg,
alt: "Discord logo", alt: "Discord logo",
joinText: "Join", joinText: "Join",
rulesText: "Rules", rulesText: "Rules",
}, },
], },
} as const satisfies MessageSchema; },
} as const satisfies Locale;

View file

@ -1,6 +1,5 @@
import { createApp } from "vue"; import { createApp } from "vue";
import App from "./App.vue"; import App from "./App.vue";
import router from "./routes"; import router from "./routes";
import i18n from "./i18n";
createApp(App).use(i18n).use(router).mount("#app"); createApp(App).use(router).mount("#app");

View file

@ -0,0 +1,16 @@
import type { Layout } from "@/i18n/locale";
import type { DeepReadonly } from "vue";
export function localizeLayout<T>(
layout: DeepReadonly<Layout<T>>,
): T[keyof T][] {
const sections: T[keyof T][] = [];
for (const sectionId of layout.layout) {
const section = (layout.data as T)[sectionId as keyof T];
if (section) {
sections.push(section);
}
}
return sections;
}