Skip to content

Commit 91e16a1

Browse files
authored
fix(ui5-icon): ui5-icon i18n works for all packages (#2816)
1 parent 23ff62d commit 91e16a1

File tree

8 files changed

+44
-27
lines changed

8 files changed

+44
-27
lines changed

packages/base/src/asset-registries/Icons.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ const _fillRegistry = bundleData => {
3636
ltr: iconData.ltr,
3737
accData: iconData.acc,
3838
collection: bundleData.collection,
39+
packageName: bundleData.packageName,
3940
});
4041
});
4142
};
4243

4344
// set
44-
const registerIcon = (name, { pathData, ltr, accData, collection } = {}) => { // eslint-disable-line
45+
const registerIcon = (name, { pathData, ltr, accData, collection, packageName } = {}) => { // eslint-disable-line
4546
if (!collection) {
4647
collection = DEFAULT_COLLECTION;
4748
}
4849

4950
const key = `${collection}/${name}`;
50-
registry.set(key, { pathData, ltr, accData });
51+
registry.set(key, {
52+
pathData,
53+
ltr,
54+
accData,
55+
packageName,
56+
});
5157
};
5258

5359
const _parseName = name => {

packages/fiori/src/Assets.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// main package assets (transitively base and theme-base)
1+
// main package assets (transitively base, theme-base and icons)
22
import "@ui5/webcomponents/dist/Assets.js";
33

44
// own fiori package assets

packages/icons-tnt/src/SAP-icons-TNT.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"collection": "tnt",
3+
"packageName": "@ui5/webcomponents-icons-tnt",
34
"data": {
45
"actor": {
56
"path": "M254.871 140q-29 0-49-20.5t-20-49.5 20-49.5 49-20.5 50 20.5 21 49.5-21 49.5-50 20.5zm0-116q-18 0-32 13.5t-14 32.5 14 33 32 14q20 0 33.5-14t13.5-33-13.5-32.5-33.5-13.5zm140 48q9-6 21-6 16 0 29.5 12t11.5 32q-2 14-15 24l-100 76q-7 7-7 15v250q0 15-10.5 26t-27.5 11q-15 0-26-11t-11-26V352h-8v123q0 15-11 26t-26 11q-17 0-27.5-11t-10.5-26V225q0-8-14-20.5t-32-26.5-35.5-26-25.5-18q-12-9-14.5-21t2.5-23 15-18.5 22-7.5q13 0 22 8l107 80h64l35-25zm-171 106q-2 0-6-2t-16-10-35.5-25.5-65.5-48.5q-5-4-11-4-9 0-12.5 9t7.5 17l100 75q17 14 17 36v250q0 11 13 11 5 0 8.5-3t3.5-8V347q0-20 20-20h18q20 0 20 20v128q0 5 3.5 8t8.5 3q13 0 13-11V225q0-22 17-36l100-75q10-7 6.5-15.5t-13.5-8.5q-6 0-9 2l-94 71-17.5 12.5-7.5 2.5h-68z"
@@ -449,4 +450,4 @@
449450
"path": "M179 96V64h77v128h51v-19q0-11 7.5-18t18.5-7h153q11 0 18.5 7t7.5 18v154q0 11-7.5 18t-18.5 7H333q-11 0-18.5-7t-7.5-18V224h-83V96h-45zM10 358h133q11 0 11 11v47h22v32h-22v54q0 10-11 10H10q-10 0-10-10V369q0-11 10-11zM0 80q0-33 23.5-56.5T80 0t56.5 23.5T160 80t-23.5 56.5T80 160t-56.5-23.5T0 80zm461 221V199H358v102h103zM192 416h32v-32h32v64h-64v-32zm32-160h32v32h-32v-32zm32 64v32h-32v-32h32z"
450451
}
451452
}
452-
}
453+
}

