@refinitiv-ui/translate 5.2.5 → 5.4.0-alpha.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.
- package/CHANGELOG.md +11 -0
- package/README.md +1 -1
- package/package.json +10 -8
- package/lib/translate.d.ts +0 -43
- package/lib/translate.js +0 -137
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.3.0](https://github.com/Refinitiv/refinitiv-ui/compare/@refinitiv-ui/translate@5.2.5...@refinitiv-ui/translate@5.3.0) (2022-01-12)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* upgrade `lit` to 2.1.1 ([#156](https://github.com/Refinitiv/refinitiv-ui/issues/156)) ([358ca28](https://github.com/Refinitiv/refinitiv-ui/commit/358ca282491075973f12895bddbac990b79b00e6))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [5.2.5](https://github.com/Refinitiv/refinitiv-ui/compare/@refinitiv-ui/translate@5.2.4...@refinitiv-ui/translate@5.2.5) (2021-12-21)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @refinitiv-ui/translate
|
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
} from '@refinitiv-ui/translate';
|
|
32
32
|
|
|
33
33
|
// import default English language
|
|
34
|
-
import '@refinitiv-ui/phrasebook/
|
|
34
|
+
import '@refinitiv-ui/phrasebook/locale/en/my-translate-element';
|
|
35
35
|
|
|
36
36
|
@customElement('my-translate-element')
|
|
37
37
|
export class MyTranslateElement extends BasicElement {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@refinitiv-ui/translate",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.0-alpha.0",
|
|
4
4
|
"description": "i18n implementation for Element Framework components",
|
|
5
5
|
"author": "Refinitiv",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "tsc --sourceMap --declarationMap",
|
|
17
|
-
"build:prod": "
|
|
17
|
+
"build:prod": "echo \"No build:prod command found\"",
|
|
18
18
|
"build:watch": "npm run build -- --watch --preserveWatchOutput",
|
|
19
19
|
"lint": "eslint .",
|
|
20
20
|
"lint:fix": "eslint . --fix",
|
|
@@ -24,17 +24,19 @@
|
|
|
24
24
|
"test:watch": "npm run test -- --watch"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"
|
|
28
|
-
"@refinitiv-ui/phrasebook": "^5.4.0",
|
|
29
|
-
"lit": "^2.0.0",
|
|
27
|
+
"lit": "~2.1.1",
|
|
30
28
|
"tslib": "^2.3.1"
|
|
31
29
|
},
|
|
32
30
|
"devDependencies": {
|
|
33
|
-
"@refinitiv-ui/core": "^5.
|
|
34
|
-
"@refinitiv-ui/test-helpers": "^5.1.
|
|
31
|
+
"@refinitiv-ui/core": "^5.4.0",
|
|
32
|
+
"@refinitiv-ui/test-helpers": "^5.1.2"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@refinitiv-ui/i18n": "^5.3.0-alpha.0",
|
|
36
|
+
"@refinitiv-ui/phrasebook": "^5.5.0-alpha.0"
|
|
35
37
|
},
|
|
36
38
|
"publishConfig": {
|
|
37
39
|
"access": "public"
|
|
38
40
|
},
|
|
39
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "de44f7c988b30eec26d37336eb9c6dc6ba54da64"
|
|
40
42
|
}
|
package/lib/translate.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { BasicElement } from '@refinitiv-ui/core';
|
|
2
|
-
import { PartInfo, DirectiveResult } from 'lit/directive.js';
|
|
3
|
-
import { AsyncDirective } from 'lit/async-directive.js';
|
|
4
|
-
import { TranslateOptions, TranslateParams } from '@refinitiv-ui/i18n';
|
|
5
|
-
declare const TranslatePropertyKey: unique symbol;
|
|
6
|
-
declare type DecoratorOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* The scope translation should subscribe to.
|
|
9
|
-
* If not defined, equal to element local name
|
|
10
|
-
*/
|
|
11
|
-
scope?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Decorator mode.
|
|
14
|
-
* `directive` - used in render function as LitHTML directive
|
|
15
|
-
* `promise` - can be used everywhere as a promise
|
|
16
|
-
*/
|
|
17
|
-
mode?: 'directive' | 'promise';
|
|
18
|
-
};
|
|
19
|
-
declare type TranslateFunction = (prototype: BasicElement, name: PropertyKey) => void;
|
|
20
|
-
declare type TranslateDirective = (key: string, options?: TranslateOptions, translateParams?: TranslateParams) => DirectiveResult<typeof AsyncTranslateDirective>;
|
|
21
|
-
declare type TranslatePromise = (key: string, options?: TranslateOptions, translateParams?: TranslateParams) => Promise<string>;
|
|
22
|
-
declare type Translate = TranslateDirective | TranslatePromise;
|
|
23
|
-
declare class AsyncTranslateDirective extends AsyncDirective {
|
|
24
|
-
private readonly partType;
|
|
25
|
-
constructor(partInfo: PartInfo);
|
|
26
|
-
render(scope: string, locale: string, key: string, options?: TranslateOptions, translateParams?: TranslateParams): symbol;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Get locale of the element.
|
|
30
|
-
* Locale is resolved following the algorithm:
|
|
31
|
-
* `element.lang` -> `document.lang` -> `navigator.language`
|
|
32
|
-
* @param element HTML element
|
|
33
|
-
* @return element locale
|
|
34
|
-
*/
|
|
35
|
-
declare const getLocale: (element: HTMLElement) => string;
|
|
36
|
-
/**
|
|
37
|
-
* Binds an element to translation keys
|
|
38
|
-
* @param [options] decorator `options` or `scope`.
|
|
39
|
-
* If not provided provided, `scope = element.localName` and `mode = 'directive'`
|
|
40
|
-
* @returns translate directive
|
|
41
|
-
*/
|
|
42
|
-
declare const translate: (options?: string | DecoratorOptions | undefined) => TranslateFunction;
|
|
43
|
-
export { translate, TranslatePropertyKey, getLocale, Translate, TranslatePromise, TranslateDirective };
|
package/lib/translate.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { PartType } from 'lit/directive.js';
|
|
2
|
-
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
|
3
|
-
import { AsyncDirective, directive } from 'lit/async-directive.js';
|
|
4
|
-
import { noChange } from 'lit';
|
|
5
|
-
import { LangAttributeObserver, t } from '@refinitiv-ui/i18n';
|
|
6
|
-
import { Phrasebook } from '@refinitiv-ui/phrasebook';
|
|
7
|
-
const TranslatePropertyKey = Symbol('ef-translate');
|
|
8
|
-
class AsyncTranslateDirective extends AsyncDirective {
|
|
9
|
-
constructor(partInfo) {
|
|
10
|
-
super(partInfo);
|
|
11
|
-
this.partType = partInfo.type;
|
|
12
|
-
if (!(this.partType === PartType.CHILD || this.partType === PartType.ATTRIBUTE || this.partType === PartType.PROPERTY)) {
|
|
13
|
-
throw new Error('Element Framework Translate can only be used in content, attribute or property bindings');
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
render(scope, locale, key, options, translateParams) {
|
|
17
|
-
Promise.resolve(t(scope, locale, key, options, translateParams))
|
|
18
|
-
.then(message => {
|
|
19
|
-
this.setValue(this.partType === PartType.CHILD ? unsafeHTML(message) : message);
|
|
20
|
-
})
|
|
21
|
-
.catch(error => {
|
|
22
|
-
this.setValue(key);
|
|
23
|
-
// the code may fail if polyfills are not available in IE11 or translate syntax is wrong
|
|
24
|
-
/* istanbul ignore next */
|
|
25
|
-
setTimeout(() => {
|
|
26
|
-
throw new Error(error);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
return noChange;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
const translateDirective = directive(AsyncTranslateDirective);
|
|
33
|
-
const translatePromise = (scope, locale, key, options, translateParams) => {
|
|
34
|
-
return Promise.resolve(t(scope, locale, key, options, translateParams))
|
|
35
|
-
.then(message => {
|
|
36
|
-
return message;
|
|
37
|
-
})
|
|
38
|
-
.catch(error => {
|
|
39
|
-
// the code may fail if polyfills are not available in IE11 or translate syntax is wrong
|
|
40
|
-
/* istanbul ignore next */
|
|
41
|
-
setTimeout(() => {
|
|
42
|
-
throw new Error(error);
|
|
43
|
-
});
|
|
44
|
-
return key;
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* Get locale of the element.
|
|
49
|
-
* Locale is resolved following the algorithm:
|
|
50
|
-
* `element.lang` -> `document.lang` -> `navigator.language`
|
|
51
|
-
* @param element HTML element
|
|
52
|
-
* @return element locale
|
|
53
|
-
*/
|
|
54
|
-
const getLocale = (element) => element.lang || LangAttributeObserver.documentLang || navigator.language;
|
|
55
|
-
/**
|
|
56
|
-
* Start observing translations.
|
|
57
|
-
* This step is required when translations are loaded or the `lang` has been
|
|
58
|
-
* after the element has been rendered
|
|
59
|
-
* @param this Function scope. ELF Basic Element
|
|
60
|
-
* @param scope Scope
|
|
61
|
-
* @returns {void}
|
|
62
|
-
*/
|
|
63
|
-
const observeTranslations = function (scope = this.localName) {
|
|
64
|
-
let locale = getLocale(this);
|
|
65
|
-
// Observe `lang` attribute changes on
|
|
66
|
-
// document.documentElement and element itself
|
|
67
|
-
LangAttributeObserver.observe(this, () => {
|
|
68
|
-
const newLocale = getLocale(this);
|
|
69
|
-
if (locale !== newLocale) {
|
|
70
|
-
locale = newLocale;
|
|
71
|
-
// this ensures that requestUpdate always comes through
|
|
72
|
-
// however, external applications still can detect that the change is coming for translations
|
|
73
|
-
// this is better than empty requestUpdate() as in that case in is not possible to detect source of update
|
|
74
|
-
this.requestUpdate(TranslatePropertyKey, {});
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
// Observe new translations for the scope
|
|
78
|
-
return Phrasebook.observe(scope, () => {
|
|
79
|
-
this.requestUpdate(TranslatePropertyKey, {});
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* Disconnect translation listeners.
|
|
84
|
-
* This step is required to remove all element references from LangAttributeObserver and Phrasebook
|
|
85
|
-
* @param this Function scope. ELF Basic Element
|
|
86
|
-
* @param key Observe key from the `observeTranslations` function
|
|
87
|
-
* @returns {void}
|
|
88
|
-
*/
|
|
89
|
-
const disconnectTranslations = function (key) {
|
|
90
|
-
LangAttributeObserver.disconnect(this);
|
|
91
|
-
Phrasebook.disconnect(key);
|
|
92
|
-
};
|
|
93
|
-
/**
|
|
94
|
-
* Binds an element to translation keys
|
|
95
|
-
* @param [options] decorator `options` or `scope`.
|
|
96
|
-
* If not provided provided, `scope = element.localName` and `mode = 'directive'`
|
|
97
|
-
* @returns translate directive
|
|
98
|
-
*/
|
|
99
|
-
const translate = function (options) {
|
|
100
|
-
return (prototype, name) => {
|
|
101
|
-
const scope = options ? typeof options === 'string' ? options : options.scope : undefined;
|
|
102
|
-
const mode = options && typeof options !== 'string' ? options.mode : 'directive';
|
|
103
|
-
// Cannot use an element itself as a key.
|
|
104
|
-
// Element may have multiple translate directives with different scope
|
|
105
|
-
// Therefore we need a truly unique key
|
|
106
|
-
let key;
|
|
107
|
-
const connectedCallback = prototype.connectedCallback;
|
|
108
|
-
prototype.connectedCallback = function () {
|
|
109
|
-
connectedCallback.call(this);
|
|
110
|
-
key = observeTranslations.call(this, scope);
|
|
111
|
-
};
|
|
112
|
-
const disconnectedCallback = prototype.disconnectedCallback;
|
|
113
|
-
prototype.disconnectedCallback = function () {
|
|
114
|
-
disconnectedCallback.call(this);
|
|
115
|
-
disconnectTranslations.call(this, key);
|
|
116
|
-
};
|
|
117
|
-
const descriptor = mode === 'promise'
|
|
118
|
-
? {
|
|
119
|
-
get() {
|
|
120
|
-
return (key, options, translateParams) => {
|
|
121
|
-
return translatePromise(scope || this.localName, getLocale(this), key, options, translateParams);
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
} : {
|
|
125
|
-
get() {
|
|
126
|
-
return (key, options, translateParams) => {
|
|
127
|
-
return translateDirective(scope || this.localName, getLocale(this), key, options, translateParams);
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
Object.defineProperty(prototype, name, Object.assign({
|
|
132
|
-
enumerable: false,
|
|
133
|
-
configurable: false
|
|
134
|
-
}, descriptor));
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
export { translate, TranslatePropertyKey, getLocale };
|