@lokascript/i18n 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +286 -0
  2. package/dist/browser.cjs +7669 -0
  3. package/dist/browser.cjs.map +1 -0
  4. package/dist/browser.d.cts +50 -0
  5. package/dist/browser.d.ts +50 -0
  6. package/dist/browser.js +7592 -0
  7. package/dist/browser.js.map +1 -0
  8. package/dist/hyperfixi-i18n.min.js +2 -0
  9. package/dist/hyperfixi-i18n.min.js.map +1 -0
  10. package/dist/hyperfixi-i18n.mjs +8558 -0
  11. package/dist/hyperfixi-i18n.mjs.map +1 -0
  12. package/dist/index.cjs +14205 -0
  13. package/dist/index.cjs.map +1 -0
  14. package/dist/index.d.cts +947 -0
  15. package/dist/index.d.ts +947 -0
  16. package/dist/index.js +14095 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/transformer-Ckask-yw.d.cts +1041 -0
  19. package/dist/transformer-Ckask-yw.d.ts +1041 -0
  20. package/package.json +84 -0
  21. package/src/browser.ts +122 -0
  22. package/src/compatibility/browser-tests/grammar-demo.spec.ts +169 -0
  23. package/src/constants.ts +366 -0
  24. package/src/dictionaries/ar.ts +233 -0
  25. package/src/dictionaries/bn.ts +156 -0
  26. package/src/dictionaries/de.ts +233 -0
  27. package/src/dictionaries/derive.ts +515 -0
  28. package/src/dictionaries/en.ts +237 -0
  29. package/src/dictionaries/es.ts +233 -0
  30. package/src/dictionaries/fr.ts +233 -0
  31. package/src/dictionaries/hi.ts +270 -0
  32. package/src/dictionaries/id.ts +233 -0
  33. package/src/dictionaries/index.ts +238 -0
  34. package/src/dictionaries/it.ts +233 -0
  35. package/src/dictionaries/ja.ts +233 -0
  36. package/src/dictionaries/ko.ts +233 -0
  37. package/src/dictionaries/ms.ts +276 -0
  38. package/src/dictionaries/pl.ts +239 -0
  39. package/src/dictionaries/pt.ts +237 -0
  40. package/src/dictionaries/qu.ts +233 -0
  41. package/src/dictionaries/ru.ts +270 -0
  42. package/src/dictionaries/sw.ts +233 -0
  43. package/src/dictionaries/th.ts +156 -0
  44. package/src/dictionaries/tl.ts +276 -0
  45. package/src/dictionaries/tr.ts +233 -0
  46. package/src/dictionaries/uk.ts +270 -0
  47. package/src/dictionaries/vi.ts +210 -0
  48. package/src/dictionaries/zh.ts +233 -0
  49. package/src/enhanced-i18n.test.ts +454 -0
  50. package/src/enhanced-i18n.ts +713 -0
  51. package/src/examples/new-languages.ts +326 -0
  52. package/src/formatting.test.ts +213 -0
  53. package/src/formatting.ts +416 -0
  54. package/src/grammar/direct-mappings.ts +353 -0
  55. package/src/grammar/grammar.test.ts +1053 -0
  56. package/src/grammar/index.ts +59 -0
  57. package/src/grammar/profiles/index.ts +860 -0
  58. package/src/grammar/transformer.ts +1318 -0
  59. package/src/grammar/types.ts +630 -0
  60. package/src/index.ts +202 -0
  61. package/src/new-languages.test.ts +389 -0
  62. package/src/parser/analyze-conflicts.test.ts +229 -0
  63. package/src/parser/ar.ts +40 -0
  64. package/src/parser/create-provider.ts +309 -0
  65. package/src/parser/de.ts +36 -0
  66. package/src/parser/es.ts +31 -0
  67. package/src/parser/fr.ts +31 -0
  68. package/src/parser/id.ts +34 -0
  69. package/src/parser/index.ts +50 -0
  70. package/src/parser/ja.ts +36 -0
  71. package/src/parser/ko.ts +37 -0
  72. package/src/parser/locale-manager.test.ts +198 -0
  73. package/src/parser/locale-manager.ts +197 -0
  74. package/src/parser/parser-integration.test.ts +439 -0
  75. package/src/parser/pt.ts +37 -0
  76. package/src/parser/qu.ts +37 -0
  77. package/src/parser/sw.ts +37 -0
  78. package/src/parser/tr.ts +38 -0
  79. package/src/parser/types.ts +113 -0
  80. package/src/parser/zh.ts +38 -0
  81. package/src/plugins/vite.ts +224 -0
  82. package/src/plugins/webpack.ts +124 -0
  83. package/src/pluralization.test.ts +197 -0
  84. package/src/pluralization.ts +393 -0
  85. package/src/runtime.ts +441 -0
  86. package/src/ssr-integration.ts +225 -0
  87. package/src/test-setup.ts +195 -0
  88. package/src/translation-validation.test.ts +171 -0
  89. package/src/translator.test.ts +252 -0
  90. package/src/translator.ts +297 -0
  91. package/src/types.ts +209 -0
  92. package/src/utils/locale.ts +190 -0
  93. package/src/utils/tokenizer-adapter.ts +469 -0
  94. package/src/utils/tokenizer.ts +19 -0
  95. package/src/validators/index.ts +174 -0
  96. package/src/validators/schema.ts +129 -0