packages/icons/src/SAP-icons.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"collection": "SAP-icons",
3+
"packageName": "@ui5/webcomponents-icons",
34
"data": {
45
"Chart-Tree-Map": {
56
"path": "M32 472V40q0-8 8-8h176q8 0 8 8v432q0 8-8 8H40q-8 0-8-8zm224-256V40q0-8 8-8h208q8 0 8 8v176q0 8-8 8H264q-8 0-8-8zm0 48q0-8 8-8h80q8 0 8 8v208q0 8-8 8h-80q-8 0-8-8V264zm128 0q0-8 8-8h80q8 0 8 8v80q0 8-8 8h-80q-8 0-8-8v-80zm0 128q0-8 8-8h80q8 0 8 8v80q0 8-8 8h-80q-8 0-8-8v-80z"

packages/main/src/Assets.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "@ui5/webcomponents-localization/dist/Assets.js"; // CLDR
22
import "@ui5/webcomponents-theme-base/dist/Assets.js"; // Theming
3+
import "@ui5/webcomponents-icons/dist/Assets.js"; // Icons texts
34

45
// own main package assets
56
import "./generated/json-imports/Themes.js";

packages/main/src/Icon.hbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
role="{{role}}"
77
focusable="false"
88
preserveAspectRatio="xMidYMid meet"
9-
aria-label="{{accessibleNameText}}"
9+
aria-label="{{effectiveAccessibleName}}"
1010
xmlns="http://www.w3.org/2000/svg"
1111
@focusin={{_onfocusin}}
1212
@focusout={{_onfocusout}}
@@ -15,7 +15,7 @@
1515
@click={{_onclick}}
1616
>
1717
{{#if hasIconTooltip}}
18-
<title id="{{_id}}-tooltip">{{accessibleNameText}}</title>
18+
<title id="{{_id}}-tooltip">{{effectiveAccessibleName}}</title>
1919
{{/if}}
2020

2121
<g role="presentation">

packages/main/src/Icon.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import { getIconData, getIconDataSync } from "@ui5/webcomponents-base/dist/asset-registries/Icons.js";
44
import createStyleInHead from "@ui5/webcomponents-base/dist/util/createStyleInHead.js";
5-
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
5+
import { getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
6+
import { getI18nBundleData, fetchI18nBundle } from "@ui5/webcomponents-base/dist/asset-registries/i18n.js";
67
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
78
import isLegacyBrowser from "@ui5/webcomponents-base/dist/isLegacyBrowser.js";
89
import IconTemplate from "./generated/templates/IconTemplate.lit.js";
@@ -113,6 +114,14 @@ const metadata = {
113114
invalid: {
114115
type: Boolean,
115116
},
117+
118+
/**
119+
* @private
120+
*/
121+
effectiveAccessibleName: {
122+
type: String,
123+
noAttribute: true,
124+
},
116125
},
117126
events: {
118127
/**
@@ -158,11 +167,6 @@ const metadata = {
158167
* @public
159168
*/
160169
class Icon extends UI5Element {
161-
constructor() {
162-
super();
163-
this.i18nBundle = getI18nBundle("@ui5/webcomponents-icons");
164-
}
165-
166170
static get metadata() {
167171
return metadata;
168172
}
@@ -181,7 +185,6 @@ class Icon extends UI5Element {
181185

182186
static async onDefine() {
183187
this.createGlobalStyle(); // hide all icons until the first icon has rendered (and added the Icon.css)
184-
await fetchI18nBundle("@ui5/webcomponents-icons");
185188
}
186189

187190
_onfocusin(event) {
@@ -235,7 +238,7 @@ class Icon extends UI5Element {
235238
return "button";
236239
}
237240

238-
return this.accessibleNameText ? "img" : "presentation";
241+
return this.effectiveAccessibleName ? "img" : "presentation";
239242
}
240243

241244
static createGlobalStyle() {
@@ -284,18 +287,21 @@ class Icon extends UI5Element {
284287
this.pathData = iconData.pathData;
285288
this.accData = iconData.accData;
286289
this.ltr = iconData.ltr;
287-
}
288-
289-
get hasIconTooltip() {
290-
return this.showTooltip && this.accessibleNameText;
291-
}
290+
this.packageName = iconData.packageName;
292291

293-
get accessibleNameText() {
294292
if (this.accessibleName) {
295-
return this.accessibleName;
293+
this.effectiveAccessibleName = this.accessibleName;
294+
} else if (this.accData) {
295+
if (!getI18nBundleData(this.packageName)) {
296+
await fetchI18nBundle(this.packageName);
297+
}
298+
const i18nBundle = getI18nBundle(this.packageName);
299+
this.effectiveAccessibleName = i18nBundle.getText(this.accData) || undefined;
296300
}
301+
}
297302

298-
return this.i18nBundle.getText(this.accData) || undefined;
303+
get hasIconTooltip() {
304+
return this.showTooltip && this.effectiveAccessibleName;
299305
}
300306

301307
async onEnterDOM() {

packages/tools/lib/create-icons/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ const destDir = path.normalize("dist/");
88

99
mkdirp.sync(destDir);
1010

11-
const template = (name, pathData, ltr, collection) => `import { registerIcon } from "@ui5/webcomponents-base/dist/asset-registries/Icons.js";
11+
const template = (name, pathData, ltr, collection, packageName) => `import { registerIcon } from "@ui5/webcomponents-base/dist/asset-registries/Icons.js";
1212
1313
const name = "${name}";
1414
const pathData = "${pathData}";
1515
const ltr = ${ltr};
1616
const collection = "${collection}";
17+
const packageName = "${packageName}";
1718
18-
registerIcon(name, { pathData, ltr, collection});
19+
registerIcon(name, { pathData, ltr, collection, packageName });
1920
2021
export default { pathData };`;
2122

2223

23-
const accTemplate = (name, pathData, ltr, accData, collection) => `import { registerIcon } from "@ui5/webcomponents-base/dist/asset-registries/Icons.js";
24+
const accTemplate = (name, pathData, ltr, accData, collection, packageName) => `import { registerIcon } from "@ui5/webcomponents-base/dist/asset-registries/Icons.js";
2425
import { ${accData.key} } from "./generated/i18n/i18n-defaults.js";
2526
2627
const name = "${name}";
2728
const pathData = "${pathData}";
2829
const ltr = ${ltr};
2930
const accData = ${accData.key};
3031
const collection = "${collection}";
32+
const packageName = "${packageName}";
3133
32-
registerIcon(name, { pathData, ltr, accData, collection });
34+
registerIcon(name, { pathData, ltr, accData, collection, packageName });
3335
3436
export default { pathData, accData };`;
3537

@@ -46,7 +48,7 @@ const createIcons = (file) => {
4648
const ltr = !!iconData.ltr;
4749
const acc = iconData.acc;
4850

49-
const content = acc ? accTemplate(name, pathData, ltr, acc, json.collection) : template(name, pathData, ltr, json.collection);
51+
const content = acc ? accTemplate(name, pathData, ltr, acc, json.collection, json.packageName) : template(name, pathData, ltr, json.collection, json.packageName);
5052

5153
fs.writeFileSync(path.join(destDir, `${name}.js`), content);
5254
fs.writeFileSync(path.join(destDir, `${name}.svg`), svgTemplate(pathData));

0 commit comments

Comments
 (0)