UNPKG

wuchale

Version:

Protobuf-like i18n from normal code

56 lines 1.66 kB
import { resolve } from "node:path"; import {} from "./adapters.js"; export const defaultConfig = { sourceLocale: 'en', otherLocales: [], adapters: {}, hmr: true, geminiAPIKey: 'env', messages: true, }; function deepAssign(fromObj, toObj) { for (const [key, value] of Object.entries(fromObj)) { if (value === undefined) { delete toObj[key]; } if (!value || Array.isArray(value) || typeof value !== 'object') { toObj[key] = value; continue; } // objects if (!toObj[key]) { toObj[key] = {}; } deepAssign(fromObj[key], toObj[key]); } } export function defineConfig(config) { return config; } export function deepMergeObjects(source, target) { const full = { ...target }; deepAssign(source, full); return full; } export const configName = 'wuchale.config.js'; const displayName = new Intl.DisplayNames(['en'], { type: 'language' }); export const getLanguageName = (code) => displayName.of(code); function checkValidLocale(locale) { try { getLanguageName(locale); } catch { throw new Error(`Invalid locale identifier: ${locale}`); } } export async function getConfig(configPath) { const importPath = (configPath && resolve(configPath)) ?? `${process.cwd()}/${configName}`; const module = await import(importPath); const config = deepMergeObjects(module.default, defaultConfig); checkValidLocale(config.sourceLocale); for (const loc of config.otherLocales) { checkValidLocale(loc); } return config; } //# sourceMappingURL=config.js.map