package/README.md ADDED
@@ -0,0 +1,286 @@
1
+ # @lokascript/i18n
2
+
3
+ Comprehensive internationalization (i18n) support for LokaScript and \_hyperscript applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @lokascript/i18n
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **🌍 Multi-language Support**: Built-in dictionaries for 13 languages (English, Spanish, French, German, Japanese, Korean, Chinese, Arabic, Turkish, Portuguese, Indonesian, Quechua, Swahili)
14
+ - **🔄 Runtime Locale Switching**: Dynamic language switching in browser environments with automatic detection
15
+ - **⚡ SSR Integration**: Server-side rendering with locale detection, SEO optimization, and hydration support
16
+ - **📊 Pluralization**: CLDR-compliant pluralization rules for complex languages (Russian, Arabic, etc.)
17
+ - **💰 Formatting**: Locale-aware number, date, currency, and unit formatting with fallbacks
18
+ - **🛠️ Build Tool Integration**: Vite and Webpack plugins for build-time translation
19
+ - **🔍 Language Detection**: Automatically detect the language of hyperscript code from content
20
+ - **✅ Validation**: Comprehensive dictionary validation with coverage reports and warnings
21
+ - **🎯 Type Safety**: Full TypeScript support with comprehensive type definitions
22
+ - **⚡ Performance**: Caching, lazy loading, and optimized translation algorithms
23
+ - **📱 Browser Support**: Modern APIs with graceful fallbacks for legacy environments
24
+
25
+ ## When to Use i18n vs Semantic
26
+
27
+ LokaScript has two packages for multilingual support with different purposes:
28
+
29
+ | Package | Purpose | Use Case |
30
+ | ------------------------ | -------------------------------------------- | ---------------------------------------------------- |
31
+ | **@lokascript/semantic** | Parse code written in any language → execute | Users **write** hyperscript in their native language |
32
+ | **@lokascript/i18n** | Transform code between languages | **Translate** code examples for docs/teaching |
33
+
34
+ **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 `クリックしたら`).
35
+
36
+ **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.
37
+
38
+ Example workflow for documentation:
39
+
40
+ ```typescript
41
+ // You have English examples in your docs
42
+ const english = 'on click toggle .active';
43
+
44
+ // Translate to show Japanese readers the equivalent
45
+ const japanese = translator.translate(english, { from: 'en', to: 'ja' });
46
+ // → "クリック で .active を 切り替え"
47
+ ```
48
+
49
+ ## Quick Start
50
+
51
+ ### Basic Translation
52
+
53
+ ```typescript
54
+ import { HyperscriptTranslator } from '@lokascript/i18n';
55
+
56
+ const translator = new HyperscriptTranslator({ locale: 'es' });
57
+
58
+ // Translate from Spanish to English
59
+ const english = translator.translate('en clic alternar .activo', { to: 'en' });
60
+ // Result: "on click toggle .activo"
61
+
62
+ // Translate from English to Korean
63
+ const korean = translator.translate('on click toggle .active', { from: 'en', to: 'ko' });
64
+ // Result: "클릭 토글 .active"
65
+ ```
66
+
67
+ ### Language Detection
68
+
69
+ ```typescript
70
+ const detectedLocale = translator.detectLanguage('si verdadero entonces registrar "hola"');
71
+ // Result: "es"
72
+ ```
73
+
74
+ ### Build Tool Integration
75
+
76
+ #### Vite
77
+
78
+ ```typescript
79
+ // vite.config.ts
80
+ import { hyperscriptI18nVitePlugin } from '@lokascript/i18n/plugins/vite';
81
+
82
+ export default {
83
+ plugins: [
84
+ hyperscriptI18nVitePlugin({
85
+ sourceLocale: 'es',
86
+ targetLocale: 'en',
87
+ preserveOriginal: true,
88
+ }),
89
+ ],
90
+ };
91
+ ```
92
+
93
+ #### Webpack
94
+
95
+ ```javascript
96
+ // webpack.config.js
97
+ const { HyperscriptI18nWebpackPlugin } = require('@lokascript/i18n/plugins/webpack');
98
+
99
+ module.exports = {
100
+ plugins: [
101
+ new HyperscriptI18nWebpackPlugin({
102
+ sourceLocale: 'es',
103
+ targetLocale: 'en',
104
+ }),
105
+ ],
106
+ };
107
+ ```
108
+
109
+ ## Supported Languages
110
+
111
+ | Language | Code | Status | Word Order | Features |
112
+ | ---------- | ---- | ----------- | ---------- | ------------------------------------------ |
113
+ | English | `en` | ✅ Complete | SVO | Base language |
114
+ | Spanish | `es` | ✅ Complete | SVO | Pluralization, morphological normalization |
115
+ | French | `fr` | ✅ Complete | SVO | Pluralization |
116
+ | German | `de` | ✅ Complete | V2 | Pluralization |
117
+ | Japanese | `ja` | ✅ Complete | SOV | Native idioms, morphological normalization |
118
+ | Korean | `ko` | ✅ Complete | SOV | Native idioms, morphological normalization |
119
+ | Chinese | `zh` | ✅ Complete | SVO | Particle handling |
120
+ | Arabic | `ar` | ✅ Complete | VSO | RTL support, morphological normalization |
121
+ | Turkish | `tr` | ✅ Complete | SOV | Agglutinative morphology, vowel harmony |
122
+ | Portuguese | `pt` | ✅ Complete | SVO | Full dictionary |
123
+ | Indonesian | `id` | ✅ Complete | SVO | Agglutinative support |
124
+ | Quechua | `qu` | ✅ Complete | SOV | Agglutinative support |
125
+ | Swahili | `sw` | ✅ Complete | SVO | Noun class system |
126
+
127
+ ## API Reference
128
+
129
+ ### HyperscriptTranslator
130
+
131
+ ```typescript
132
+ class HyperscriptTranslator {
133
+ constructor(config: I18nConfig);
134
+
135
+ // Translate hyperscript text
136
+ translate(text: string, options: TranslationOptions): string;
137
+
138
+ // Get detailed translation with token information
139
+ translateWithDetails(text: string, options: TranslationOptions): TranslationResult;
140
+
141
+ // Detect language of hyperscript text
142
+ detectLanguage(text: string): string;
143
+
144
+ // Add custom dictionary
145
+ addDictionary(locale: string, dictionary: Dictionary): void;
146
+
147
+ // Get supported locales
148
+ getSupportedLocales(): string[];
149
+
150
+ // Validate a dictionary
151
+ validateDictionary(locale: string): ValidationResult;
152
+
153
+ // Check if locale uses RTL
154
+ isRTL(locale: string): boolean;
155
+
156
+ // Get completions for IDE support
157
+ getCompletions(context: CompletionContext): string[];
158
+ }
159
+ ```
160
+
161
+ ### Types
162
+
163
+ ```typescript
164
+ interface I18nConfig {
165
+ locale: string;
166
+ fallbackLocale?: string;
167
+ dictionaries?: Record<string, Dictionary>;
168
+ detectLocale?: boolean;
169
+ rtlLocales?: string[];
170
+ }
171
+
172
+ interface TranslationOptions {
173
+ from?: string;
174
+ to: string;
175
+ preserveOriginal?: boolean;
176
+ validate?: boolean;
177
+ }
178
+
179
+ interface Dictionary {
180
+ commands: Record<string, string>;
181
+ modifiers: Record<string, string>;
182
+ events: Record<string, string>;
183
+ logical: Record<string, string>;
184
+ temporal: Record<string, string>;
185
+ values: Record<string, string>;
186
+ attributes: Record<string, string>;
187
+ }
188
+ ```
189
+
190
+ ## Advanced Usage
191
+
192
+ ### Custom Dictionaries
193
+
194
+ ```typescript
195
+ import { HyperscriptTranslator, Dictionary } from '@lokascript/i18n';
196
+
197
+ const customDictionary: Dictionary = {
198
+ commands: {
199
+ on: 'sur',
200
+ click: 'cliquer',
201
+ toggle: 'basculer',
202
+ },
203
+ // ... other categories
204
+ };
205
+
206
+ const translator = new HyperscriptTranslator({
207
+ locale: 'fr',
208
+ dictionaries: {
209
+ fr: customDictionary,
210
+ },
211
+ });
212
+ ```
213
+
214
+ ### LSP Integration
215
+
216
+ ```typescript
217
+ import { I18nLanguageProvider } from '@lokascript/i18n/lsp';
218
+
219
+ const provider = new I18nLanguageProvider('es');
220
+
221
+ // Get completions
222
+ const completions = await provider.provideCompletions(document, position);
223
+
224
+ // Get hover information
225
+ const hover = await provider.provideHover(document, position);
226
+ ```
227
+
228
+ ### CLI Usage
229
+
230
+ ```bash
231
+ # Install globally
232
+ npm install -g @lokascript/i18n
233
+
234
+ # Translate a file
235
+ lokascript-translate input.html output.html --from es --to en
236
+
237
+ # Translate a directory
238
+ lokascript-translate src/ dist/ --from es --to en
239
+ ```
240
+
241
+ ## Contributing
242
+
243
+ We welcome contributions, especially new language dictionaries!
244
+
245
+ ### Adding a New Language
246
+
247
+ 1. Create a new dictionary file in `src/dictionaries/[locale].ts`
248
+ 2. Follow the existing dictionary structure
249
+ 3. Add comprehensive tests
250
+ 4. Submit a pull request
251
+
252
+ Example dictionary structure:
253
+
254
+ ```typescript
255
+ export const fr: Dictionary = {
256
+ commands: {
257
+ on: 'sur',
258
+ tell: 'dire',
259
+ trigger: 'déclencher',
260
+ // ... all commands
261
+ },
262
+ modifiers: {
263
+ to: 'à',
264
+ from: 'de',
265
+ // ... all modifiers
266
+ },
267
+ // ... other categories
268
+ };
269
+ ```
270
+
271
+ ## Testing
272
+
273
+ ```bash
274
+ # Run tests
275
+ npm test
276
+
277
+ # Run tests with coverage
278
+ npm run test:coverage
279
+
280
+ # Validate dictionaries
281
+ npm run validate-dictionaries
282
+ ```
283
+
284
+ ## License
285
+
286
+ MIT