Skip to content
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): import psuedo-locale json from TC #5726

Merged
merged 6 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ function getFlags(manualArgv) {
'list-trace-categories', 'view', 'verbose', 'quiet', 'help',
])
.choices('locale', [
// Later, this could be Object.keys(require('../lighthouse-core/lib/locales/index.js'))
// But we'll curate the list for now..

'en-US', // English
'en-XA', // Accented English, good for testing
'ar-XB', // RTL English, good for testing
])
.choices('output', printer.getValidOutputOptions())
.choices('throttling-method', ['devtools', 'provided', 'simulate'])
Expand Down
317 changes: 317 additions & 0 deletions lighthouse-core/lib/locales/ar-XB.json

Large diffs are not rendered by default.

353 changes: 175 additions & 178 deletions lighthouse-core/lib/locales/en-XA.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lighthouse-core/lib/locales/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
module.exports = {
'en-US': require('./en-US.json'),
'en-XA': require('./en-XA.json'),
'ar-XB': require('./ar-XB.json'),
};
34 changes: 0 additions & 34 deletions lighthouse-core/scripts/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,6 @@ function collectAllStringsInDir(dir, strings = {}) {
return strings;
}

/**
* @param {Record<string, ICUMessageDefn>} strings
*/
function createPsuedoLocaleStrings(strings) {
const psuedoLocalizedStrings = {};
for (const [key, defn] of Object.entries(strings)) {
const message = defn.message;
const psuedoLocalizedString = [];
let braceCount = 0;
let useHatForAccentMark = true;
for (let i = 0; i < message.length; i++) {
const char = message.substr(i, 1);
psuedoLocalizedString.push(char);
// Don't touch the characters inside braces
if (char === '{') {
braceCount++;
} else if (char === '}') {
braceCount--;
} else if (braceCount === 0) {
if (/[a-z]/i.test(char)) {
psuedoLocalizedString.push(useHatForAccentMark ? `\u0302` : `\u0301`);
useHatForAccentMark = !useHatForAccentMark;
}
}
}

psuedoLocalizedStrings[key] = {message: psuedoLocalizedString.join('')};
}

return psuedoLocalizedStrings;
}

/**
* @param {LH.Locale} locale
* @param {Record<string, ICUMessageDefn>} strings
Expand All @@ -148,9 +116,7 @@ function writeStringsToLocaleFormat(locale, strings) {
}

const strings = collectAllStringsInDir(path.join(LH_ROOT, 'lighthouse-core'));
const psuedoLocalizedStrings = createPsuedoLocaleStrings(strings);
console.log('Collected!');

writeStringsToLocaleFormat('en-US', strings);
writeStringsToLocaleFormat('en-XA', psuedoLocalizedStrings);
console.log('Written to disk!');
3 changes: 2 additions & 1 deletion lighthouse-core/test/lib/i18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe('i18n', () => {
describe('#getRendererFormattedStrings', () => {
it('returns icu messages in the specified locale', () => {
const strings = i18n.getRendererFormattedStrings('en-XA');
expect(strings.passedAuditsGroupTitle).toEqual('P̂áŝśêd́ âúd̂ít̂ś');
expect(strings.passedAuditsGroupTitle).toEqual('[Þåššéð åûðîţš one two]');
expect(strings.scorescaleLabel).toEqual('[Šçöŕé šçåļé: one two]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably convert this file back to assert at some point rather than having a different assertion library just for this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nooooooooooooo 😢 assert is so sad compared to expect

});
});
});
2 changes: 1 addition & 1 deletion typings/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ declare global {
cpuSlowdownMultiplier?: number
}

export type Locale = 'en-US'|'en-XA';
export type Locale = 'en-US'|'en-XA'|'ar-XB';

export type OutputMode = 'json' | 'html' | 'csv';

Expand Down