-
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 14 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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,21 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
const fs = require('fs'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const GhPagesApp = require('./gh-pages-app.js'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
function buildLocales() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const locales = require('../lighthouse-core/lib/i18n/locales.js'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const clonedLocales = JSON.parse(JSON.stringify(locales)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
for (const lhlMessages of Object.values(clonedLocales)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
for (const key of Object.keys(lhlMessages)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (!key.startsWith('lighthouse-treemap')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
delete lhlMessages[key]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return 'const locales =' + JSON.stringify(clonedLocales, null, 2) + ';'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. with something this specially injected, WDYT about the name being a little more in your face?
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. I don't think the cloak and dagger is necessary here, since we control the code that runs here (and it's not a library) :) 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. I'm mostly interested in improving the experience for someone reading 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. A comment then? 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. self-describing variable names >> comments :) but yes a comment would somewhat address my concern too. |
||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. just for fun since we can use
Suggested change
(not serious review, just curious) :) 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. because it's nested, i prefer the current approach 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. haha, agree with @connorjclark on this one :) |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Build treemap app, optionally deploying to gh-pages if `--deploy` flag was set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -28,7 +43,9 @@ async function run() { | |||||||||||||||||||||||||||||||||||||||||||||||||
fs.readFileSync(require.resolve('tabulator-tables/dist/js/modules/format.js'), 'utf8'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
fs.readFileSync(require.resolve('tabulator-tables/dist/js/modules/resize_columns.js'), 'utf8'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
/* eslint-enable max-len */ | ||||||||||||||||||||||||||||||||||||||||||||||||||
{path: 'src/*'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
buildLocales(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
{path: '../../lighthouse-core/report/html/renderer/i18n.js'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
{path: 'src/**/*'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||
assets: [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
{path: 'debug.json'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ const license = `/* | |
* @return {string[]} | ||
*/ | ||
function loadFiles(pattern) { | ||
const filePaths = glob.sync(pattern); | ||
const filePaths = glob.sync(pattern, {nodir: true}); | ||
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. no longer necessary, but is more correct. |
||
return filePaths.map(path => fs.readFileSync(path, {encoding: 'utf8'})); | ||
} | ||
|
||
|
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)); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -569,7 +569,18 @@ describe('ReportUIFeatures', () => { | |
const container = render(sampleResults); | ||
for (const node of dom.findAll('[data-i18n]', container)) { | ||
const val = node.getAttribute('data-i18n'); | ||
assert.ok(val in Util.UIStrings, `Invalid data-i18n value of: "${val}" found.`); | ||
assert.ok(val in Util.UIStrings, `Invalid data-i18n value of: "${val}" not found.`); | ||
} | ||
|
||
// Do the same for the strings in treemap app. | ||
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. can this move to a treemap test since 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. also, FWIW, if it's just the single string, could just set it manually and avoid the loop here and in 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. there's just so much BLEGH to do re: jsdom setup... altho I guess it could go into the puppeteer page?
I think there will be more later (if we do the settings cog). 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.
normally true but I think it's all self contained in this case. Free test for you: const assert = require('assert').strict;
const fs = require('fs');
const jsdom = require('jsdom');
describe('data-i18n', () => {
it('should have only valid data-i18n values in treemap html', () => {
const TreemapUtil = require('../../../../../lighthouse-treemap/app/src/util.js');
const TREEMAP_INDEX = fs.readFileSync(__dirname +
'/../../../../../lighthouse-treemap/app/index.html', 'utf8');
const dom = new jsdom.JSDOM(TREEMAP_INDEX);
for (const node of dom.window.document.querySelectorAll('[data-i18n]')) {
const val = node.getAttribute('data-i18n');
assert.ok(val in TreemapUtil.UIStrings, `Invalid data-i18n value of: "${val}" not found.`);
}
});
}); though the paths will have to be updated :) |
||
const TreemapUtil = require('../../../../../lighthouse-treemap/app/src/util.js'); | ||
const TREEMAP_INDEX = fs.readFileSync(__dirname + | ||
'/../../../../../lighthouse-treemap/app/index.html', 'utf8'); | ||
const document = new jsdom.JSDOM(TREEMAP_INDEX); | ||
dom = new DOM(document.window.document); | ||
for (const node of dom.findAll('[data-i18n]', dom._document)) { | ||
const val = node.getAttribute('data-i18n'); | ||
assert.ok(val in TreemapUtil.UIStrings, `Invalid data-i18n value of: "${val}" not found.`); | ||
} | ||
}); | ||
}); | ||
|
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.
a jsdoc comment on this function would be appreciated so you don't have to know the structure of
locales.js
and the files it requires and what the significance ofkey.startsWith('lighthouse-treemap')
is to know what this is doing to the locale files :)