imba-localization 0.4.10 → 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/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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imba-localization",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/HeapVoid/imba-localization.git"