-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(i18n): initial utility library (#5644)
- Loading branch information
1 parent
e94be2c
commit 589162b
Showing
16 changed files
with
354 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* @license Copyright 2018 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const MessageFormat = require('intl-messageformat').default; | ||
const MessageParser = require('intl-messageformat-parser'); | ||
const LOCALES = require('./locales'); | ||
|
||
let locale = MessageFormat.defaultLocale; | ||
|
||
const LH_ROOT = path.join(__dirname, '../../'); | ||
|
||
try { | ||
// Node usually doesn't come with the locales we want built-in, so load the polyfill. | ||
// In browser environments, we won't need the polyfill, and this will throw so wrap in try/catch. | ||
|
||
// @ts-ignore | ||
const IntlPolyfill = require('intl'); | ||
// @ts-ignore | ||
Intl.NumberFormat = IntlPolyfill.NumberFormat; | ||
// @ts-ignore | ||
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat; | ||
} catch (_) {} | ||
|
||
const UIStrings = { | ||
ms: '{timeInMs, number, milliseconds}\xa0ms', | ||
columnURL: 'URL', | ||
columnSize: 'Size (KB)', | ||
columnWastedTime: 'Potential Savings (ms)', | ||
}; | ||
|
||
const formats = { | ||
number: { | ||
milliseconds: { | ||
maximumFractionDigits: 0, | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* @param {string} msg | ||
* @param {Record<string, *>} values | ||
*/ | ||
function preprocessMessageValues(msg, values) { | ||
const parsed = MessageParser.parse(msg); | ||
// Round all milliseconds to 10s place | ||
parsed.elements | ||
.filter(el => el.format && el.format.style === 'milliseconds') | ||
.forEach(el => (values[el.id] = Math.round(values[el.id] / 10) * 10)); | ||
|
||
// Replace all the bytes with KB | ||
parsed.elements | ||
.filter(el => el.format && el.format.style === 'bytes') | ||
.forEach(el => (values[el.id] = values[el.id] / 1024)); | ||
} | ||
|
||
module.exports = { | ||
UIStrings, | ||
/** | ||
* @param {string} filename | ||
* @param {Record<string, string>} fileStrings | ||
*/ | ||
createStringFormatter(filename, fileStrings) { | ||
const mergedStrings = {...UIStrings, ...fileStrings}; | ||
|
||
/** @param {string} msg @param {*} [values] */ | ||
const formatFn = (msg, values) => { | ||
const keyname = Object.keys(mergedStrings).find(key => mergedStrings[key] === msg); | ||
if (!keyname) throw new Error(`Could not locate: ${msg}`); | ||
preprocessMessageValues(msg, values); | ||
|
||
const filenameToLookup = keyname in UIStrings ? __filename : filename; | ||
const lookupKey = path.relative(LH_ROOT, filenameToLookup) + '!#' + keyname; | ||
const localeStrings = LOCALES[locale] || {}; | ||
const localeString = localeStrings[lookupKey] && localeStrings[lookupKey].message; | ||
// fallback to the original english message if we couldn't find a message in the specified locale | ||
// better to have an english message than no message at all, in some number cases it won't even matter | ||
const messageForMessageFormat = localeString || msg; | ||
// when using accented english, force the use of a different locale for number formatting | ||
const localeForMessageFormat = locale === 'en-XA' ? 'de-DE' : locale; | ||
|
||
const formatter = new MessageFormat(messageForMessageFormat, localeForMessageFormat, formats); | ||
return formatter.format(values); | ||
}; | ||
|
||
return formatFn; | ||
}, | ||
/** | ||
* @param {LH.Locale|null} [newLocale] | ||
*/ | ||
setLocale(newLocale) { | ||
if (!newLocale) return; | ||
locale = newLocale; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"lighthouse-core/audits/byte-efficiency/render-blocking-resources.js!#title": { | ||
"message": "Eliminate render-blocking resources" | ||
}, | ||
"lighthouse-core/audits/byte-efficiency/render-blocking-resources.js!#description": { | ||
"message": "Resources are blocking the first paint of your page. Consider delivering critical JS/CSS inline and deferring all non-critical JS/styles. [Learn more](https://developers.google.com/web/tools/lighthouse/audits/blocking-resources)." | ||
}, | ||
"lighthouse-core/audits/byte-efficiency/render-blocking-resources.js!#displayValue": { | ||
"message": "{itemCount, plural,\n one {1 resource}\n other {# resources}\n } delayed first paint by {timeInMs, number, milliseconds} ms" | ||
}, | ||
"lighthouse-core/audits/metrics/interactive.js!#title": { | ||
"message": "Time to Interactive" | ||
}, | ||
"lighthouse-core/audits/metrics/interactive.js!#description": { | ||
"message": "Interactive marks the time at which the page is fully interactive. [Learn more](https://developers.google.com/web/tools/lighthouse/audits/consistently-interactive)." | ||
}, | ||
"lighthouse-core/lib/i18n.js!#ms": { | ||
"message": "{timeInMs, number, milliseconds} ms" | ||
}, | ||
"lighthouse-core/lib/i18n.js!#columnURL": { | ||
"message": "URL" | ||
}, | ||
"lighthouse-core/lib/i18n.js!#columnSize": { | ||
"message": "Size (KB)" | ||
}, | ||
"lighthouse-core/lib/i18n.js!#columnWastedTime": { | ||
"message": "Potential Savings (ms)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"lighthouse-core/audits/byte-efficiency/render-blocking-resources.js!#title": { | ||
"message": "Êĺîḿîńât́ê ŕêńd̂ér̂-b́l̂óĉḱîńĝ ŕêśôúr̂ćêś" | ||
}, | ||
"lighthouse-core/audits/byte-efficiency/render-blocking-resources.js!#description": { | ||
"message": "R̂éŝóûŕĉéŝ ár̂é b̂ĺôćk̂ín̂ǵ t̂h́ê f́îŕŝt́ p̂áîńt̂ óf̂ ýôúr̂ ṕâǵê. Ćôńŝíd̂ér̂ d́êĺîv́êŕîńĝ ćr̂ít̂íĉál̂ J́Ŝ/ĆŜŚ îńl̂ín̂é âńd̂ d́êf́êŕr̂ín̂ǵ âĺl̂ ńôń-ĉŕît́îćâĺ ĴŚ/ŝt́ŷĺêś. [L̂éâŕn̂ ḿôŕê](h́t̂t́p̂ś://d̂év̂él̂óp̂ér̂ś.ĝóôǵl̂é.ĉóm̂/ẃêb́/t̂óôĺŝ/ĺîǵĥt́ĥóûśê/áûd́ît́ŝ/b́l̂óĉḱîńĝ-ŕêśôúr̂ćêś)." | ||
}, | ||
"lighthouse-core/audits/byte-efficiency/render-blocking-resources.js!#displayValue": { | ||
"message": "{itemCount, plural,\n one {1 resource}\n other {# resources}\n } d̂él̂áŷéd̂ f́îŕŝt́ p̂áîńt̂ b́ŷ {timeInMs, number, milliseconds} ḿŝ" | ||
}, | ||
"lighthouse-core/audits/metrics/interactive.js!#title": { | ||
"message": "T̂ím̂é t̂ó Îńt̂ér̂áĉt́îv́ê" | ||
}, | ||
"lighthouse-core/audits/metrics/interactive.js!#description": { | ||
"message": "Îńt̂ér̂áĉt́îv́ê ḿâŕk̂ś t̂h́ê t́îḿê át̂ ẃĥíĉh́ t̂h́ê ṕâǵê íŝ f́ûĺl̂ý îńt̂ér̂áĉt́îv́ê. [Ĺêár̂ń m̂ór̂é](ĥt́t̂ṕŝ://d́êv́êĺôṕêŕŝ.ǵôóĝĺê.ćôḿ/ŵéb̂/t́ôól̂ś/l̂íĝh́t̂h́ôúŝé/âúd̂ít̂ś/ĉón̂śîśt̂én̂t́l̂ý-îńt̂ér̂áĉt́îv́ê)." | ||
}, | ||
"lighthouse-core/lib/i18n.js!#ms": { | ||
"message": "{timeInMs, number, milliseconds} m̂ś" | ||
}, | ||
"lighthouse-core/lib/i18n.js!#columnURL": { | ||
"message": "ÛŔL̂" | ||
}, | ||
"lighthouse-core/lib/i18n.js!#columnSize": { | ||
"message": "Ŝíẑé (K̂B́)" | ||
}, | ||
"lighthouse-core/lib/i18n.js!#columnWastedTime": { | ||
"message": "P̂ót̂én̂t́îál̂ Śâv́îńĝś (m̂ś)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @license Copyright 2018 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
// @ts-nocheck | ||
'use strict'; | ||
|
||
module.exports = { | ||
'en-XA': require('./en-XA.json'), | ||
}; |
Oops, something went wrong.