summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/content-src/asrouter/rich-text-strings.js
blob: 6a52732ad1ff6502a50f97350d07c786a46927a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

import { FluentBundle } from "fluent";

/**
 * Properties that allow rich text MUST be added to this list.
 *   key: the localization_id that should be used
 *   value: a property or array of properties on the message.content object
 */
const RICH_TEXT_CONFIG = {
  text: ["text", "scene1_text"],
  success_text: "success_text",
  error_text: "error_text",
  scene2_text: "scene2_text",
  amo_html: "amo_html",
  privacy_html: "scene2_privacy_html",
  disclaimer_html: "scene2_disclaimer_html",
};

export const RICH_TEXT_KEYS = Object.keys(RICH_TEXT_CONFIG);

/**
 * Generates an array of messages suitable for fluent's localization provider
 * including all needed strings for rich text.
 * @param {object} content A .content object from an ASR message (i.e. message.content)
 * @returns {FluentBundle[]} A array containing the fluent message context
 */
export function generateBundles(content) {
  const bundle = new FluentBundle("en-US");

  RICH_TEXT_KEYS.forEach(key => {
    const attrs = RICH_TEXT_CONFIG[key];
    const attrsToTry = Array.isArray(attrs) ? [...attrs] : [attrs];
    let string = "";
    while (!string && attrsToTry.length) {
      const attr = attrsToTry.pop();
      string = content[attr];
    }
    bundle.addMessages(`${key} = ${string}`);
  });
  return [bundle];
}