UNPKG

next-translations

Version:
99 lines (98 loc) 5.75 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/no-explicit-any */ import fse from "fs-extra"; import path from "path"; import { fileURLToPath } from "url"; //@ts-ignore import translationsConfigUser from "../../translations.config.js"; const translationsConfig = { defaultLocale: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.defaultLocale) || "en", locales: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.locales) || ["en"], outputFolderTranslations: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.outputFolderTranslations) || "/public/locales", defaultLocaleWithMultirouting: translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.defaultLocaleWithMultirouting, constNamespaces: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.constNamespaces) || ["common"], }; const allTranslationsLanguages = translationsConfig.locales; const getTranslationsFromFiles = (locale = translationsConfig.defaultLocale, ns = []) => __awaiter(void 0, void 0, void 0, function* () { var _a; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const translations = {}; const uniqueArray = (_a = ns === null || ns === void 0 ? void 0 : ns.filter((value, index, self) => { return self.indexOf(value) === index && !!value; })) !== null && _a !== void 0 ? _a : []; try { for (const namespace of uniqueArray) { const folderPath = `../..${translationsConfig.outputFolderTranslations}/${locale}/${namespace}.json`; const pathToFile = path.resolve(__dirname, folderPath); const exists = yield fse.pathExists(pathToFile); if (exists) { const translationsJson = yield fse.readJson(pathToFile); if (translationsJson) { translations[namespace] = translationsJson; } } else { console.error("Path not found!!!"); } } return { translations: translations }; } catch (err) { console.error(err); } }); function getTranslationsProps(ctx, ns = []) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { let locale = translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.defaultLocale; if ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.params) === null || _a === void 0 ? void 0 : _a.locale) { const isInLocales = (_b = translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.locales) === null || _b === void 0 ? void 0 : _b.some((itemLocale) => { var _a; return itemLocale === ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.params) === null || _a === void 0 ? void 0 : _a.locale); }); locale = isInLocales ? (_c = ctx === null || ctx === void 0 ? void 0 : ctx.params) === null || _c === void 0 ? void 0 : _c.locale : translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.defaultLocale; } const defaultNamespacesToUseInAllPages = translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.constNamespaces; ctx.locale = locale; ctx.locales = translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.locales; ctx.defaultLocale = translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.defaultLocale; const props = Object.assign({}, (yield getTranslationsFromFiles(locale, [ ...ns, ...defaultNamespacesToUseInAllPages, ]))); return props; }); } const getPaths = (filterDefaultLocale = true) => { var _a, _b; return (_b = (filterDefaultLocale ? (_a = translationsConfig.locales) === null || _a === void 0 ? void 0 : _a.filter((item) => { if (!(translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.defaultLocaleWithMultirouting)) { return item !== (translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.defaultLocale); } else { return true; } }) : translationsConfig.locales)) === null || _b === void 0 ? void 0 : _b.map((lng) => ({ params: { locale: lng, }, })); }; const getStaticPaths = () => { return { fallback: false, paths: getPaths(), }; }; export { allTranslationsLanguages, getTranslationsProps, getPaths, getStaticPaths, };