rich-text-strings.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {FluentBundle} from "fluent";
  2. /**
  3. * Properties that allow rich text MUST be added to this list.
  4. * key: the localization_id that should be used
  5. * value: a property or array of properties on the message.content object
  6. */
  7. const RICH_TEXT_CONFIG = {
  8. "text": ["text", "scene1_text"],
  9. "success_text": "success_text",
  10. "error_text": "error_text",
  11. "scene2_text": "scene2_text",
  12. "amo_html": "amo_html",
  13. "privacy_html": "scene2_privacy_html",
  14. "disclaimer_html": "scene2_disclaimer_html",
  15. };
  16. export const RICH_TEXT_KEYS = Object.keys(RICH_TEXT_CONFIG);
  17. /**
  18. * Generates an array of messages suitable for fluent's localization provider
  19. * including all needed strings for rich text.
  20. * @param {object} content A .content object from an ASR message (i.e. message.content)
  21. * @returns {FluentBundle[]} A array containing the fluent message context
  22. */
  23. export function generateBundles(content) {
  24. const bundle = new FluentBundle("en-US");
  25. RICH_TEXT_KEYS.forEach(key => {
  26. const attrs = RICH_TEXT_CONFIG[key];
  27. const attrsToTry = Array.isArray(attrs) ? [...attrs] : [attrs];
  28. let string = "";
  29. while (!string && attrsToTry.length) {
  30. const attr = attrsToTry.pop();
  31. string = content[attr];
  32. }
  33. bundle.addMessages(`${key} = ${string}`);
  34. });
  35. return [bundle];
  36. }