You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

  1. /*eslint no-console: "off"*/
  2. const manageTranslations = require('react-intl-translations-manager').default;
  3. const argv = require('minimist')(process.argv.slice(2));
  4. const fs = require('fs');
  5. const translationsDirectory = 'app/javascript/mastodon/locales';
  6. const localeFn = /^([a-z]{2,3}(|\-[A-Z]+))\.json$/;
  7. const reRFC5646 = /^[a-z]{2,3}(|\-[A-Z]+)$/;
  8. const availableLanguages = fs.readdirSync(`${process.cwd()}/${translationsDirectory}`).reduce((acc, fn) => {
  9. if (fn.match(localeFn)) {
  10. acc.push(fn.replace(localeFn, '$1'));
  11. }
  12. return acc;
  13. }, []);
  14. // print help message
  15. if (argv.help !== undefined) {
  16. console.log(
  17. `Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
  18. Manage javascript translation files in mastodon. Generates and update
  19. translations in translationsDirectory: ${translationsDirectory}
  20. OPTIONS
  21. --help show this message
  22. --force force using the provided languages. create files if not exists.
  23. default: false
  24. LANGUAGES
  25. The RFC5646 language tag for the language you want to test or fix. If you want
  26. to input multiple languages, separate them with space.
  27. Available languages:
  28. ${availableLanguages}
  29. `);
  30. process.exit(0);
  31. }
  32. // determine the languages list
  33. const languages = (argv._.length === 0) ? availableLanguages : argv._;
  34. // check if the languages provided are RFC5626 compliant
  35. (function() {
  36. let invalidLanguages = languages.reduce((acc, language) => {
  37. if (!language.match(reRFC5646)) {
  38. acc.push(language);
  39. }
  40. return acc;
  41. }, []);
  42. if (invalidLanguages.length > 0) {
  43. console.log(`Error:`);
  44. for (let language of invalidLanguages) {
  45. console.error(`* Not RFC5626 name: ${language}`);
  46. }
  47. console.log(`\nUse yarn "manage:translations -- --help" for usage information\n`);
  48. process.exit(1);
  49. }
  50. })();
  51. // make sure the language exists. Unless force to create locale file.
  52. if (argv.force !== true) {
  53. let invalidLanguages = languages.reduce((acc, language) => {
  54. if (availableLanguages.indexOf(language) < 0) {
  55. acc.push(language);
  56. }
  57. return acc;
  58. }, []);
  59. if (invalidLanguages.length > 0) {
  60. console.log(`Error:`);
  61. for (let language of invalidLanguages) {
  62. console.error(`* Language not available: ${language}`);
  63. }
  64. console.log(`\nIf you want to force creating the language(s) above, please add the --force option.\n`);
  65. process.exit(1);
  66. }
  67. }
  68. manageTranslations({
  69. messagesDirectory: 'build/messages',
  70. translationsDirectory,
  71. detectDuplicateIds: false,
  72. singleMessagesFile: true,
  73. languages,
  74. });