smartloc 2.0.1 → 2.0.2
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/package.json +2 -2
- package/src/core/locale-list.ts +2 -0
- package/src/core/smartloc.tsx +12 -1
- package/src/core/tag.ts +9 -1
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smartloc",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "A i18n toolset for nodejs apis leveraging tagged strings",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc && cp package.json bin/package.json && cp README.md build",
|
|
8
|
-
"release": "npm publish
|
|
8
|
+
"release": "npm publish",
|
|
9
9
|
"t": "tsc --project tsconfig.json --noEmit"
|
|
10
10
|
},
|
|
11
11
|
"bin": {
|
package/src/core/locale-list.ts
CHANGED
|
@@ -34,6 +34,8 @@ export function subscribeToLocaleChange(callback: () => void) {
|
|
|
34
34
|
subs.add(callback);
|
|
35
35
|
return () => { subs.delete(callback); };
|
|
36
36
|
}
|
|
37
|
+
subscribeToLocaleChange.count = () => subs.size;
|
|
38
|
+
|
|
37
39
|
export function getCurrentLocale(noDefault?: false): ILocaleDef;
|
|
38
40
|
export function getCurrentLocale(noDefault: true): ILocaleDef | null;
|
|
39
41
|
export function getCurrentLocale(noDefault = false) {
|
package/src/core/smartloc.tsx
CHANGED
|
@@ -204,10 +204,21 @@ function createKind<props extends object>(opts: {
|
|
|
204
204
|
// trick to create a function that is named like the given name
|
|
205
205
|
const creator = {
|
|
206
206
|
[opts.name](props: props): LocStr {
|
|
207
|
+
// This is a dirty hack to create some item that will both be recognized by React as a valid element,
|
|
208
|
+
// but that also behaves as a LocStr with the appropriate methods.
|
|
209
|
+
// 1) create a react element
|
|
207
210
|
const e = React.createElement(Renderer, props);
|
|
208
|
-
|
|
211
|
+
|
|
212
|
+
// 2) create a new instance of our LocStr kind
|
|
209
213
|
const ret = Object.create(Proto.prototype);
|
|
214
|
+
|
|
215
|
+
// 3) raw-copy what the react element has as properties
|
|
216
|
+
// ... this work because React.createElement() actually only creates a raw frozen object with some properties.
|
|
217
|
+
// see createElement() source code
|
|
218
|
+
const eProps = Object.getOwnPropertyDescriptors(e);
|
|
210
219
|
Object.defineProperties(ret, eProps);
|
|
220
|
+
|
|
221
|
+
// 4) set the isLoc flag to true so isLocStr() will recognize this instance
|
|
211
222
|
setIsLoc(ret); // this will freeze the object
|
|
212
223
|
return ret;
|
|
213
224
|
}
|
package/src/core/tag.ts
CHANGED
|
@@ -8,7 +8,12 @@ type Templater = (literals: TemplateStringsArray, ...placeholders: LocLiteral[])
|
|
|
8
8
|
declare function _tagSig(id: string): Templater
|
|
9
9
|
declare function _tagSig(literals: TemplateStringsArray, ...placeholders: LocLiteral[]): LocStr;
|
|
10
10
|
type TagSignature = typeof _tagSig;
|
|
11
|
-
type LocTag = TagSignature & {
|
|
11
|
+
type LocTag = TagSignature & {
|
|
12
|
+
/** Creates a pluralizable string (will have two versions to be translated based on the given count: singlular & plural) */
|
|
13
|
+
plural(count: number): TagSignature;
|
|
14
|
+
/** Does nothing, except that the translation will not be collected (use for formatting purposes) */
|
|
15
|
+
nocollect: LocTag
|
|
16
|
+
};
|
|
12
17
|
|
|
13
18
|
|
|
14
19
|
|
|
@@ -36,5 +41,8 @@ _loc.plural = (count: number) => {
|
|
|
36
41
|
}
|
|
37
42
|
return templater;
|
|
38
43
|
};
|
|
44
|
+
_loc.nocollect = () => {
|
|
45
|
+
return _loc;
|
|
46
|
+
};
|
|
39
47
|
|
|
40
48
|
export const loc: LocTag = _loc as any;
|