emailLocalization.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // emailLocalization.js
  2. // Utility functions to handle email localization in Wekan
  3. import { TAPi18n } from '/imports/i18n';
  4. import { ReactiveCache } from '/imports/reactiveCache';
  5. // Main object for email localization utilities
  6. EmailLocalization = {
  7. /**
  8. * Send an email using the recipient's preferred language
  9. * @param {Object} options - Standard email sending options plus language options
  10. * @param {String} options.to - Recipient email address
  11. * @param {String} options.from - Sender email address
  12. * @param {String} options.subject - Email subject i18n key
  13. * @param {String} options.text - Email text i18n key
  14. * @param {Object} options.params - Parameters for i18n translation
  15. * @param {String} options.language - Language code to use (if not provided, will try to detect)
  16. * @param {String} options.userId - User ID to determine language (if not provided with language)
  17. */
  18. sendEmail(options) {
  19. // Determine the language to use
  20. let lang = options.language;
  21. // If no language is specified but we have a userId, try to get the user's language
  22. if (!lang && options.userId) {
  23. const user = ReactiveCache.getUser(options.userId);
  24. if (user) {
  25. lang = user.getLanguage();
  26. }
  27. }
  28. // If no language could be determined, use the site default
  29. if (!lang) {
  30. lang = TAPi18n.getLanguage() || 'en';
  31. }
  32. // Translate subject and text using the determined language
  33. const subject = TAPi18n.__(options.subject, options.params || {}, lang);
  34. let text = options.text;
  35. // If text is an i18n key, translate it
  36. if (typeof text === 'string' && text.startsWith('email-')) {
  37. text = TAPi18n.__(text, options.params || {}, lang);
  38. }
  39. // Send the email with translated content
  40. return Email.send({
  41. to: options.to,
  42. from: options.from || Accounts.emailTemplates.from,
  43. subject: subject,
  44. text: text,
  45. html: options.html
  46. });
  47. }
  48. };
  49. // Add module.exports to make it accessible from other files
  50. module.exports = EmailLocalization;