mirror of
https://github.com/Spythere/pojazdownik.git
synced 2026-05-03 05:18:10 +00:00
cleanup i18n; wykrywanie języka
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import localePL from './locales/pl.json';
|
||||
import localeEN from './locales/en.json';
|
||||
import { createI18n } from 'vue-i18n';
|
||||
import axios from 'axios';
|
||||
|
||||
type LocaleMessageSchema = typeof localePL;
|
||||
type LocaleKey = 'en' | 'pl';
|
||||
|
||||
const locales: { [key in LocaleKey]: LocaleMessageSchema } = {
|
||||
en: localeEN,
|
||||
pl: localePL,
|
||||
};
|
||||
|
||||
const i18n = createI18n<[LocaleMessageSchema], 'en' | 'pl'>({
|
||||
locale: /^pl\b/.test(navigator.language) ? 'pl' : 'en',
|
||||
fallbackLocale: 'pl',
|
||||
legacy: false,
|
||||
globalInjection: true,
|
||||
messages: locales,
|
||||
});
|
||||
|
||||
async function fetchBackendTranslations() {
|
||||
const localeData = (await axios.get(`https://spythere.github.io/api/td2/data/locales.json`)).data;
|
||||
|
||||
i18n.global.mergeLocaleMessage('pl', localeData.pl);
|
||||
i18n.global.mergeLocaleMessage('en', localeData.en);
|
||||
}
|
||||
|
||||
fetchBackendTranslations();
|
||||
|
||||
export default i18n;
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
import localePL from './locales/pl.json';
|
||||
import localeEN from './locales/en.json';
|
||||
import { I18n } from 'vue-i18n';
|
||||
|
||||
export type LocaleMessageSchema = typeof localePL;
|
||||
type LocaleKey = 'en' | 'pl';
|
||||
|
||||
export const locales: { [key in LocaleKey]: LocaleMessageSchema } = {
|
||||
en: localeEN,
|
||||
pl: localePL,
|
||||
};
|
||||
|
||||
export async function fetchBackendTranslations(i18n: I18n) {
|
||||
const localeData = await (await fetch(`https://spythere.github.io/api/td2/data/locales.json`)).json();
|
||||
|
||||
i18n.global.mergeLocaleMessage('pl', localeData.pl);
|
||||
i18n.global.mergeLocaleMessage('en', localeData.en);
|
||||
}
|
||||
+1
-13
@@ -1,23 +1,11 @@
|
||||
import { createApp } from 'vue';
|
||||
import { createPinia } from 'pinia';
|
||||
import { registerSW } from 'virtual:pwa-register';
|
||||
import { createI18n } from 'vue-i18n';
|
||||
|
||||
import App from './App.vue';
|
||||
import { LocaleMessageSchema, fetchBackendTranslations, locales } from './i18n';
|
||||
|
||||
import i18n from './i18n-setup';
|
||||
const pinia = createPinia();
|
||||
|
||||
const i18n = createI18n<[LocaleMessageSchema], 'en' | 'pl'>({
|
||||
locale: 'en',
|
||||
fallbackLocale: 'pl',
|
||||
legacy: false,
|
||||
globalInjection: true,
|
||||
messages: locales,
|
||||
});
|
||||
|
||||
fetchBackendTranslations(i18n);
|
||||
|
||||
registerSW({
|
||||
immediate: true,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user