UNPKG

next-translations

Version:
67 lines (66 loc) 3.69 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 */ import * as dotenv from "dotenv"; import fse from "fs-extra"; import fetch from "node-fetch"; import path from "path"; import { fileURLToPath } from "url"; // @ts-ignore import translationsConfigUser from "../../translations.config.js"; dotenv.config(); const translationsConfig = { locales: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.locales) || ["en"], linkFetchTranslations: translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.linkFetchTranslations, outputFolderTranslations: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.outputFolderTranslations) || "/public/locales", namespacesToFetch: (translationsConfigUser === null || translationsConfigUser === void 0 ? void 0 : translationsConfigUser.namespacesToFetch) || ["common"], }; const fetchLanguages = (language, namespace) => __awaiter(void 0, void 0, void 0, function* () { var _a; const linkToFetch = translationsConfig.linkFetchTranslations((_a = process.env) === null || _a === void 0 ? void 0 : _a.NEXT_PUBLIC_NEXT_TRANSLATIONS_APP_ENV, language, namespace); const result = yield fetch(linkToFetch); if (!result) { return {}; } return result.json(); }); export const downloadLanguages = () => __awaiter(void 0, void 0, void 0, function* () { var _b, _c; if (!(translationsConfig === null || translationsConfig === void 0 ? void 0 : translationsConfig.linkFetchTranslations)) { console.log("No detected link to download translations :("); return; } console.log("Fetching translations from api..."); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); try { for (const lang of (_b = translationsConfig.locales) !== null && _b !== void 0 ? _b : []) { for (const namespace of (_c = translationsConfig.namespacesToFetch) !== null && _c !== void 0 ? _c : []) { const folderPath = `../..${translationsConfig.outputFolderTranslations}/${lang}/${namespace}.json`; const pathToFile = path.resolve(__dirname, folderPath); try { const data = yield fetchLanguages(lang, namespace); yield fse.outputFile(pathToFile, JSON.stringify(data !== null && data !== void 0 ? data : {})); } catch (err) { console.log(`Fail on downloading translations in lang: ${lang}, namespace: ${namespace}`); yield fse.outputFile(pathToFile, JSON.stringify({})); } } } console.log("Fetching translations success!!!"); return 0; } catch (err) { console.error(err); return 0; } }); downloadLanguages();