UNPKG

@mr-hope/vuepress-shared

Version:
81 lines 3.24 kB
import { lang2PathConfig, path2langConfig, supportedLangs } from "./config"; import { deepAssign } from "../assign"; const reportStatus = {}; /** Check if the lang is supported */ export const checkLang = (lang = "") => supportedLangs.includes(lang); export const showLangError = (lang, plugin = "") => { if (!reportStatus[lang]) { console.warn(`${lang} locates config is missing, and will return 'en-US' instead. ${lang === "root" ? "" : `You can contribute to https://github.com/vuepress-theme-hope/vuepress-theme-hope-v1/blob/main/packages/${plugin || "<YOUR PLUGIN>"}/src/node/locales.ts in this repository. `}Note: This warning will be shown only once`); reportStatus[lang] = true; } }; /** Get language from path */ export const path2Lang = (path = "") => { if (path in path2langConfig) return path2langConfig[path]; console.error(`${path} isn’t assign with a lang, and will return 'en-US' instead.`); return "en-US"; }; /** Get path from language */ export const lang2Path = (lang = "") => { if (lang in lang2PathConfig) return lang2PathConfig[lang]; console.error(`${lang} has no path config, and will return '/' instead.`); return "/"; }; /** * Get language of root directory * * @param context VuePress Node Context * @returns root language */ export const getRootLang = (context) => { var _a, _b; // infer from siteLocale const siteLocales = context.siteConfig.locales; if ((siteLocales === null || siteLocales === void 0 ? void 0 : siteLocales["/"]) && checkLang((_a = siteLocales["/"]) === null || _a === void 0 ? void 0 : _a.lang)) return siteLocales["/"].lang; // infer from themeLocale const themeLocales = context.themeConfig.locales; if ((themeLocales === null || themeLocales === void 0 ? void 0 : themeLocales["/"]) && checkLang((_b = themeLocales["/"]) === null || _b === void 0 ? void 0 : _b.lang)) return themeLocales["/"].lang; showLangError("root"); return "en-US"; }; /** * Get the infer language path from root directory language * * @param context VuePress Node Context * @returns infer language */ export const getRootLangPath = (context) => lang2Path(getRootLang(context)); export const getLocalePaths = (context) => Array.from(new Set([ ...Object.keys(context.siteConfig.locales || {}), ...Object.keys(context.themeConfig.locales || {}), ])); /** * Get final locale options to passed to client * * @param context VuePress Node Context * @param defaultLocalesConfig default locale config * @param userLocalesConfig user locale config * @returns final locale config */ export const getLocales = (context, defaultLocalesConfig, userLocalesConfig = {}) => { const rootPath = getRootLangPath(context); return Object.fromEntries([ ...getLocalePaths(context).map((localePath) => [ localePath, deepAssign({}, userLocalesConfig[localePath] || {}, defaultLocalesConfig[localePath] || {}), ]), [ "/", deepAssign({}, userLocalesConfig["/"] || userLocalesConfig[rootPath] || {}, defaultLocalesConfig[rootPath] || {}), ], ]); }; //# sourceMappingURL=helpers.js.map