Upgrade react-intl (#24906)
This commit is contained in:
parent
00c222377d
commit
44cd88adc4
130 changed files with 413 additions and 5046 deletions
|
@ -1,51 +0,0 @@
|
|||
// To avoid adding a lot of boilerplate, locale packs are
|
||||
// automatically generated here. These are written into the tmp/
|
||||
// directory and then used to generate locale_en.js, locale_fr.js, etc.
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const { mkdirp } = require('mkdirp');
|
||||
const rimraf = require('rimraf');
|
||||
|
||||
const localesJsonPath = path.join(__dirname, '../../app/javascript/mastodon/locales');
|
||||
const locales = fs.readdirSync(localesJsonPath).filter(filename => {
|
||||
return /\.json$/.test(filename) &&
|
||||
!/defaultMessages/.test(filename) &&
|
||||
!/whitelist/.test(filename);
|
||||
}).map(filename => filename.replace(/\.json$/, ''));
|
||||
|
||||
const outPath = path.join(__dirname, '../../tmp/packs');
|
||||
|
||||
rimraf.sync(outPath);
|
||||
mkdirp.sync(outPath);
|
||||
|
||||
const outPaths = [];
|
||||
|
||||
locales.forEach(locale => {
|
||||
const localePath = path.join(outPath, `locale_${locale}.js`);
|
||||
const baseLocale = locale.split('-')[0]; // e.g. 'zh-TW' -> 'zh'
|
||||
const localeDataPath = [
|
||||
// first try react-intl
|
||||
`../../node_modules/react-intl/locale-data/${baseLocale}.js`,
|
||||
// then check locales/locale-data
|
||||
`../../app/javascript/mastodon/locales/locale-data/${baseLocale}.js`,
|
||||
// fall back to English (this is what react-intl does anyway)
|
||||
'../../node_modules/react-intl/locale-data/en.js',
|
||||
].filter(filename => fs.existsSync(path.join(outPath, filename)))
|
||||
.map(filename => filename.replace(/..\/..\/node_modules\//, ''))[0];
|
||||
|
||||
const localeContent = `//
|
||||
// locale_${locale}.js
|
||||
// automatically generated by generateLocalePacks.js
|
||||
//
|
||||
import messages from '../../app/javascript/mastodon/locales/${locale}.json';
|
||||
import localeData from ${JSON.stringify(localeDataPath)};
|
||||
import { setLocale } from '../../app/javascript/mastodon/locales';
|
||||
setLocale({messages, localeData});
|
||||
`;
|
||||
fs.writeFileSync(localePath, localeContent, 'utf8');
|
||||
outPaths.push(localePath);
|
||||
});
|
||||
|
||||
module.exports = outPaths;
|
|
@ -9,7 +9,6 @@ const webpack = require('webpack');
|
|||
const AssetsManifestPlugin = require('webpack-assets-manifest');
|
||||
|
||||
const { env, settings, themes, output } = require('./configuration');
|
||||
const localePackPaths = require('./generateLocalePacks');
|
||||
const rules = require('./rules');
|
||||
|
||||
const extensionGlob = `**/*{${settings.extensions.join(',')}}*`;
|
||||
|
@ -24,11 +23,6 @@ module.exports = {
|
|||
localMap[join(namespace, basename(entry, extname(entry)))] = resolve(entry);
|
||||
return localMap;
|
||||
}, {}),
|
||||
localePackPaths.reduce((map, entry) => {
|
||||
const localMap = map;
|
||||
localMap[basename(entry, extname(entry, extname(entry)))] = resolve(entry);
|
||||
return localMap;
|
||||
}, {}),
|
||||
Object.keys(themes).reduce((themePaths, name) => {
|
||||
themePaths[name] = resolve(join(settings.source_path, themes[name]));
|
||||
return themePaths;
|
||||
|
|
|
@ -1,101 +1,3 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
console.error("The localisation functionality has been refactored, please see the Localisation section in the development documentation (https://docs.joinmastodon.org/dev/code/#localizations)");
|
||||
|
||||
// eslint-disable-next-line import/order
|
||||
const { default: manageTranslations } = require('react-intl-translations-manager');
|
||||
|
||||
const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
|
||||
|
||||
const rootDirectory = path.resolve(__dirname, '..', '..');
|
||||
const translationsDirectory = path.resolve(rootDirectory, 'app', 'javascript', 'mastodon', 'locales');
|
||||
const messagesDirectory = path.resolve(rootDirectory, 'build', 'messages');
|
||||
const availableLanguages = fs.readdirSync(translationsDirectory).reduce((languages, filename) => {
|
||||
const basename = path.basename(filename, '.json');
|
||||
if (RFC5646_REGEXP.test(basename)) {
|
||||
languages.push(basename);
|
||||
}
|
||||
return languages;
|
||||
}, []);
|
||||
|
||||
const testRFC5646 = language => {
|
||||
if (!RFC5646_REGEXP.test(language)) {
|
||||
throw new Error('Not RFC5646 name');
|
||||
}
|
||||
};
|
||||
|
||||
const testAvailability = language => {
|
||||
if (!availableLanguages.includes(language)) {
|
||||
throw new Error('Not an available language');
|
||||
}
|
||||
};
|
||||
|
||||
const validateLanguages = (languages, validators) => {
|
||||
const invalidLanguages = languages.reduce((acc, language) => {
|
||||
try {
|
||||
validators.forEach(validator => validator(language));
|
||||
} catch (error) {
|
||||
acc.push({ language, error });
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
if (invalidLanguages.length > 0) {
|
||||
console.error(`
|
||||
Error: Specified invalid LANGUAGES:
|
||||
${invalidLanguages.map(({ language, error }) => `* ${language}: ${error.message}`).join('\n')}
|
||||
|
||||
Use yarn "manage:translations -- --help" for usage information
|
||||
`);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
const usage = `Usage: yarn manage:translations [OPTIONS] [LANGUAGES]
|
||||
|
||||
Manage JavaScript translation files in Mastodon. Generates and update translations in translationsDirectory: ${translationsDirectory}
|
||||
|
||||
LANGUAGES
|
||||
The RFC5646 language tag for the language you want to test or fix. If you want to input multiple languages, separate them with space.
|
||||
|
||||
Available languages:
|
||||
${availableLanguages.join(', ')}
|
||||
`;
|
||||
|
||||
const { argv } = require('yargs')
|
||||
.usage(usage)
|
||||
.option('f', {
|
||||
alias: 'force',
|
||||
default: false,
|
||||
describe: 'force using the provided languages. create files if not exists.',
|
||||
type: 'boolean',
|
||||
});
|
||||
|
||||
// check if message directory exists
|
||||
if (!fs.existsSync(messagesDirectory)) {
|
||||
console.error(`
|
||||
Error: messagesDirectory not exists
|
||||
(${messagesDirectory})
|
||||
Try to run "yarn build:development" first`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// determine the languages list
|
||||
const languages = (argv._.length > 0) ? argv._ : availableLanguages;
|
||||
|
||||
// validate languages
|
||||
validateLanguages(languages, [
|
||||
testRFC5646,
|
||||
!argv.force && testAvailability,
|
||||
].filter(Boolean));
|
||||
|
||||
// manage translations
|
||||
manageTranslations({
|
||||
messagesDirectory,
|
||||
translationsDirectory,
|
||||
detectDuplicateIds: false,
|
||||
singleMessagesFile: true,
|
||||
languages,
|
||||
jsonOptions: {
|
||||
trailingNewline: true,
|
||||
},
|
||||
});
|
||||
process.exit(1);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue