summaryrefslogtreecommitdiffstats
path: root/browser/components/storybook/.storybook/fluent-utils.mjs
blob: f38e99770b4b06db7b5551783a3f8f96995664f8 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 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 { DOMLocalization } from "@fluent/dom";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { addons } from "@storybook/addons";
import { PSEUDO_STRATEGY_TRANSFORMS } from "./l10n-pseudo.mjs";
import {
  FLUENT_SET_STRINGS,
  UPDATE_STRATEGY_EVENT,
  STRATEGY_DEFAULT,
  PSEUDO_STRATEGIES,
} from "./addon-pseudo-localization/constants.mjs";

let loadedResources = new Map();
let currentStrategy;
let storybookBundle = new FluentBundle("en-US", {
  transform(str) {
    if (currentStrategy in PSEUDO_STRATEGY_TRANSFORMS) {
      return PSEUDO_STRATEGY_TRANSFORMS[currentStrategy](str);
    }
    return str;
  },
});

// Listen for update events from addon-pseudo-localization.
const channel = addons.getChannel();
channel.on(UPDATE_STRATEGY_EVENT, updatePseudoStrategy);
channel.on(FLUENT_SET_STRINGS, ftlContents => {
  let resource = new FluentResource(ftlContents);
  for (let message of resource.body) {
    let existingMessage = storybookBundle.getMessage(message.id);
    existingMessage.value = message.value;
    existingMessage.attributes = message.attributes;
  }
  document.l10n.translateRoots();
});

/**
 * Updates "currentStrategy" when the selected pseudo localization strategy
 * changes, which in turn changes the transform used by the Fluent bundle.
 *
 * @param {string} strategy
 *  Pseudo localization strategy. Can be "default", "accented", or "bidi".
 */
function updatePseudoStrategy(strategy = STRATEGY_DEFAULT) {
  if (strategy !== currentStrategy && PSEUDO_STRATEGIES.includes(strategy)) {
    currentStrategy = strategy;
    document.l10n.translateRoots();
  }
}

export function connectFluent() {
  document.l10n = new DOMLocalization([], generateBundles);
  document.l10n.connectRoot(document.documentElement);
  document.l10n.translateRoots();
}

function* generateBundles() {
  yield* [storybookBundle];
}

export async function insertFTLIfNeeded(fileName) {
  if (loadedResources.has(fileName)) {
    return;
  }

  // This should be browser, locales-preview or toolkit.
  let [root, ...rest] = fileName.split("/");
  let ftlContents;

  // TODO(mstriemer): These seem like they could be combined but I don't want
  // to fight with webpack anymore.
  if (root == "toolkit") {
    // eslint-disable-next-line no-unsanitized/method
    let imported = await import(
      /* webpackInclude: /.*[\/\\].*\.ftl$/ */
      `toolkit/locales/en-US/${fileName}`
    );
    ftlContents = imported.default;
  } else if (root == "browser") {
    // eslint-disable-next-line no-unsanitized/method
    let imported = await import(
      /* webpackInclude: /.*[\/\\].*\.ftl$/ */
      `browser/locales/en-US/${fileName}`
    );
    ftlContents = imported.default;
  } else if (root == "locales-preview") {
    // eslint-disable-next-line no-unsanitized/method
    let imported = await import(
      /* webpackInclude: /\.ftl$/ */
      `browser/locales-preview/${rest}`
    );
    ftlContents = imported.default;
  } else if (root == "branding") {
    // eslint-disable-next-line no-unsanitized/method
    let imported = await import(
      /* webpackInclude: /\.ftl$/ */
      `browser/branding/nightly/locales/en-US/${rest}`
    );
    ftlContents = imported.default;
  }

  if (loadedResources.has(fileName)) {
    // Seems possible we've attempted to load this twice before the first call
    // resolves, so once the first load is complete we can abandon the others.
    return;
  }

  provideFluent(ftlContents, fileName);
}

export function provideFluent(ftlContents, fileName) {
  let ftlResource = new FluentResource(ftlContents);
  storybookBundle.addResource(ftlResource);
  if (fileName) {
    loadedResources.set(fileName, ftlResource);
  }
  document.l10n.translateRoots();
  return ftlResource;
}