Grammar transformation and keyword translation for hyperscript
npm install @lokascript/i18nComprehensive internationalization (i18n) support for LokaScript and \_hyperscript applications.
``bash`
npm install @lokascript/i18n
- π Multi-language Support: Built-in dictionaries for 13 languages (English, Spanish, French, German, Japanese, Korean, Chinese, Arabic, Turkish, Portuguese, Indonesian, Quechua, Swahili)
- π Runtime Locale Switching: Dynamic language switching in browser environments with automatic detection
- β‘ SSR Integration: Server-side rendering with locale detection, SEO optimization, and hydration support
- π Pluralization: CLDR-compliant pluralization rules for complex languages (Russian, Arabic, etc.)
- π° Formatting: Locale-aware number, date, currency, and unit formatting with fallbacks
- π οΈ Build Tool Integration: Vite and Webpack plugins for build-time translation
- π Language Detection: Automatically detect the language of hyperscript code from content
- β
Validation: Comprehensive dictionary validation with coverage reports and warnings
- π― Type Safety: Full TypeScript support with comprehensive type definitions
- β‘ Performance: Caching, lazy loading, and optimized translation algorithms
- π± Browser Support: Modern APIs with graceful fallbacks for legacy environments
LokaScript has two packages for multilingual support with different purposes:
| Package | Purpose | Use Case |
| ------------------------ | -------------------------------------------- | ---------------------------------------------------- |
| @lokascript/semantic | Parse code written in any language β execute | Users write hyperscript in their native language |
| @lokascript/i18n | Transform code between languages | Translate code examples for docs/teaching |
Use @lokascript/semantic when your users will write hyperscript in their native language. It parses multilingual input directly into executable AST nodes with native idiom support (e.g., Japanese conditionals like γ―γͺγγ―γγγ).
Use @lokascript/i18n (this package) when you need to translate code examples between languages for documentation, tutorials, or teaching materials. It transforms existing code for display purposesβshowing learners how the same logic looks in different languages.
Example workflow for documentation:
`typescript
// You have English examples in your docs
const english = 'on click toggle .active';
// Translate to show Japanese readers the equivalent
const japanese = translator.translate(english, { from: 'en', to: 'ja' });
// β "γ―γͺγγ― γ§ .active γ εγζΏγ"
`
`typescript
import { HyperscriptTranslator } from '@lokascript/i18n';
const translator = new HyperscriptTranslator({ locale: 'es' });
// Translate from Spanish to English
const english = translator.translate('en clic alternar .activo', { to: 'en' });
// Result: "on click toggle .activo"
// Translate from English to Korean
const korean = translator.translate('on click toggle .active', { from: 'en', to: 'ko' });
// Result: "ν΄λ¦ ν κΈ .active"
`
`typescript`
const detectedLocale = translator.detectLanguage('si verdadero entonces registrar "hola"');
// Result: "es"
#### Vite
`typescript
// vite.config.ts
import { hyperscriptI18nVitePlugin } from '@lokascript/i18n/plugins/vite';
export default {
plugins: [
hyperscriptI18nVitePlugin({
sourceLocale: 'es',
targetLocale: 'en',
preserveOriginal: true,
}),
],
};
`
#### Webpack
`javascript
// webpack.config.js
const { HyperscriptI18nWebpackPlugin } = require('@lokascript/i18n/plugins/webpack');
module.exports = {
plugins: [
new HyperscriptI18nWebpackPlugin({
sourceLocale: 'es',
targetLocale: 'en',
}),
],
};
`
| Language | Code | Status | Word Order | Features |
| ---------- | ---- | ----------- | ---------- | ------------------------------------------ |
| English | en | β
Complete | SVO | Base language |es
| Spanish | | β
Complete | SVO | Pluralization, morphological normalization |fr
| French | | β
Complete | SVO | Pluralization |de
| German | | β
Complete | V2 | Pluralization |ja
| Japanese | | β
Complete | SOV | Native idioms, morphological normalization |ko
| Korean | | β
Complete | SOV | Native idioms, morphological normalization |zh
| Chinese | | β
Complete | SVO | Particle handling |ar
| Arabic | | β
Complete | VSO | RTL support, morphological normalization |tr
| Turkish | | β
Complete | SOV | Agglutinative morphology, vowel harmony |pt
| Portuguese | | β
Complete | SVO | Full dictionary |id
| Indonesian | | β
Complete | SVO | Agglutinative support |qu
| Quechua | | β
Complete | SOV | Agglutinative support |sw
| Swahili | | β
Complete | SVO | Noun class system |
`typescript
class HyperscriptTranslator {
constructor(config: I18nConfig);
// Translate hyperscript text
translate(text: string, options: TranslationOptions): string;
// Get detailed translation with token information
translateWithDetails(text: string, options: TranslationOptions): TranslationResult;
// Detect language of hyperscript text
detectLanguage(text: string): string;
// Add custom dictionary
addDictionary(locale: string, dictionary: Dictionary): void;
// Get supported locales
getSupportedLocales(): string[];
// Validate a dictionary
validateDictionary(locale: string): ValidationResult;
// Check if locale uses RTL
isRTL(locale: string): boolean;
// Get completions for IDE support
getCompletions(context: CompletionContext): string[];
}
`
`typescript
interface I18nConfig {
locale: string;
fallbackLocale?: string;
dictionaries?: Record
detectLocale?: boolean;
rtlLocales?: string[];
}
interface TranslationOptions {
from?: string;
to: string;
preserveOriginal?: boolean;
validate?: boolean;
}
interface Dictionary {
commands: Record
modifiers: Record
events: Record
logical: Record
temporal: Record
values: Record
attributes: Record
}
`
`typescript
import { HyperscriptTranslator, Dictionary } from '@lokascript/i18n';
const customDictionary: Dictionary = {
commands: {
on: 'sur',
click: 'cliquer',
toggle: 'basculer',
},
// ... other categories
};
const translator = new HyperscriptTranslator({
locale: 'fr',
dictionaries: {
fr: customDictionary,
},
});
`
`typescript
import { I18nLanguageProvider } from '@lokascript/i18n/lsp';
const provider = new I18nLanguageProvider('es');
// Get completions
const completions = await provider.provideCompletions(document, position);
// Get hover information
const hover = await provider.provideHover(document, position);
`
`bashInstall globally
npm install -g @lokascript/i18n
Contributing
We welcome contributions, especially new language dictionaries!
$3
1. Create a new dictionary file in
src/dictionaries/[locale].ts
2. Follow the existing dictionary structure
3. Add comprehensive tests
4. Submit a pull requestExample dictionary structure:
`typescript
export const fr: Dictionary = {
commands: {
on: 'sur',
tell: 'dire',
trigger: 'dΓ©clencher',
// ... all commands
},
modifiers: {
to: 'Γ ',
from: 'de',
// ... all modifiers
},
// ... other categories
};
`Testing
`bash
Run tests
npm testRun tests with coverage
npm run test:coverageValidate dictionaries
npm run validate-dictionaries
``MIT