imba-localization 0.4.10 → 0.4.12
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 +7 -0
- package/index.d.ts +5 -0
- package/localization.imba +26 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -136,6 +136,13 @@ new Localization(url, default = 'en')
|
|
|
136
136
|
- `languages`: Object containing all loaded language data
|
|
137
137
|
- `preferred`: Detected browser language (first 2 characters of `navigator.language`)
|
|
138
138
|
|
|
139
|
+
### Methods
|
|
140
|
+
|
|
141
|
+
- `lookup(path, fallback = '')`: Reads a nested value by dot path or path array
|
|
142
|
+
- `text(path, fallback = '', data = null)`: Reads a localized string and replaces `{key}` placeholders from `data`
|
|
143
|
+
- `table(path)`: Reads a nested object table, returning `{}` when missing
|
|
144
|
+
- `render(value, data = null)`: Replaces `{key}` placeholders in any string-like value
|
|
145
|
+
|
|
139
146
|
### Events
|
|
140
147
|
|
|
141
148
|
- `onready`: Called when localization data is successfully loaded
|
package/index.d.ts
CHANGED
|
@@ -21,6 +21,11 @@ export class Localization {
|
|
|
21
21
|
|
|
22
22
|
active: string;
|
|
23
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
|
+
|
|
24
29
|
[key: string]: any;
|
|
25
30
|
}
|
|
26
31
|
|
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
|