1
0
Fork 0
forked from forgejo/forgejo

Refactor locale number (#24134)

Before, the `GiteaLocaleNumber.js` was just written as a a drop-in
replacement for old `js-pretty-number`.

Actually, we can use Golang's `text` package to format.

This PR partially completes the TODOs in `GiteaLocaleNumber.js`:

> if we have complete backend locale support (eg: Golang "x/text"
package), we can drop this component.
> tooltip: only 2 usages of this, we can replace it with Golang's
"x/text/number" package in the future.

This PR also helps #24131

Screenshots:

<details>

![image](https://user-images.githubusercontent.com/2114189/232179420-b1b9974b-9d96-4408-b209-b80182c8b359.png)


![image](https://user-images.githubusercontent.com/2114189/232179416-14f36aa0-3f3e-4ac9-b366-7bd3a4464a11.png)

</details>
This commit is contained in:
wxiaoguang 2023-04-17 11:37:23 +08:00 committed by GitHub
parent be7cd73439
commit 7681d582cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 118 additions and 106 deletions

View file

@ -1,20 +0,0 @@
// Convert a number to a locale string by data-number attribute.
// Or add a tooltip by data-number-in-tooltip attribute. JSON: {message: "count: %s", number: 123}
window.customElements.define('gitea-locale-number', class extends HTMLElement {
connectedCallback() {
// ideally, the number locale formatting and plural processing should be done by backend with translation strings.
// if we have complete backend locale support (eg: Golang "x/text" package), we can drop this component.
const number = this.getAttribute('data-number');
if (number) {
this.attachShadow({mode: 'open'});
this.shadowRoot.textContent = new Intl.NumberFormat().format(Number(number));
}
const numberInTooltip = this.getAttribute('data-number-in-tooltip');
if (numberInTooltip) {
// TODO: only 2 usages of this, we can replace it with Golang's "x/text/number" package in the future
const {message, number} = JSON.parse(numberInTooltip);
const tooltipContent = message.replace(/%[ds]/, new Intl.NumberFormat().format(Number(number)));
this.setAttribute('data-tooltip-content', tooltipContent);
}
}
});

View file

@ -1,4 +1,3 @@
import '@webcomponents/custom-elements'; // polyfill for some browsers like Pale Moon
import '@github/relative-time-element';
import './GiteaLocaleNumber.js';
import './GiteaOriginUrl.js';