UNPKG

wuchale

Version:

Protobuf-like i18n from plain code

59 lines (58 loc) 2.04 kB
export { MixedVisitor } from './mixed-visitor.js'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; export const varNames = { rt: '_w_runtime_', hmrUpdate: '_w_hmrUpdate_', }; export function runtimeVars(wrapFunc, base = varNames.rt) { return { rtTrans: `${wrapFunc(base)}.t`, rtTPlural: `${wrapFunc(base)}.tp`, rtPlural: `${wrapFunc(base)}._.p`, rtLocale: `${wrapFunc(base)}.l`, rtCtx: `${wrapFunc(base)}.cx`, rtTransCtx: `${wrapFunc(base)}.tx`, rtTransTag: `${wrapFunc(base)}.tt`, /** for when nesting, used in adapters with elements */ nestCtx: '_w_ctx_', }; } export function nonWhitespaceText(msgStr) { let trimmedS = msgStr.trimStart(); const startWh = msgStr.length - trimmedS.length; let trimmed = trimmedS.trimEnd(); const endWh = trimmedS.length - trimmed.length; return [startWh, trimmed, endWh]; } export function loaderPathResolver(importMetaUrl, baseDir, ext) { const dir = dirname(fileURLToPath(importMetaUrl)); return (name) => resolve(dir, `${baseDir}/${name}.${ext}`); } export const commentPrefix = '@wc-'; const commentDirectives = { ignore: `${commentPrefix}ignore`, ignoreFile: `${commentPrefix}ignore-file`, include: `${commentPrefix}include`, url: `${commentPrefix}url`, context: `${commentPrefix}context:`, }; export function processCommentDirectives(data, current) { const directives = { ...current }; if (data === commentDirectives.ignore) { directives.forceType = false; } if (data === commentDirectives.include) { directives.forceType = 'message'; } if (data === commentDirectives.url) { directives.forceType = 'url'; } if (data === commentDirectives.ignoreFile) { directives.ignoreFile = true; } if (data.startsWith(commentDirectives.context)) { directives.context = data.slice(commentDirectives.context.length).trim(); } return directives; }