@refinitiv-ui/translate 5.2.0-next.0 → 5.2.0-next.1

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 CHANGED
@@ -3,6 +3,14 @@
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.2.0-next.1](https://git.sami.int.thomsonreuters.com/elf/refinitiv-ui/compare/@refinitiv-ui/translate@5.2.0-next.0...@refinitiv-ui/translate@5.2.0-next.1) (2021-09-20)
7
+
8
+ **Note:** Version bump only for package @refinitiv-ui/translate
9
+
10
+
11
+
12
+
13
+
6
14
  # [5.2.0-next.0](https://git.sami.int.thomsonreuters.com/elf/refinitiv-ui/compare/@refinitiv-ui/translate@5.1.5-next.0...@refinitiv-ui/translate@5.2.0-next.0) (2021-09-16)
7
15
 
8
16
 
@@ -1,3 +1,5 @@
1
+ import { PartInfo, DirectiveResult } from 'lit/directive.js';
2
+ import { AsyncDirective } from 'lit/async-directive.js';
1
3
  import { BasicElement } from '@refinitiv-ui/core';
2
4
  import { TranslateOptions, TranslateParams } from '@refinitiv-ui/i18n';
3
5
  declare const TranslatePropertyKey: unique symbol;
@@ -15,9 +17,14 @@ declare type DecoratorOptions = {
15
17
  mode?: 'directive' | 'promise';
16
18
  };
17
19
  declare type TranslateFunction = (prototype: BasicElement, name: PropertyKey) => void;
18
- declare type TranslateDirective = (key: string, options?: TranslateOptions, translateParams?: TranslateParams) => void;
20
+ declare type TranslateDirective = (key: string, options?: TranslateOptions, translateParams?: TranslateParams) => DirectiveResult<typeof AsyncTranslateDirective>;
19
21
  declare type TranslatePromise = (key: string, options?: TranslateOptions, translateParams?: TranslateParams) => Promise<string>;
20
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
+ }
21
28
  /**
22
29
  * Get locale of the element.
23
30
  * Locale is resolved following the algorithm:
package/lib/translate.js CHANGED
@@ -1,47 +1,35 @@
1
- import { directive, NodePart, AttributePart, PropertyPart, isPrimitive } from 'lit-html';
1
+ import { PartType } from 'lit/directive.js';
2
+ import { AsyncDirective, directive } from 'lit/async-directive.js';
3
+ import { unsafeHTML } from '@refinitiv-ui/core';
4
+ import { noChange } from 'lit';
2
5
  import { LangAttributeObserver, t } from '@refinitiv-ui/i18n';
3
6
  import { Phrasebook } from '@refinitiv-ui/phrasebook';
4
7
  const TranslatePropertyKey = Symbol('ef-translate');
5
- // For each part, remember the value that was last rendered to the part
6
- const previousNodeValues = new WeakMap();
7
- const updatePart = (part, value) => {
8
- // support unsafeHTML syntax
9
- if (part instanceof NodePart) {
10
- const previousValue = previousNodeValues.get(part);
11
- if (previousValue !== undefined && isPrimitive(value) && value === previousValue.value && part.value === previousValue.fragment) {
12
- return;
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');
13
14
  }
14
- const template = document.createElement('template');
15
- template.innerHTML = value; // innerHTML casts to string internally
16
- const fragment = document.importNode(template.content, true);
17
- previousNodeValues.set(part, { value, fragment });
18
- part.setValue(fragment);
19
- part.commit();
20
- return;
21
15
  }
22
- part.setValue(value);
23
- part.commit();
24
- };
25
- const translateDirective = directive((scope, locale, key, options, translateParams) => (part) => {
26
- /* istanbul ignore next */
27
- if (!(part instanceof NodePart || part instanceof AttributePart || part instanceof PropertyPart)) {
28
- throw new Error('Element Framework Translate can only be used in content, attribute or property bindings');
29
- }
30
- // TODO: support additional API to show some text while translations are loading
31
- // it can be just an English translation, greyed div, loading mask etc.
32
- Promise.resolve(t(scope, locale, key, options, translateParams))
33
- .then(message => {
34
- updatePart(part, message);
35
- })
36
- .catch(error => {
37
- updatePart(part, key);
38
- // the code may fail if polyfills are not available in IE11 or translate syntax is wrong
39
- /* istanbul ignore next */
40
- setTimeout(() => {
41
- throw new Error(error);
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
+ });
42
28
  });
43
- });
44
- });
29
+ return noChange;
30
+ }
31
+ }
32
+ const translateDirective = directive(AsyncTranslateDirective);
45
33
  const translatePromise = (scope, locale, key, options, translateParams) => {
46
34
  return Promise.resolve(t(scope, locale, key, options, translateParams))
47
35
  .then(message => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@refinitiv-ui/translate",
3
- "version": "5.2.0-next.0",
3
+ "version": "5.2.0-next.1",
4
4
  "description": "i18n implementation for Element Framework components",
5
5
  "author": "Refinitiv",
6
6
  "license": "Apache-2.0",
@@ -23,16 +23,16 @@
23
23
  "test:watch": "npm run test -- --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@refinitiv-ui/core": "^5.1.0-next.0",
27
- "@refinitiv-ui/i18n": "^5.1.5-next.1",
26
+ "@refinitiv-ui/core": "^5.1.0-next.1",
27
+ "@refinitiv-ui/i18n": "^5.1.5-next.2",
28
28
  "@refinitiv-ui/phrasebook": "^5.1.3-next.1",
29
- "lit-html": "^1.4.1"
29
+ "lit": "^2.0.0-rc.4"
30
30
  },
31
31
  "devDependencies": {
32
- "@refinitiv-ui/test-helpers": "^5.0.2-next.1"
32
+ "@refinitiv-ui/test-helpers": "^5.0.2-next.2"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
- "gitHead": "2af1587b19b6397e123bb85ad793042cf0182d4a"
37
+ "gitHead": "1a106b6e43552d132ebff6e0bac71045a6de9a82"
38
38
  }