Add auto detect of locales files and contrib guide

This commit is contained in:
Restray
2022-04-03 21:47:58 +02:00
parent 8cd561b8cc
commit e4f701b148
3 changed files with 103 additions and 32 deletions

View File

@@ -1,25 +1,32 @@
import i18n from 'sveltekit-i18n';
import lang from './lang.json';
import * as fs from 'fs';
// Get all translations files
const loaders = [];
const translations = {};
fs.readdir('src/lib/locales/', (err, files) => {
files.forEach((file) => {
if (file.endsWith('.json')) {
const lang_iso = file.replace('.json', '');
loaders.push({
locale: file.replace('.json', ''),
key: '',
/* @vite-ignore */
loader: async () => (await import(`./locales/${lang_iso}.json`)).default
});
translations[lang_iso] = { lang };
}
});
});
/** @type {import('sveltekit-i18n').Config} */
const config = {
fallbackLocale: 'en',
translations: {
en: { lang },
fr: { lang }
},
loaders: [
{
locale: 'en',
key: '',
loader: async () => (await import('./locales/en.json')).default
},
{
locale: 'fr',
key: '',
loader: async () => (await import('./locales/fr.json')).default
}
]
translations: translations,
loaders: loaders
};
export const { t, loading, locales, locale, loadTranslations } = new i18n(config);