API Reference

For the complete hyperscript command reference (43 commands,
expressions, compilation API), see the
hyperfixi API docs.

This page documents the LokaScript multilingual-specific APIs.

@lokascript/i18n

Grammar transformation and keyword translation for hyperscript.

translate(code, fromLang, toLang)

Translate hyperscript code between languages.

import { translate } from '@lokascript/i18n';

// English to Japanese
const ja = translate('on click toggle .active on me', 'en', 'ja');
// → 'クリック で 私 の .active を 切り替え'

// Japanese back to English
const en = translate(ja, 'ja', 'en');
// → 'on click toggle .active on me'

getSupportedLanguages()

Returns the list of supported language codes.

import { getSupportedLanguages } from '@lokascript/i18n';

const langs = getSupportedLanguages();
// ['en', 'es', 'pt', 'fr', 'de', 'ja', 'zh', 'ko', 'ar', ...]

getWordOrder(lang)

Returns the grammar word order for a language.

import { getWordOrder } from '@lokascript/i18n';

getWordOrder('en'); // 'SVO'
getWordOrder('ja'); // 'SOV'
getWordOrder('ar'); // 'VSO'

@lokascript/semantic

Semantic multilingual parser for hyperscript.

parse(code, options?)

Parse hyperscript code with language-aware semantic analysis.

import { parse } from '@lokascript/semantic';

const result = parse('on click toggle .active on me', {
  language: 'en',
  confidenceThreshold: 0.7
});

console.log(result.confidence); // 0.98
console.log(result.language);   // 'en'
console.log(result.ast);        // Parsed AST

detect(code)

Detect the language of hyperscript code.

import { detect } from '@lokascript/semantic';

const lang = detect('クリック で 私 の .active を 切り替え');
// { language: 'ja', confidence: 0.95 }

@lokascript/hyperscript-adapter

Drop-in multilingual plugin for original _hyperscript.
See the adapter plugin page for
quick start, live demos, and bundle options.

hyperscriptI18n(options?)

Create a plugin for _hyperscript.use(). Returns a function that patches runtime.getScript() to translate non-English _="..." attributes before parsing.

import { hyperscriptI18n } from '@lokascript/hyperscript-adapter';

// Basic — uses defaults
_hyperscript.use(hyperscriptI18n());

// With options
_hyperscript.use(
  hyperscriptI18n({
    defaultLanguage: 'es',
    confidenceThreshold: { ja: 0.1, '*': 0.5 },
    debug: true,
  })
);

PluginOptions

Option Type Default Description
defaultLanguage string Fallback language when no data-lang or data-hyperscript-lang is found
languageAttribute string "data-lang" Custom attribute name for per-element language
confidenceThreshold number | Record<string, number> 0.5 Min confidence (0–1). Per-language map supported — use '*' key for default.
strategy 'semantic' | 'i18n' | 'auto' 'semantic' Translation strategy. 'auto' tries semantic first, then i18n fallback.
debug boolean false Log translations to console as [hyperscript-i18n] lang: "input" → "output"
i18nToEnglish function Optional @lokascript/i18n toEnglish function for fallback (used with 'auto' or 'i18n' strategy)

preprocess(src, lang, config?)

Standalone preprocessing for programmatic _hyperscript("code") or _hyperscript.evaluate() calls. The plugin only intercepts DOM attributes — use this for code strings.

import { preprocess } from '@lokascript/hyperscript-adapter';

const english = preprocess('alternar .active', 'es');
// → 'toggle .active'

_hyperscript(english);

Parameters:

  • src (string) — hyperscript code in the source language
  • lang (string) — ISO 639-1 language code (e.g. 'es', 'ja')
  • config (Partial<PreprocessorConfig>) — optional overrides

Returns string — English hyperscript. Returns original text if lang is 'en' or translation fails.

preprocessToEnglish(src, lang, config?)

Lower-level preprocessing function. Unlike preprocess(), does not short-circuit for English input. Most users should use preprocess() instead.

PreprocessorConfig

Option Type Default Description
confidenceThreshold number | Record<string, number> 0.5 Min confidence for semantic parsing
strategy 'semantic' | 'i18n' | 'auto' 'semantic' Translation strategy
fallbackToOriginal boolean true Return original text when translation fails
i18nToEnglish function Optional i18n fallback function

resolveLanguage(elt)

Resolve the language for an element using the cascading strategy:

  1. data-lang attribute on the element
  2. data-hyperscript-lang on the element or closest ancestor
  3. <html lang="..."> on the document
  4. Returns null (English assumed)
import { resolveLanguage } from '@lokascript/hyperscript-adapter';

// Useful for debugging language detection
const lang = resolveLanguage(document.querySelector('#myButton'));
console.log(lang); // 'es', 'ja', or null

BCP-47 tags are normalized to ISO 639-1 codes (e.g. "ja-JP""ja").

Core API

For the core hyperscript runtime API (compileSync,
compileAsync, eval, validate, etc.), see the
hyperfixi API reference.