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.

66 lines
2.2 KiB

  1. // A message from upstream:
  2. // ========================
  3. // To avoid adding a lot of boilerplate, locale packs are
  4. // automatically generated here. These are written into the tmp/
  5. // directory and then used to generate locale_en.js, locale_fr.js, etc.
  6. // Glitch note:
  7. // ============
  8. // This code has been entirely rewritten to support glitch flavours.
  9. // However, the underlying process is exactly the same.
  10. const { existsSync, readdirSync, writeFileSync } = require('fs');
  11. const { join, resolve } = require('path');
  12. const rimraf = require('rimraf');
  13. const mkdirp = require('mkdirp');
  14. const { flavours } = require('./configuration.js');
  15. module.exports = Object.keys(flavours).reduce(function (map, entry) {
  16. const flavour = flavours[entry];
  17. if (!flavour.locales) {
  18. return map;
  19. }
  20. const locales = readdirSync(flavour.locales).filter(
  21. filename => /\.js(?:on)?$/.test(filename) && !/defaultMessages|whitelist|index/.test(filename)
  22. );
  23. const outPath = resolve('tmp', 'locales', entry);
  24. rimraf.sync(outPath);
  25. mkdirp.sync(outPath);
  26. locales.forEach(function (locale) {
  27. const localeName = locale.replace(/\.js(?:on)?$/, '');
  28. const localePath = join(outPath, `${localeName}.js`);
  29. const baseLocale = localeName.split('-')[0]; // e.g. 'zh-TW' -> 'zh'
  30. const localeDataPath = [
  31. // first try react-intl
  32. `node_modules/react-intl/locale-data/${baseLocale}.js`,
  33. // then check locales/locale-data
  34. `app/javascript/locales/locale-data/${baseLocale}.js`,
  35. // fall back to English (this is what react-intl does anyway)
  36. 'node_modules/react-intl/locale-data/en.js',
  37. ].filter(
  38. filename => existsSync(filename)
  39. ).map(
  40. filename => filename.replace(/(?:node_modules|app\/javascript)\//, '')
  41. )[0];
  42. const localeContent = `//
  43. // locales/${entry}/${localeName}.js
  44. // automatically generated by generateLocalePacks.js
  45. //
  46. import messages from '../../../${flavour.locales}/${locale.replace(/\.js$/, '')}';
  47. import localeData from '${localeDataPath}';
  48. import { setLocale } from 'locales';
  49. setLocale({
  50. localeData,
  51. messages,
  52. });
  53. `;
  54. writeFileSync(localePath, localeContent, 'utf8');
  55. map[`locales/${entry}/${localeName}`] = localePath;
  56. });
  57. return map;
  58. }, {});