wuchale
Version:
Protobuf-like i18n from plain code
141 lines (140 loc) • 4.5 kB
TypeScript
import type { CompiledElement } from "./compile.js";
import type { URLLocalizer } from "./url.js";
type TxtScope = "script" | "markup" | "attribute";
export type HeuristicDetailsBase = {
scope: TxtScope;
element?: string;
attribute?: string;
};
export type ScriptDeclType = "variable" | "function" | "class" | "expression";
export type HeuristicDetails = HeuristicDetailsBase & {
file: string;
declaring?: ScriptDeclType;
funcName?: string | null;
topLevelCall?: string;
call?: string;
};
export type MessageType = 'message' | 'url';
export declare class Message {
msgStr: string[];
plural: boolean;
context?: string;
comments: string[];
details: HeuristicDetails;
type: MessageType;
constructor(msgStr: string | (string | null | undefined)[], heuristicDetails?: HeuristicDetails, context?: string);
toKey: () => string;
}
export type HeuristicResultChecked = MessageType | false;
export type HeuristicResult = HeuristicResultChecked | null | undefined;
export type HeuristicFunc = (msg: Message) => HeuristicResult;
export declare const defaultHeuristicOpts: {
ignoreElements: string[];
ignoreAttribs: string[][];
ignoreCalls: string[];
urlAttribs: string[][];
urlCalls: string[];
};
export type CreateHeuristicOpts = typeof defaultHeuristicOpts;
export declare function createHeuristic(opts: CreateHeuristicOpts): HeuristicFunc;
/** Default heuristic */
export declare const defaultHeuristic: HeuristicFunc;
/** Default heuristic which ignores messages outside functions in the `script` scope */
export declare const defaultHeuristicFuncOnly: HeuristicFunc;
export declare const defaultGenerateLoadID: (filename: string) => string;
export declare class IndexTracker {
indices: Record<string, number>;
nextIndex: number;
get: (msgStr: string) => number;
}
export type GlobConf = string | string[] | {
include: string | string[];
ignore: string | string[];
};
export type CatalogExpr = {
plain: string;
reactive: string;
};
export type TransformHeader = {
head: string;
};
export type UrlMatcher = (url: string) => string | null | undefined;
type TransformCtx = {
content: string;
filename: string;
index: IndexTracker;
expr: CatalogExpr;
matchUrl: UrlMatcher;
};
export type HMRData = {
version: number;
data: Record<string, [number, CompiledElement][]>;
};
export type TransformOutputFunc = (header: string) => {
code?: string;
map?: any;
};
export type TransformOutput = {
output: TransformOutputFunc;
msgs: Message[];
};
export type TransformFunc = (expr: TransformCtx) => TransformOutput;
export type TransformFuncAsync = (expr: TransformCtx) => Promise<TransformOutput>;
export type WrapFunc = (expr: string) => string;
export type UseReactiveFunc = (details: {
funcName?: string;
nested: boolean;
file: string;
additional: object;
}) => {
/** null to disable initializing */
init: boolean | null;
use: boolean;
};
type RuntimeConfDetails = {
wrapInit: WrapFunc;
wrapUse: WrapFunc;
};
export type RuntimeConf = {
useReactive: UseReactiveFunc;
plain: RuntimeConfDetails;
reactive: RuntimeConfDetails;
};
export type LoaderPath = {
client: string;
server: string;
};
export type AdapterPassThruOpts = {
files: GlobConf;
localesDir: string;
/** if writing transformed code to a directory is desired, specify this */
outDir?: string;
granularLoad: boolean;
bundleLoad: boolean;
url?: {
patterns?: string[];
localize?: boolean | URLLocalizer;
};
generateLoadID: (filename: string) => string;
runtime: Partial<RuntimeConf>;
};
export type Adapter = AdapterPassThruOpts & {
transform: TransformFunc | TransformFuncAsync;
/** possible filename extensions for loader. E.g. `.js` */
loaderExts: string[];
/** default loaders to copy, `null` means custom */
defaultLoaderPath: LoaderPath | string | null;
/** names to import from loaders, should avoid collision with code variables */
getRuntimeVars?: Partial<CatalogExpr>;
};
export type CodePattern = {
name: string;
args: ('message' | 'pluralFunc' | 'locale' | 'other')[];
};
export type LoaderChoice<LoadersAvailable> = LoadersAvailable | string & {} | 'custom';
export type AdapterArgs<LoadersAvailable> = AdapterPassThruOpts & {
loader: LoaderChoice<LoadersAvailable>;
heuristic: HeuristicFunc;
patterns: CodePattern[];
};
export {};