logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

translationRunner.js (2875B)


  1. const fs = require('fs');
  2. const path = require('path');
  3. const { default: manageTranslations } = require('react-intl-translations-manager');
  4. const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
  5. const rootDirectory = path.resolve(__dirname, '..', '..');
  6. const translationsDirectory = path.resolve(rootDirectory, 'app', 'javascript', 'mastodon', 'locales');
  7. const messagesDirectory = path.resolve(rootDirectory, 'build', 'messages');
  8. const availableLanguages = fs.readdirSync(translationsDirectory).reduce((languages, filename) => {
  9. const basename = path.basename(filename, '.json');
  10. if (RFC5646_REGEXP.test(basename)) {
  11. languages.push(basename);
  12. }
  13. return languages;
  14. }, []);
  15. const testRFC5646 = language => {
  16. if (!RFC5646_REGEXP.test(language)) {
  17. throw new Error('Not RFC5646 name');
  18. }
  19. };
  20. const testAvailability = language => {
  21. if (!availableLanguages.includes(language)) {
  22. throw new Error('Not an available language');
  23. }
  24. };
  25. const validateLanguages = (languages, validators) => {
  26. const invalidLanguages = languages.reduce((acc, language) => {
  27. try {
  28. validators.forEach(validator => validator(language));
  29. } catch (error) {
  30. acc.push({ language, error });
  31. }
  32. return acc;
  33. }, []);
  34. if (invalidLanguages.length > 0) {
  35. console.error(`
  36. Error: Specified invalid LANGUAGES:
  37. ${invalidLanguages.map(({ language, error }) => `* ${language}: ${error.message}`).join('\n')}
  38. Use yarn "manage:translations -- --help" for usage information
  39. `);
  40. process.exit(1);
  41. }
  42. };
  43. const { argv } = require('yargs')
  44. .usage(`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
  45. Manage JavaScript translation files in Mastodon. Generates and update translations in translationsDirectory: ${translationsDirectory}
  46. LANGUAGES
  47. The RFC5646 language tag for the language you want to test or fix. If you want to input multiple languages, separate them with space.
  48. Available languages:
  49. ${availableLanguages.join(', ')}
  50. `)
  51. .help('h', 'show this message')
  52. .alias('h', 'help')
  53. .options({
  54. f: {
  55. alias: 'force',
  56. default: false,
  57. describe: 'force using the provided languages. create files if not exists.',
  58. type: 'boolean',
  59. },
  60. });
  61. // check if message directory exists
  62. if (!fs.existsSync(messagesDirectory)) {
  63. console.error(`
  64. Error: messagesDirectory not exists
  65. (${messagesDirectory})
  66. Try to run "yarn build:development" first`);
  67. process.exit(1);
  68. }
  69. // determine the languages list
  70. const languages = (argv._.length > 0) ? argv._ : availableLanguages;
  71. // validate languages
  72. validateLanguages(languages, [
  73. testRFC5646,
  74. !argv.force && testAvailability,
  75. ].filter(Boolean));
  76. // manage translations
  77. manageTranslations({
  78. messagesDirectory,
  79. translationsDirectory,
  80. detectDuplicateIds: false,
  81. singleMessagesFile: true,
  82. languages,
  83. jsonOptions: {
  84. trailingNewline: true,
  85. },
  86. });