imba-localization 0.4.9 โ 0.4.11
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.
- package/README.md +2 -1
- package/index.d.ts +38 -0
- package/localization.imba +26 -0
- package/package.json +17 -3
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ A lightweight Imba module for loading and handling JSON-based localization files
|
|
|
10
10
|
- ๐ **Smart fallback system** - Falls back to a default language when needed
|
|
11
11
|
- ๐ง **Intuitive access** - Proxy-based access to translation strings
|
|
12
12
|
- ๐ก **Event handling** - Support for `onready`, `onchange`, and `onerror` events
|
|
13
|
+
- ๐งพ **Type declarations** - Includes package-level TypeScript declarations for editor and language-server imports
|
|
13
14
|
- ๐ **Simple integration** - Easy to use in any Imba-based web application
|
|
14
15
|
- ๐งฉ **`<language-selector>`** - Plug and play tag component for switching languages
|
|
15
16
|
|
|
@@ -310,4 +311,4 @@ tag App
|
|
|
310
311
|
css w:20px h:20px stroke:red
|
|
311
312
|
<{svg-arrow-down}>
|
|
312
313
|
|
|
313
|
-
```
|
|
314
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type LocalizationLanguage = Record<string, any>;
|
|
2
|
+
export type LocalizationLanguages = Record<string, LocalizationLanguage>;
|
|
3
|
+
|
|
4
|
+
export interface LocalizationErrorState {
|
|
5
|
+
cache: Record<string, boolean>;
|
|
6
|
+
throw(code: string, details?: any): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class Localization {
|
|
10
|
+
constructor(url: string, fallback?: string);
|
|
11
|
+
|
|
12
|
+
onready?: () => void;
|
|
13
|
+
onerror?: (error: string, details?: any) => void;
|
|
14
|
+
onchange?: (language: string) => void;
|
|
15
|
+
|
|
16
|
+
languages: LocalizationLanguages;
|
|
17
|
+
preferred: string;
|
|
18
|
+
default: string;
|
|
19
|
+
ready: boolean;
|
|
20
|
+
err: LocalizationErrorState;
|
|
21
|
+
|
|
22
|
+
active: string;
|
|
23
|
+
|
|
24
|
+
render(value: unknown, data?: Record<string, unknown> | null): string;
|
|
25
|
+
lookup(path: string | string[], fallback?: unknown): unknown;
|
|
26
|
+
text(path: string | string[], fallback?: string, data?: Record<string, unknown> | null): string;
|
|
27
|
+
table(path: string | string[]): Record<string, any>;
|
|
28
|
+
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const flags: Record<string, string>;
|
|
33
|
+
|
|
34
|
+
declare global {
|
|
35
|
+
interface HTMLElementTagNameMap {
|
|
36
|
+
"language-selector": HTMLElement;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/localization.imba
CHANGED
|
@@ -101,6 +101,32 @@ export class Localization
|
|
|
101
101
|
target.err.throw('localization-no-key', p)
|
|
102
102
|
return ''
|
|
103
103
|
}
|
|
104
|
+
|
|
105
|
+
def render value, data = null
|
|
106
|
+
let text = String(value or '')
|
|
107
|
+
return text unless data and typeof data == 'object'
|
|
108
|
+
text.replace /\{([^}]+)\}/g, do(_match, key)
|
|
109
|
+
const value = data[key]
|
|
110
|
+
if value == undefined or value == null then '' else String(value)
|
|
111
|
+
|
|
112
|
+
def lookup path, fallback = ''
|
|
113
|
+
const keys = if Array.isArray(path) then path else String(path or '').split('.').filter(do(part) part)
|
|
114
|
+
let value = languages..[active] or languages..[default] or {}
|
|
115
|
+
for key in keys
|
|
116
|
+
if value and typeof value == 'object' and value[key] != undefined
|
|
117
|
+
value = value[key]
|
|
118
|
+
else
|
|
119
|
+
return fallback
|
|
120
|
+
value
|
|
121
|
+
|
|
122
|
+
def text path, fallback = '', data = null
|
|
123
|
+
const value = lookup(path, fallback)
|
|
124
|
+
return render(value, data) if typeof value == 'string' or typeof value == 'number'
|
|
125
|
+
render(fallback, data)
|
|
126
|
+
|
|
127
|
+
def table path
|
|
128
|
+
const value = lookup(path, {})
|
|
129
|
+
if value and typeof value == 'object' then value else {}
|
|
104
130
|
|
|
105
131
|
def _finalize data, error
|
|
106
132
|
if error or !data
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "imba-localization",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.11",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/HeapVoid/imba-localization.git"
|
|
@@ -8,11 +8,25 @@
|
|
|
8
8
|
"main": "localization.imba",
|
|
9
9
|
"module": "localization.imba",
|
|
10
10
|
"browser": "localization.imba",
|
|
11
|
+
"types": "index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"import": "./localization.imba",
|
|
16
|
+
"default": "./localization.imba"
|
|
17
|
+
},
|
|
18
|
+
"./localization.imba": {
|
|
19
|
+
"types": "./index.d.ts",
|
|
20
|
+
"import": "./localization.imba",
|
|
21
|
+
"default": "./localization.imba"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
11
24
|
"description": "A class to make using language localizations in Imba easier.",
|
|
12
25
|
"keywords": ["imba", "theme", "localization"],
|
|
13
26
|
"license": "MIT",
|
|
14
27
|
"type": "module",
|
|
15
28
|
"files": [
|
|
16
|
-
"localization.imba"
|
|
29
|
+
"localization.imba",
|
|
30
|
+
"index.d.ts"
|
|
17
31
|
]
|
|
18
|
-
}
|
|
32
|
+
}
|