generate_l10n_map.js 893 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. This code is included by both the server and frontend via
  3. common/locales.js
  4. When included from the server the export will be the function.
  5. When included from the frontend (via webpack) the export will
  6. be an object mapping ftl files to js files. Example:
  7. "public/locales/en-US/send.ftl":"public/locales/en-US/send.6b4f8354.js"
  8. */
  9. const fs = require('fs');
  10. const path = require('path');
  11. function kv(d) {
  12. return `"${d}": require('../public/locales/${d}/send.ftl')`;
  13. }
  14. module.exports = function() {
  15. const dirs = fs.readdirSync(path.join(__dirname, '..', 'public', 'locales'));
  16. const code = `
  17. module.exports = {
  18. translate: function (id, data) { return window.translate(id, data) },
  19. ${dirs.map(kv).join(',\n')}
  20. };`;
  21. return {
  22. code,
  23. dependencies: dirs.map(d =>
  24. require.resolve(`../public/locales/${d}/send.ftl`)
  25. ),
  26. cacheable: true
  27. };
  28. };