Use LocaleMatcher to resolve system preferred locales

This commit is contained in:
Jamie Kyle 2023-04-17 12:26:57 -07:00 committed by GitHub
parent 68ae25f5cd
commit cdc68d1c34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 224 additions and 48 deletions

View file

@ -0,0 +1,62 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import fs from 'fs/promises';
import path from 'path';
import fastGlob from 'fast-glob';
import * as LocaleMatcher from '@formatjs/intl-localematcher';
const ROOT_DIR = path.join(__dirname, '..', '..');
function matches(input: string, expected: string) {
const match = LocaleMatcher.match([input], [expected], 'en', {
algorithm: 'best fit',
});
return match === expected;
}
async function main() {
const dirEntries = await fastGlob('_locales/*', {
cwd: ROOT_DIR,
onlyDirectories: true,
});
const localeDirNames = [];
for (const dirEntry of dirEntries) {
const dirName = path.basename(dirEntry);
const locale = new Intl.Locale(dirName);
// Smartling doesn't always use the correct language tag, so this check and
// reverse check are to make sure we don't accidentally add a locale that
// doesn't match its directory name (using LocaleMatcher).
//
// If this check ever fails, we may need to update our get-strings script to
// manually rename language tags before writing them to disk.
//
// Such is the case for Smartling's "zh-YU" locale, which we renamed to
// "yue" to match the language tag used by... everyone else.
if (!matches(dirName, locale.baseName)) {
throw new Error(
`Matched locale "${dirName}" does not match its resolved name "${locale.baseName}"`
);
}
if (!matches(locale.baseName, dirName)) {
throw new Error(
`Matched locale "${dirName}" does not match its dir name "${dirName}"`
);
}
localeDirNames.push(dirName);
}
const jsonPath = path.join(ROOT_DIR, 'build', 'available-locales.json');
console.log(`Writing to "${jsonPath}"...`);
await fs.writeFile(jsonPath, `${JSON.stringify(localeDirNames, null, 2)}\n`);
}
main().catch(error => {
console.error(error);
process.exit(1);
});

View file

@ -2,6 +2,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { execSync } from 'child_process';
import fsExtra from 'fs-extra';
import path from 'path';
const { SMARTLING_USER, SMARTLING_SECRET } = process.env;
@ -29,6 +31,19 @@ execSync(
}
);
function rename(from: string, to: string) {
console.log(`Renaming "${from}" to "${to}"`);
fsExtra.moveSync(path.join('_locales', from), path.join('_locales', to), {
overwrite: true,
});
}
// Smartling uses "zh-YU" for Cantonese (or Yue Chinese).
// This is wrong.
// The language tag for Yue Chinese is "yue"
// "zh-YU" actually implies "Chinese as spoken in Yugoslavia (canonicalized to Serbia)"
rename('zh-YU', 'yue');
console.log('Formatting newly-downloaded strings!');
console.log();
execSync('yarn format', {