-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
misc(treemap): i18n #12441
misc(treemap): i18n #12441
Changes from all commits
51bae87
7ff5861
3ca66c4
c06b918
139bcaa
cba6faa
e8dbc07
36e0838
576f374
4b0b721
12b1ba8
e150536
b76273f
78f168c
a97202d
44a203f
eddd1af
9a7f0f8
cdc698e
6f9e53c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,25 @@ | |
|
||
// Not named `NBSP` because that creates a duplicate identifier (util.js). | ||
const NBSP2 = '\xa0'; | ||
const KiB = 1024; | ||
const MiB = KiB * KiB; | ||
|
||
/** | ||
* @template T | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parameterizing on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving to Util/TreemapUtil SGTM, and so does worrying about it in a future refactor ;) |
||
*/ | ||
class I18n { | ||
/** | ||
* @param {LH.Locale} locale | ||
* @param {LH.I18NRendererStrings=} strings | ||
* @param {T=} strings | ||
*/ | ||
constructor(locale, strings) { | ||
// When testing, use a locale with more exciting numeric formatting. | ||
if (locale === 'en-XA') locale = 'de'; | ||
|
||
this._numberDateLocale = locale; | ||
this._numberFormatter = new Intl.NumberFormat(locale); | ||
this._strings = /** @type {LH.I18NRendererStrings} */ (strings || {}); | ||
this._percentFormatter = new Intl.NumberFormat(locale, {style: 'percent'}); | ||
this._strings = /** @type {T} */ (strings || {}); | ||
} | ||
|
||
get strings() { | ||
|
@@ -39,6 +45,15 @@ class I18n { | |
return this._numberFormatter.format(coarseValue); | ||
} | ||
|
||
/** | ||
* Format percent. | ||
* @param {number} number 0–1 | ||
* @return {string} | ||
*/ | ||
formatPercent(number) { | ||
return this._percentFormatter.format(number); | ||
} | ||
|
||
/** | ||
* @param {number} size | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 0.1 | ||
|
@@ -50,6 +65,17 @@ class I18n { | |
return `${kbs}${NBSP2}KiB`; | ||
} | ||
|
||
/** | ||
* @param {number} size | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 0.1 | ||
* @return {string} | ||
*/ | ||
formatBytesToMiB(size, granularity = 0.1) { | ||
const formatter = this._byteFormatterForGranularity(granularity); | ||
const kbs = formatter.format(Math.round(size / 1024 ** 2 / granularity) * granularity); | ||
return `${kbs}${NBSP2}MiB`; | ||
} | ||
|
||
/** | ||
* @param {number} size | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 1 | ||
|
@@ -61,6 +87,17 @@ class I18n { | |
return `${kbs}${NBSP2}bytes`; | ||
} | ||
|
||
/** | ||
* @param {number} size | ||
* @param {number=} granularity Controls how coarse the displayed value is, defaults to 1 | ||
* @return {string} | ||
*/ | ||
formatBytesWithBestUnit(size, granularity = 1) { | ||
if (size >= MiB) return this.formatBytesToMiB(size, granularity); | ||
if (size >= KiB) return this.formatBytesToKiB(size, granularity); | ||
return this.formatNumber(size, granularity) + '\xa0B'; | ||
} | ||
|
||
/** | ||
* Format bytes with a constant number of fractional digits, i.e for a granularity of 0.1, 10 becomes '10.0' | ||
* @param {number} granularity Controls how coarse the displayed value is | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,10 +118,9 @@ function saveLhlStrings(path, localeStrings) { | |
|
||
/** | ||
* @param {string} dir | ||
* @param {string} outputDir | ||
* @return {Array<string>} | ||
*/ | ||
function collectAndBakeCtcStrings(dir, outputDir) { | ||
function collectAndBakeCtcStrings(dir) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor refactor, didn't need two parameters. |
||
const lhlFilenames = []; | ||
for (const filename of fs.readdirSync(dir)) { | ||
const fullPath = path.join(dir, filename); | ||
|
@@ -131,7 +130,7 @@ function collectAndBakeCtcStrings(dir, outputDir) { | |
if (!process.env.CI) console.log('Baking', relativePath); | ||
const ctcStrings = loadCtcStrings(relativePath); | ||
const strings = bakePlaceholders(ctcStrings); | ||
const outputFile = outputDir + path.basename(filename).replace('.ctc', ''); | ||
const outputFile = path.join(dir, path.basename(filename).replace('.ctc', '')); | ||
saveLhlStrings(outputFile, strings); | ||
lhlFilenames.push(path.basename(filename)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no longer necessary, but is more correct.