@mr-hope/vuepress-shared
Version:
Shared for vuepress-theme-hope
92 lines • 3.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocales = exports.getLocalePaths = exports.getRootLangPath = exports.getRootLang = exports.lang2Path = exports.path2Lang = exports.showLangError = exports.checkLang = void 0;
const config_1 = require("./config");
const assign_1 = require("../assign");
const reportStatus = {};
/** Check if the lang is supported */
const checkLang = (lang = "") => config_1.supportedLangs.includes(lang);
exports.checkLang = checkLang;
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;
}
};
exports.showLangError = showLangError;
/** Get language from path */
const path2Lang = (path = "") => {
if (path in config_1.path2langConfig)
return config_1.path2langConfig[path];
console.error(`${path} isn’t assign with a lang, and will return 'en-US' instead.`);
return "en-US";
};
exports.path2Lang = path2Lang;
/** Get path from language */
const lang2Path = (lang = "") => {
if (lang in config_1.lang2PathConfig)
return config_1.lang2PathConfig[lang];
console.error(`${lang} has no path config, and will return '/' instead.`);
return "/";
};
exports.lang2Path = lang2Path;
/**
* Get language of root directory
*
* @param context VuePress Node Context
* @returns root language
*/
const getRootLang = (context) => {
var _a, _b;
// infer from siteLocale
const siteLocales = context.siteConfig.locales;
if ((siteLocales === null || siteLocales === void 0 ? void 0 : siteLocales["/"]) && (0, exports.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["/"]) && (0, exports.checkLang)((_b = themeLocales["/"]) === null || _b === void 0 ? void 0 : _b.lang))
return themeLocales["/"].lang;
(0, exports.showLangError)("root");
return "en-US";
};
exports.getRootLang = getRootLang;
/**
* Get the infer language path from root directory language
*
* @param context VuePress Node Context
* @returns infer language
*/
const getRootLangPath = (context) => (0, exports.lang2Path)((0, exports.getRootLang)(context));
exports.getRootLangPath = getRootLangPath;
const getLocalePaths = (context) => Array.from(new Set([
...Object.keys(context.siteConfig.locales || {}),
...Object.keys(context.themeConfig.locales || {}),
]));
exports.getLocalePaths = getLocalePaths;
/**
* 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
*/
const getLocales = (context, defaultLocalesConfig, userLocalesConfig = {}) => {
const rootPath = (0, exports.getRootLangPath)(context);
return Object.fromEntries([
...(0, exports.getLocalePaths)(context).map((localePath) => [
localePath,
(0, assign_1.deepAssign)({}, userLocalesConfig[localePath] || {}, defaultLocalesConfig[localePath] || {}),
]),
[
"/",
(0, assign_1.deepAssign)({}, userLocalesConfig["/"] || userLocalesConfig[rootPath] || {}, defaultLocalesConfig[rootPath] || {}),
],
]);
};
exports.getLocales = getLocales;
//# sourceMappingURL=helpers.js.map