translationRunner.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*eslint no-console: "off"*/
  2. const manageTranslations = require('react-intl-translations-manager').default;
  3. const fs = require('fs');
  4. const testRFC5626 = function (reRFC5646) {
  5. return function (language) {
  6. if (!language.match(reRFC5646)) {
  7. throw new Error('Not RFC5626 name');
  8. }
  9. }
  10. }
  11. const testAvailability = function (availableLanguages) {
  12. return function (language) {
  13. if ((argv.force !== true) && availableLanguages.indexOf(language) < 0) {
  14. throw new Error('Not an available language');
  15. }
  16. }
  17. }
  18. const validateLanguages = function (languages, validators) {
  19. let invalidLanguages = languages.reduce((acc, language) => {
  20. try {
  21. for (let validator of validators) {
  22. validator(language);
  23. }
  24. } catch (error) {
  25. acc.push({
  26. language,
  27. error,
  28. });
  29. }
  30. return acc;
  31. }, []);
  32. if (invalidLanguages.length > 0) {
  33. console.log(`\nError: Specified invalid LANGUAGES:`);
  34. for (let {language, error} of invalidLanguages) {
  35. console.error(`* ${language}: ${error}`);
  36. }
  37. console.log(`\nUse yarn "manage:translations -- --help" for usage information\n`);
  38. process.exit(1);
  39. }
  40. }
  41. const printHelpMessages = function () {
  42. console.log(
  43. `Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
  44. Manage javascript translation files in mastodon. Generates and update
  45. translations in translationsDirectory: ${translationsDirectory}
  46. OPTIONS
  47. -h,--help show this message
  48. -f,--force force using the provided languages. create files if not exists.
  49. default: false
  50. LANGUAGES
  51. The RFC5646 language tag for the language you want to test or fix. If you want
  52. to input multiple languages, separate them with space.
  53. Available languages:
  54. ${availableLanguages}
  55. `);
  56. }
  57. // parse arguments
  58. const argv = require('minimist')(process.argv.slice(2), {
  59. 'boolean': [
  60. 'force',
  61. 'help'
  62. ],
  63. 'alias': {
  64. 'f': 'force',
  65. 'h': 'help',
  66. },
  67. });
  68. const translationsDirectory = 'app/javascript/mastodon/locales';
  69. const messagesDirectory = 'build/messages';
  70. const localeFn = /^([a-z]{2,3}(|\-[A-Z]+))\.json$/;
  71. const reRFC5646 = /^[a-z]{2,3}(|\-[A-Z]+)$/;
  72. const availableLanguages = fs.readdirSync(`${process.cwd()}/${translationsDirectory}`).reduce((acc, fn) => {
  73. if (fn.match(localeFn)) {
  74. acc.push(fn.replace(localeFn, '$1'));
  75. }
  76. return acc;
  77. }, []);
  78. // print help message
  79. if (argv.help) {
  80. printHelpMessages();
  81. process.exit(0);
  82. }
  83. // check if message directory exists
  84. if (!fs.existsSync(`${process.cwd()}/${messagesDirectory}`)) {
  85. console.error(`\nError: messageDirectory not exists\n(${process.cwd()}/${messagesDirectory})\n`);
  86. console.error(`Try to run "yarn build:development" first`);
  87. process.exit(1);
  88. }
  89. // determine the languages list
  90. const languages = (argv._.length === 0) ? availableLanguages : argv._;
  91. // validate languages
  92. validateLanguages(languages, [
  93. testRFC5626(reRFC5646),
  94. testAvailability(availableLanguages),
  95. ]);
  96. // manage translations
  97. manageTranslations({
  98. messagesDirectory,
  99. translationsDirectory,
  100. detectDuplicateIds: false,
  101. singleMessagesFile: true,
  102. languages,
  103. jsonOptions: {
  104. trailingNewline: true,
  105. },
  106. });