Skip to content

Commit 80844f8

Browse files
committed
improve code and comment
1 parent 4798763 commit 80844f8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

web_src/js/webcomponents/GiteaLocaleNumber.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
// Or add a tooltip by data-number-in-tooltip attribute. JSON: {message: "count: %s", number: 123}
33
window.customElements.define('gitea-locale-number', class extends HTMLElement {
44
connectedCallback() {
5+
// ideally, the number locale formatting and plural processing should be done by backend with translation strings.
6+
// if we have complete backend locale support (eg: Golang "x/text" package), we can drop this component.
57
const number = this.getAttribute('data-number');
68
if (number) {
79
this.attachShadow({mode: 'open'});
810
this.shadowRoot.textContent = new Intl.NumberFormat().format(Number(number));
911
}
10-
let numberInTooltip = this.getAttribute('data-number-in-tooltip');
12+
const numberInTooltip = this.getAttribute('data-number-in-tooltip');
1113
if (numberInTooltip) {
12-
numberInTooltip = JSON.parse(numberInTooltip);
13-
let msg = numberInTooltip.message;
14-
msg = msg.replace(/%[ds]/, new Intl.NumberFormat().format(Number(numberInTooltip.number)));
15-
this.setAttribute('data-tooltip-content', msg);
14+
// TODO: only 2 usages of this, we can replace it with Golang's "x/text/number" package in the future
15+
const {message, number} = JSON.parse(numberInTooltip);
16+
const tooltipContent = message.replace(/%[ds]/, new Intl.NumberFormat().format(Number(number)));
17+
this.setAttribute('data-tooltip-content', tooltipContent);
1618
}
1719
}
1820
});

0 commit comments

Comments
 (0)