config ln array changed

This commit is contained in:
Al Murad Uzzaman
2024-05-16 17:11:25 +06:00
parent 97d6f22dcb
commit c137b8afcd
24 changed files with 200 additions and 362 deletions
+23 -41
View File
@@ -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),
);