-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfluent.js
172 lines (154 loc) · 4.31 KB
/
fluent.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { FluentBundle, FluentResource } from "@fluent/bundle";
import insane from "insane";
import USStrings from "./l10n/en-us.flt";
import DEStrings from "./l10n/de.flt";
const languages = [["en-US", "English"]];
const whitelistedTags = ["i", "strong", "br"];
const whitelistedAttributes = ["title", "aria-label"];
export class Fluent {
/**
* @param {string} locale
* @param {string[]} resources
*/
constructor(locale = "en-US", resources = []) {
this.usBundle = Fluent.constructBundle(new FluentBundle(locale), [
USStrings,
]);
if (resources.length > 0) {
this.bundle = Fluent.constructBundle(new FluentBundle(locale), [
USStrings,
...resources,
]);
}
}
/**
* @param {FluentBundle} bundle
* @param {string[]} resources
*/
static constructBundle(bundle, resources = []) {
resources.forEach((r) => {
const errors = bundle.addResource(new FluentResource(r), {
allowOverrides: true,
});
if (errors.length) {
console.error(errors);
}
});
return bundle;
}
/**
* @param {readonly string[]} requested
* @param {string[]} available
*/
static init(requested = navigator.languages, available) {
this.locales = this.resolveLocale(requested, available);
return this.load(...this.locales);
}
static languages() {
return languages;
}
/**
* @returns {string}
*/
get(...parameters) {
if (typeof parameters[0] === "string") {
const id = parameters[0];
switch (parameters.length) {
case 2:
if (typeof parameters[1] === "string") {
return this.get({ id, attr: parameters[1] });
}
return this.get({ id, args: parameters[1] });
default:
return this.get({ id });
}
}
const { id, attr, args, tags } = parameters[0];
const message = this.getMessage(id, attr, args);
return Fluent.sanitize(message, tags);
}
/**
* @param {string} message
*/
static sanitize(message, tags = {}) {
const allowedAttributes = {};
Object.values(tags).forEach((t) => {
allowedAttributes[t.tag] = Object.keys(t)
.filter((x) => x !== "tag")
.concat(whitelistedAttributes);
});
return insane(
message,
{
allowedAttributes,
allowedTags: Object.values(tags)
.map((t) => t.tag)
.concat(whitelistedTags),
allowedSchemes: ["http", "https", "mailto"],
filter(token) {
const name = token.attrs["data-l10n-name"];
if (name) {
Object.entries(tags[name]).forEach(([k, v]) => {
token.attrs[k] = v;
});
}
if (
whitelistedTags.includes(token.tag) ||
(Object.keys(tags).includes(name) && tags[name].tag === token.tag)
) {
return true;
}
return false;
},
},
true,
);
}
/**
* @param {string} id
* @returns {string}
*/
getMessage(id, attr = null, args = {}, bundle = this.bundle, us = false) {
const parentMessage = bundle ? bundle.getMessage(id) : undefined;
let message;
if (Fluent.locales && Fluent.locales[1] === "__ids") {
return `[${id}${attr ? `.${attr}` : ""}]`;
}
if (!parentMessage) {
if (us) {
console.error(`string ${id} doesn't exist`);
return `[${id}${attr ? `.${attr}` : ""}]`;
}
return this.getMessage(id, attr, args, this.usBundle, true);
}
if (attr) {
message = parentMessage.attributes[attr];
if (!message) {
if (us) {
console.error(`string ${id} with ${attr} attribute doesn't exist`);
return `[${id}.${attr}]`;
}
return this.getMessage(id, attr, args, this.usBundle, true);
}
} else if (parentMessage.value) {
message = parentMessage.value;
}
/** @type {Error[]} */
const errors = [];
const formatted = bundle.formatPattern(message, args, errors);
if (errors.length) {
console.error(errors);
}
return formatted;
}
}
/** @type {Map<string, Fluent>} */
let fluent = new Map();
async function l10n(locale = "en-US") {
if (!fluent.has(locale)) {
const localeF = new Fluent(locale, [DEStrings]);
fluent.set(locale, localeF);
}
return fluent.get(locale);
}
export default l10n;