import { deepAssign, fromEntries } from '../../shared/index.js'; import { Logger } from '../utils/index.js'; /** * Get locale info * * 获取本地化信息 * * @param app - VuePress App / VuePress 应用 * @param logger - Logger / 日志记录器 * * @returns Array of locale path and language pairs / 本地化路径和语言对的数组 */ export const getLocaleInfo = (app, logger) => { const localeEntries = Object.entries(app.options.locales); if (!localeEntries.length) return [['/', app.options.lang]]; return localeEntries.map(([path, { lang }]) => { if (lang) return [path, lang]; if (app.options.debug) { ; (logger ?? console).warn(`Locale ${path} is missing lang config, fallback to root lang ${app.options.lang}`); } return [path, app.options.lang]; }); }; export const getLocaleData = (info, lang, logger) => { const isShortLang = !lang.includes('-'); // full search const localeData = info.find(([langs]) => langs.includes(lang)); // find exact match if (localeData) return localeData[1]; if (!isShortLang) { // find short lang match const shortLang = lang.split('-')[0]; const shortLocaleData = info.find(([langs]) => langs.includes(shortLang)); if (shortLocaleData) return shortLocaleData[1]; } ; (logger ?? console).warn(`${lang} is missing it's i18n config`); return ( // en-US (info.find(([langs]) => langs.includes('en-US')) ?? // en info.find(([langs]) => langs.some((item) => item.startsWith('en'))) ?? // first one info[0])[1]); }; /** * Get final locale config for client * * 获取客户端的最终本地化配置 * * @param options - Configuration options / 配置选项 * * @returns Final locale config / 最终本地化配置 */ export const getFullLocaleConfig = ({ app, name, default: defaultLocaleData, config: userLocalesConfig = {}, }) => { const logger = new Logger(name); const localeInfo = getLocaleInfo(app, logger); return fromEntries(localeInfo.map(([localePath, lang]) => [ localePath, deepAssign({}, getLocaleData(defaultLocaleData, lang, logger), (userLocalesConfig[localePath] ?? {})), ])); };