feat: implemented new i18n system
This commit is contained in:
parent
4b4e8b7aa8
commit
032cfb2bfd
11 changed files with 254 additions and 124 deletions
|
|
@ -2,6 +2,7 @@
|
|||
import "./assets/style.scss";
|
||||
import { ref, type Ref } from "vue";
|
||||
import { SAMPLE } from "@repo/common/sample";
|
||||
import LocalePicker from "./components/organisms/LocalePicker.vue";
|
||||
|
||||
const burgerOpen: Ref<boolean> = ref<boolean>(false);
|
||||
|
||||
|
|
@ -45,6 +46,7 @@ const toggleBurger = (): void => {
|
|||
<RouterLink class="navbar-item" to="/resources">{{
|
||||
SAMPLE
|
||||
}}</RouterLink>
|
||||
<LocalePicker />
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
|
|
|||
43
apps/vdn-static/src/components/organisms/LocalePicker.vue
Normal file
43
apps/vdn-static/src/components/organisms/LocalePicker.vue
Normal 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>
|
||||
|
|
@ -1,25 +1,9 @@
|
|||
<script setup lang="ts">
|
||||
import HomeSectionWrapper from "@/components/molecules/HomeSectionWrapper.vue";
|
||||
import "@/assets/style.scss";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import type { MessageSchema } from "@/i18n/types";
|
||||
import { computed } from "vue";
|
||||
import { useLocale } from "@/i18n";
|
||||
import { localizeLayout } from "@/utils/localizeLayout";
|
||||
|
||||
const { tm } = useI18n();
|
||||
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);
|
||||
const locale = useLocale();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
@ -35,12 +19,12 @@ console.log(sectionList.value);
|
|||
|
||||
<section class="section container">
|
||||
<HomeSectionWrapper
|
||||
v-for="(section, index) in sectionsWithImages"
|
||||
v-for="(section, index) in localizeLayout(locale.home)"
|
||||
:key="index"
|
||||
:title="section.title"
|
||||
:text="section.text"
|
||||
:image="section.image"
|
||||
:alt="section.alt"
|
||||
:image="section.image ?? undefined"
|
||||
:alt="section.alt ?? undefined"
|
||||
:reverse="index % 2 !== 0" />
|
||||
</section>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,25 +1,9 @@
|
|||
<script setup lang="ts">
|
||||
import LearningResourceWrapper from "@/components/molecules/LearningResourceWrapper.vue";
|
||||
import "@/assets/style.scss";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import type { MessageSchema } from "@/i18n/types";
|
||||
import { computed } from "vue";
|
||||
import { useLocale } from "@/i18n";
|
||||
import { localizeLayout } from "@/utils/localizeLayout";
|
||||
|
||||
const { tm } = useI18n();
|
||||
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,
|
||||
};
|
||||
}),
|
||||
);
|
||||
const locale = useLocale();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
@ -30,7 +14,7 @@ const resourcesWithImages = computed(() =>
|
|||
|
||||
<section class="section container">
|
||||
<LearningResourceWrapper
|
||||
v-for="(resource, index) in resourcesWithImages"
|
||||
v-for="(resource, index) in localizeLayout(locale.resources)"
|
||||
:key="index"
|
||||
:title="resource.title"
|
||||
:subtitle="resource.subtitle"
|
||||
|
|
|
|||
|
|
@ -1,17 +1,56 @@
|
|||
import { createI18n } from "vue-i18n";
|
||||
import en_US from "../locales/en_US";
|
||||
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>({
|
||||
legacy: false,
|
||||
locale: "en_US" satisfies Locale,
|
||||
fallbackLocale: "en_US" satisfies Locale,
|
||||
messages: locales,
|
||||
});
|
||||
export const localeId = ref<LocaleId>("en_US");
|
||||
|
||||
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");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
39
apps/vdn-static/src/i18n/locale.ts
Normal file
39
apps/vdn-static/src/i18n/locale.ts
Normal 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;
|
||||
}
|
||||
|
|
@ -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];
|
||||
} & {};
|
||||
|
|
@ -1,33 +1,47 @@
|
|||
import type { Locale } from "@/i18n/locale";
|
||||
import flakkaImg from "@/assets/flakka.png";
|
||||
import discordImg from "@/assets/discord.png";
|
||||
|
||||
export default {
|
||||
sections: [
|
||||
{
|
||||
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.",
|
||||
image: "flakka.png",
|
||||
alt: "Flag of the Viossa Language",
|
||||
localeName: "English",
|
||||
home: {
|
||||
layout: ["whatIsViossa", "historyOfViossa", "community"],
|
||||
data: {
|
||||
whatIsViossa: {
|
||||
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.",
|
||||
image: flakkaImg,
|
||||
alt: "Flag of the Viossa Language",
|
||||
},
|
||||
historyOfViossa: {
|
||||
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.",
|
||||
image: flakkaImg,
|
||||
alt: "Flag of the Viossa Language",
|
||||
},
|
||||
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.",
|
||||
image: null,
|
||||
alt: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
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.",
|
||||
image: "flakka.png",
|
||||
alt: "Flag of the Viossa Language",
|
||||
},
|
||||
resources: {
|
||||
layout: ["discord"],
|
||||
data: {
|
||||
discord: {
|
||||
title: "Discord Server",
|
||||
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!",
|
||||
link: "https://discord.gg/g3mG2gYjZD",
|
||||
rulesLink: "https://viossadiskordserver.github.io/rules",
|
||||
image: discordImg,
|
||||
alt: "Discord logo",
|
||||
joinText: "Join",
|
||||
rulesText: "Rules",
|
||||
},
|
||||
},
|
||||
{
|
||||
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.",
|
||||
},
|
||||
],
|
||||
resources: [
|
||||
{
|
||||
title: "Discord Server",
|
||||
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!",
|
||||
link: "https://discord.gg/g3mG2gYjZD",
|
||||
rulesLink: "https://viossadiskordserver.github.io/rules",
|
||||
image: "discord.png",
|
||||
alt: "Discord logo",
|
||||
joinText: "Join",
|
||||
rulesText: "Rules",
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
} as const satisfies Locale;
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
sections: [
|
||||
{
|
||||
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",
|
||||
image: "flakka.png",
|
||||
alt: "Flag of the Viossa Language",
|
||||
localeName: "Viossa",
|
||||
home: {
|
||||
layout: [
|
||||
"whatIsViossa",
|
||||
"community",
|
||||
"historyOfViossa",
|
||||
"community",
|
||||
"community",
|
||||
],
|
||||
data: {
|
||||
whatIsViossa: {
|
||||
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",
|
||||
image: flakkaImg,
|
||||
alt: "Flag of the Viossa Language",
|
||||
},
|
||||
historyOfViossa: {
|
||||
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.",
|
||||
image: flakkaImg,
|
||||
alt: "Flag of the Viossa Language",
|
||||
},
|
||||
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.",
|
||||
image: null,
|
||||
alt: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
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.",
|
||||
image: "flakka.png",
|
||||
alt: "Flag of the Viossa Language",
|
||||
},
|
||||
resources: {
|
||||
layout: ["discord"],
|
||||
data: {
|
||||
discord: {
|
||||
title: "Discord Server",
|
||||
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!",
|
||||
link: "https://discord.gg/g3mG2gYjZD",
|
||||
rulesLink: "https://viossadiskordserver.github.io/rules",
|
||||
image: discordImg,
|
||||
alt: "Discord logo",
|
||||
joinText: "Join",
|
||||
rulesText: "Rules",
|
||||
},
|
||||
},
|
||||
{
|
||||
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.",
|
||||
},
|
||||
],
|
||||
resources: [
|
||||
{
|
||||
title: "Discord Server",
|
||||
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!",
|
||||
link: "https://discord.gg/g3mG2gYjZD",
|
||||
rulesLink: "https://viossadiskordserver.github.io/rules",
|
||||
image: "discord.png",
|
||||
alt: "Discord logo",
|
||||
joinText: "Join",
|
||||
rulesText: "Rules",
|
||||
},
|
||||
],
|
||||
} as const satisfies MessageSchema;
|
||||
},
|
||||
} as const satisfies Locale;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { createApp } from "vue";
|
||||
import App from "./App.vue";
|
||||
import router from "./routes";
|
||||
import i18n from "./i18n";
|
||||
|
||||
createApp(App).use(i18n).use(router).mount("#app");
|
||||
createApp(App).use(router).mount("#app");
|
||||
|
|
|
|||
16
apps/vdn-static/src/utils/localizeLayout.ts
Normal file
16
apps/vdn-static/src/utils/localizeLayout.ts
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue