-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
core(i18n): export rendererFormattedStrings #5713
Changes from 1 commit
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 |
---|---|---|
|
@@ -35,6 +35,11 @@ class ReportRenderer { | |
renderReport(report, container) { | ||
// If any mutations happen to the report within the renderers, we want the original object untouched | ||
const clone = /** @type {LH.ReportResult} */ (JSON.parse(JSON.stringify(report))); | ||
// Mutate the UIStrings if necessary (while saving originals) | ||
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. should the technical debt comment below be moved up here? :) It does kind of suck to rely on utils as a mutable singleton. We pass at least a Fine to punt to a future PR, but it would be nice to have a plan before we get too deep. 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.
This sounds good to me, we have other LHR reaching needs like locale in config settings and such that would be nice. I accept your offer for a followup :) 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. add a TODO? |
||
ReportRenderer.stashUIStrings(); | ||
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. if it's stashing just for this function, can it stored in a local variable instead of stashed in a static property? 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. done |
||
if (clone.i18n && clone.i18n.rendererFormattedStrings) { | ||
ReportRenderer.updateAllUIStrings(clone.i18n.rendererFormattedStrings); | ||
} | ||
|
||
// TODO(phulce): we all agree this is technical debt we should fix | ||
if (typeof clone.categories !== 'object') throw new Error('No categories provided.'); | ||
|
@@ -43,6 +48,10 @@ class ReportRenderer { | |
|
||
container.textContent = ''; // Remove previous report. | ||
container.appendChild(this._renderReport(clone)); | ||
|
||
// put the UIStrings back into original state | ||
ReportRenderer.updateAllUIStrings(ReportRenderer._UIStringsStash); | ||
|
||
return /** @type {Element} **/ (container); | ||
} | ||
|
||
|
@@ -126,6 +135,9 @@ class ReportRenderer { | |
} | ||
|
||
const container = this._dom.cloneTemplate('#tmpl-lh-warnings--toplevel', this._templateContext); | ||
const message = this._dom.find('.lh-warnings__msg', container); | ||
message.textContent = Util.UIStrings.toplevelWarningsMessage; | ||
|
||
const warnings = this._dom.find('ul', container); | ||
for (const warningString of report.runWarnings) { | ||
const warning = warnings.appendChild(this._dom.createElement('li')); | ||
|
@@ -187,6 +199,8 @@ class ReportRenderer { | |
|
||
if (scoreHeader) { | ||
const scoreScale = this._dom.cloneTemplate('#tmpl-lh-scorescale', this._templateContext); | ||
this._dom.find('.lh-scorescale-label', scoreScale).textContent = | ||
Util.UIStrings.scorescaleLabel; | ||
scoresContainer.appendChild(scoreHeader); | ||
scoresContainer.appendChild(scoreScale); | ||
} | ||
|
@@ -213,8 +227,24 @@ class ReportRenderer { | |
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {LH.I18NRendererStrings} rendererFormattedStrings | ||
*/ | ||
static updateAllUIStrings(rendererFormattedStrings) { | ||
for (const [key, value] of Object.entries(rendererFormattedStrings)) { | ||
Util.UIStrings[key] = value; | ||
} | ||
} | ||
|
||
static stashUIStrings() { | ||
ReportRenderer._UIStringsStash = JSON.parse(JSON.stringify(Util.UIStrings)); | ||
} | ||
} | ||
|
||
/** @type {LH.I18NRendererStrings} */ | ||
ReportRenderer._UIStringsStash = {}; | ||
|
||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = ReportRenderer; | ||
} else { | ||
|
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.
if warnings are full sentences this should be fine
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.
alright, removed