-
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
i18n: placeholders #9114
i18n: placeholders #9114
Changes from 7 commits
656acce
fc762a5
d1cdeab
74e11c1
981e028
6d2dd5d
f22ee1b
c66c0c6
70a5a07
a7fc83a
0de5485
f8650eb
37dc2ae
58c7ded
ad01bdd
4182889
8b0f8ea
ddf8171
705ffb6
902c2f7
4507138
29e29df
393beb6
799084c
86e1593
6160402
02e8977
f5083b8
5aca05a
ee3e235
6b8297d
77fcd92
7252f3d
d4dd3ac
996ee5c
4ff414a
588f574
2b17130
570a6cc
dcc7cc7
7743cda
ce01c75
dea280a
7f89ccf
cf42496
d156737
89410f8
ea3616b
3d40437
bc2b8f3
92cafeb
3d746d6
5ad0dff
c2e4ce5
5dd3e07
6eef366
a35a45e
13d0f02
b34ef95
48283f4
95527d5
327c7db
b006cf7
51cbb78
37be64c
d2a8326
be426b8
125cb1d
b95b96b
90efcff
6e6ef24
6a9ef28
7762381
6c19a17
be34f7e
23a68d5
3685dd6
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 |
---|---|---|
|
@@ -12,10 +12,50 @@ const computeTokenLength = require('../../lib/minification-estimator.js').comput | |
|
||
const UIStrings = { | ||
/** Imperative title of a Lighthouse audit that tells the user to minify (remove whitespace) the page's CSS code. This is displayed in a list of audit titles that Lighthouse generates. */ | ||
title: 'Minify CSS', | ||
/** Description of a Lighthouse audit that tells the user *why* they should minify (remove whitespace) the page's CSS code. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */ | ||
description: 'Minifying CSS files can reduce network payload sizes. ' + | ||
'[Learn more](https://developers.google.com/web/tools/lighthouse/audits/minify-css).', | ||
title: { | ||
message: 'Minify CSS like {css}', | ||
placeholders: { | ||
css: '`<link rel=stylesheet>`', | ||
}, | ||
}, | ||
/** (Message Description goes here) Description of a Lighthouse audit that tells the user *why* they should minify (remove whitespace) the page's CSS code. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */ | ||
description: { | ||
message: 'Minifying CSS files can reduce network payload sizes. {link_start}Learn More!!!{link_end}. This audit took {milliseconds} ms.', | ||
placeholders: { | ||
link_start: '[->', | ||
link_end: '](https://developers.google.com/web/tools/lighthouse/audits/minify-css)', | ||
/** 520 (Placeholder examples go here) */ | ||
milliseconds: '{timeInMs, number, milliseconds}', | ||
}, | ||
}, | ||
/** [ICU Syntax] Some plural warning... */ | ||
warningPlural: '{itemCount, plural, ' + | ||
'=1 {# error found}' + | ||
'other {# errors found}}', | ||
/** [ICU Syntax] Some gendered (ICU select) explanation... */ | ||
explanationGender: { | ||
message: 'Someone minified this, {direct_replace_name}. {static_replacement} {person, select, ' + | ||
'female {She minified this CSS.} ' + | ||
'male {He minified this CSS.} ' + | ||
'other {They minified this CSS.}}', | ||
placeholders: { | ||
/** Some static replacement. */ | ||
static_replacement: '`<link rel=>`', | ||
}, | ||
}, | ||
/** [ICU Syntax] Some gendered (ICU select) explanation... */ | ||
explanationGender2: { | ||
message: 'Someone minified this, {name}. {static_replacement} {person, select, ' + | ||
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 example uses placeholders to allow us to make more perfectly formed translation strings, but stutters with |
||
'female {She minified this CSS.} ' + | ||
'male {He minified this CSS.} ' + | ||
'other {They minified this CSS.}}', | ||
placeholders: { | ||
/** Some static replacement. */ | ||
static_replacement: '`<link rel=>`', | ||
/** This stutters, BUT we have the opportunity to tell translators an example, and give context? Example text: Karen. */ | ||
name: '{name}', | ||
}, | ||
}, | ||
}; | ||
|
||
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); | ||
|
@@ -34,7 +74,7 @@ class UnminifiedCSS extends ByteEfficiencyAudit { | |
return { | ||
id: 'unminified-css', | ||
title: str_(UIStrings.title), | ||
description: str_(UIStrings.description), | ||
description: str_(UIStrings.description, {timeInMs: 10}), | ||
scoreDisplayMode: ByteEfficiencyAudit.SCORING_MODES.NUMERIC, | ||
requiredArtifacts: ['CSSUsage', 'devtoolsLogs', 'traces', 'URL'], | ||
}; | ||
|
@@ -109,7 +149,7 @@ class UnminifiedCSS extends ByteEfficiencyAudit { | |
{key: 'wastedBytes', valueType: 'bytes', label: str_(i18n.UIStrings.columnWastedBytes)}, | ||
]; | ||
|
||
return {items, headings}; | ||
return {items, headings, explanation: str_(UIStrings.explanationGender, {person: 'female', direct_replace_name: 'Kim'}), warnings: [str_(UIStrings.warningPlural, {itemCount: 2})]}; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,7 +121,7 @@ function lookupLocale(locale) { | |
*/ | ||
function _preprocessMessageValues(icuMessage, values) { | ||
if (!values) return; | ||
|
||
// console.log(icuMessage, values); | ||
const clonedValues = JSON.parse(JSON.stringify(values)); | ||
const parsed = MessageParser.parse(icuMessage); | ||
// Throw an error if a message's value isn't provided | ||
|
@@ -174,7 +174,23 @@ const _icuMessageInstanceMap = new Map(); | |
*/ | ||
function _formatIcuMessage(locale, icuMessageId, icuMessage, values) { | ||
const localeMessages = LOCALES[locale]; | ||
const localeMessage = localeMessages[icuMessageId] && localeMessages[icuMessageId].message; | ||
let localeMessage = localeMessages[icuMessageId] && localeMessages[icuMessageId].message; | ||
// lets try to replace some things :o | ||
const placeholders = localeMessages[icuMessageId] && localeMessages[icuMessageId].placeholders; | ||
if (placeholders) { | ||
// do some regex | ||
Object.entries(placeholders).forEach(entry => { | ||
const key = entry[0]; | ||
const value = entry[1]; | ||
//use key and value here | ||
let regexStr = `\$${key}\$`; | ||
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 think we'll definitely want a |
||
// console.log(key, value, regexStr); | ||
localeMessage = localeMessage.replace(regexStr, value.content); | ||
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. maybe to avoid the issue with positional replacements ( |
||
// console.log(localeMessage); | ||
}); | ||
icuMessage = localeMessage; | ||
} | ||
|
||
// 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 = localeMessage || icuMessage; | ||
|
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.
This contains a 1. direct replacement in
{ICU string replace}
astatic_replacement
that is covered by a placeholder, and "Google valid"ICU select
syntax.