- Closes #9
- Removed dependency on vue-i18n
- Localization Changes
This commit is contained in:
Shiny Nematoda 2022-09-12 15:00:54 +00:00
parent 44db54737b
commit fcc3d455c6
No known key found for this signature in database
GPG key ID: 6506D50F5613A42D
18 changed files with 143 additions and 270 deletions

View file

@ -1,6 +1,19 @@
import { reactive } from 'vue';
import { reactive, ref } from 'vue';
import { defineStore } from 'pinia';
import en from '@/locales/en.json';
export const SUPPORTED_LOCALES = [
{
code: 'en',
name: 'English',
},
{
code: 'fr',
name: 'French',
},
];
export const useNav = defineStore('nav', () => {
const state = reactive({
search: '',
@ -9,3 +22,31 @@ export const useNav = defineStore('nav', () => {
return { state };
});
export const useI18n = defineStore('i18n', () => {
const locale = ref('en'),
map = ref({
en: en,
});
function t(path) {
const msgs = map.value[locale.value],
fallback = map.value['en'],
keys = path.split('.'),
translate = msg => keys.reduce((obj, i) => obj?.[i], msg),
translated = translate(msgs) || translate(fallback);
return translated || path;
}
function setupLocale(code) {
import(`@/locales/${code}.json`)
.then(mod => mod.default)
.then(mod => {
map.value[code] = mod;
locale.value = code;
});
}
return { locale, map, t, setupLocale };
});