mirror of
https://github.com/10h30/astroplate.git
synced 2026-07-18 22:23:27 +09:00
config ln array changed
This commit is contained in:
+23
-41
@@ -1,14 +1,18 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import config from "@/config/config.json";
|
||||
import languagesJSON from "@/config/language.json";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
const { defaultLang } = config.language;
|
||||
|
||||
const menusFolderPath = './src/config';
|
||||
const menusFolderPath = "./src/config";
|
||||
|
||||
const locales = fs.readdirSync(menusFolderPath)
|
||||
.filter(file => /^menu\.[a-z]{2}\.json$/.test(file))
|
||||
.map(file => {
|
||||
const locales = fs
|
||||
.readdirSync(menusFolderPath)
|
||||
.filter((file) => /^menu\.[a-z]{2}\.json$/.test(file))
|
||||
.map((file) => {
|
||||
const filePath = path.join(menusFolderPath, file);
|
||||
const localeName = file.split('.')[1]; // Extract language code from file name
|
||||
const localeData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
const localeName = file.split(".")[1]; // Extract language code from file name
|
||||
const localeData = JSON.parse(fs.readFileSync(filePath, "utf8"));
|
||||
return { [localeName]: localeData };
|
||||
})
|
||||
.reduce((accumulator, locale) => {
|
||||
@@ -19,44 +23,22 @@ const locales = fs.readdirSync(menusFolderPath)
|
||||
const languages = Object.keys(locales);
|
||||
|
||||
// Export the locales and languages
|
||||
export { locales, languages };
|
||||
export { languages, locales };
|
||||
|
||||
export function getLangFromUrl(url: URL): string {
|
||||
const [, lang] = url.pathname.split('/');
|
||||
const [, lang] = url.pathname.split("/");
|
||||
if (locales.hasOwnProperty(lang)) {
|
||||
return lang;
|
||||
}
|
||||
return 'en';
|
||||
return defaultLang;
|
||||
}
|
||||
|
||||
export const getTranslations = async (lang: string) => {
|
||||
const menu = await import(`../../config/menu.${lang || defaultLang}.json`);
|
||||
const dictionary = await import(`../../i18n/${lang || defaultLang}.json`);
|
||||
return { ...menu, ...dictionary };
|
||||
};
|
||||
|
||||
|
||||
// import { ui } from '@/i18n/ui';
|
||||
// import config from "@/config/config.json";
|
||||
// const {defaultLang}: {defaultLang : keyof typeof ui} = config.language as any;
|
||||
|
||||
// export function useTranslations(lang: keyof typeof ui) {
|
||||
// return function t(key: keyof typeof ui[typeof defaultLang]) {
|
||||
// return ui[lang][key] || ui[defaultLang][key];
|
||||
// }
|
||||
// }
|
||||
|
||||
import { ui } from '@/i18n/ui';
|
||||
import config from "@/config/config.json";
|
||||
|
||||
const { defaultLang }: { defaultLang: keyof typeof ui } = config.language as any;
|
||||
|
||||
export function useTranslations(lang?: keyof typeof ui) {
|
||||
const activeLang = lang || defaultLang;
|
||||
|
||||
return function t(key: keyof typeof ui[typeof defaultLang]): string {
|
||||
const translation = ui[activeLang][key];
|
||||
|
||||
// If not found, fall back to the default language
|
||||
if (translation) {
|
||||
return translation;
|
||||
} else {
|
||||
return ui[defaultLang][key];
|
||||
}
|
||||
};
|
||||
}
|
||||
export const supportedLang = [""].concat(
|
||||
languagesJSON.map((lang) => lang.languageCode),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user