Skip to content

Commit 15caac2

Browse files
bmeurerDevtools-frontend LUCI CQ
authored andcommitted
[docs] Add number formatting docs to UX Style Guide.
Fixed: 377814746 Bug: 372723536, 357513556 Change-Id: I3a0dd5734effabaf8ba6c8370750ec046aaa7d9c Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6038157 Reviewed-by: Alex Rudenko <[email protected]> Commit-Queue: Alex Rudenko <[email protected]> Auto-Submit: Benedikt Meurer <[email protected]>
1 parent 59ac9c0 commit 15caac2

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

docs/styleguide/ux/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ user interface.
1010
1. [How to organize UI](organizing.md)
1111
1. [How to style UI](styleguide.md)
1212
1. [How to write UI text](writing.md)
13+
1. [How to format numbers](numbers.md)
1314
1. [Glossary](glossary.md)
1415

1516
## UI reviews

docs/styleguide/ux/numbers.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# How to format numbers
2+
3+
[TOC]
4+
5+
## Checklist
6+
7+
### General
8+
9+
Use [Intl.NumberFormat] consistently for all number formatting within Chrome
10+
DevTools' UI, including - but not limited to - formatting bytes and times. This
11+
way we ensure that numbers are formatted the same way throughout the whole UI
12+
and we don't put the burden of ensuring this level of consistency on the
13+
translators for the various languages.
14+
15+
### Text length
16+
17+
Pay attention to text length, because the unit portion can be longer in different
18+
locales, sometimes even longer than 4 characters. Try to alleviate the worse UX
19+
of strings being too long.
20+
21+
Concretely these locales produces the longest unit strings, so make sure to
22+
explicitly test for them:
23+
24+
* `th` (Thai)
25+
* `vi` (Vietnamese)
26+
* `hi` (Hindi)
27+
* `kn` (Kannada)
28+
* `ur` (Urdu)
29+
30+
We also suggest to explicitly check `en-US` (English) and `de` (German) locales.
31+
32+
## Examples
33+
34+
### Formatting percentages
35+
36+
Consider the following snippet for formatting percentage values:
37+
38+
```js
39+
const {locale} = i18n.DevToolsLocale.DevToolsLocale.instance();
40+
const formatter = new Intl.NumberFormat(locale, {
41+
style: 'percent',
42+
maximumFractionDigits: 1,
43+
});
44+
const formattedValue = formatter.format(percentageValue);
45+
```
46+
47+
Note that this code will only work after the `DevToolsLocale` has been
48+
initialized.
49+
50+
### Formatting bytes
51+
52+
Prefer the helper exported by [ByteUtilities.ts] when formatting byte values.
53+
For example:
54+
55+
```js
56+
// Formatting bytes
57+
i18n.ByteUtilities.bytesToString(99);
58+
59+
// Formatting kilobytes
60+
i18n.ByteUtilities.bytesToString(1234);
61+
62+
// Formatting megabytes
63+
i18n.ByteUtilities.bytesToString(1500 * 1000);
64+
```
65+
66+
### Formatting times
67+
68+
Prefer the helpers exported by [time-utilities.ts] when formatting time values.
69+
For example:
70+
71+
```js
72+
// Formatting precise milliseconds
73+
i18n.TimeUtilities.preciseMillisToString(6.12345);
74+
75+
// Formatting milliseconds (lossy)
76+
i18n.TimeUtilities.millisToString(26500);
77+
78+
// Formatting seconds
79+
i18n.TimeUtilities.secondsToString(7849);
80+
```
81+
82+
## Tips
83+
84+
### Defining formatters upfront
85+
86+
Use `defineFormatter()` from the [NumberFormatter.ts] file in order to define
87+
a formatter upfront. The function has some magic built into such that it defers
88+
the creation of the underlying `Intl.NumberFormat` instance until the first
89+
invocation of `format()` or `formatToParts()` methods, and therefore it's safe
90+
to define the formatters this way without running into the issue that the
91+
`DevToolsLocale` isn't initialized yet:
92+
93+
```js
94+
import * as i18n from 'path/to/i18n/i18n.js';
95+
96+
const percentageFormatter = i18n.NumberFormatter.defineFormatter({
97+
style: 'percent',
98+
maximumFractionDigits: 1,
99+
});
100+
```
101+
102+
103+
[Intl.NumberFormat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
104+
[ByteUtilities.ts]: ../../../front_end/core/i18n/ByteUtilities.ts
105+
[NumberFormatter.ts]: ../../../front_end/core/i18n/NumberFormatter.ts
106+
[time-utilities.ts]: ../../../front_end/core/i18n/time-utilities.ts

0 commit comments

Comments
 (0)