Skip to content

Commit 2f10bde

Browse files
authored
Add transformation options to MessageContext (#213)
1 parent 75f57aa commit 2f10bde

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

fluent/src/context.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,24 @@ export class MessageContext {
4343
* - `useIsolating` - boolean specifying whether to use Unicode isolation
4444
* marks (FSI, PDI) for bidi interpolations.
4545
*
46+
* - `transform` - a function used to transform string parts of patterns.
47+
*
4648
* @param {string|Array<string>} locales - Locale or locales of the context
4749
* @param {Object} [options]
4850
* @returns {MessageContext}
4951
*/
50-
constructor(locales, { functions = {}, useIsolating = true } = {}) {
52+
constructor(locales, {
53+
functions = {},
54+
useIsolating = true,
55+
transform = v => v
56+
} = {}) {
5157
this.locales = Array.isArray(locales) ? locales : [locales];
5258

5359
this._terms = new Map();
5460
this._messages = new Map();
5561
this._functions = functions;
5662
this._useIsolating = useIsolating;
63+
this._transform = transform;
5764
this._intls = new WeakMap();
5865
}
5966

@@ -163,12 +170,12 @@ export class MessageContext {
163170
format(message, args, errors) {
164171
// optimize entities which are simple strings with no attributes
165172
if (typeof message === "string") {
166-
return message;
173+
return this._transform(message);
167174
}
168175

169176
// optimize simple-string entities with attributes
170177
if (typeof message.val === "string") {
171-
return message.val;
178+
return this._transform(message.val);
172179
}
173180

174181
// optimize entities with null values

fluent/src/resolver.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ function SelectExpression(env, {exp, vars, def}) {
257257
function Type(env, expr) {
258258
// A fast-path for strings which are the most common case, and for
259259
// `FluentNone` which doesn't require any additional logic.
260-
if (typeof expr === "string" || expr instanceof FluentNone) {
260+
if (typeof expr === "string") {
261+
return env.ctx._transform(expr);
262+
}
263+
if (expr instanceof FluentNone) {
261264
return expr;
262265
}
263266

@@ -454,7 +457,7 @@ function Pattern(env, ptn) {
454457

455458
for (const elem of ptn) {
456459
if (typeof elem === "string") {
457-
result.push(elem);
460+
result.push(ctx._transform(elem));
458461
continue;
459462
}
460463

fluent/test/transform_test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
import assert from 'assert';
4+
5+
import { MessageContext } from '../src/context';
6+
import { ftl } from '../src/util';
7+
8+
suite('Transformations', function(){
9+
let ctx, errs;
10+
11+
suiteSetup(function() {
12+
ctx = new MessageContext('en-US', {
13+
transform: v => v.replace(/a/g, "A")
14+
});
15+
ctx.addMessages(ftl`
16+
foo = Faa
17+
.bar = Bar { $foo } Baz
18+
`);
19+
});
20+
21+
setup(function() {
22+
errs = [];
23+
});
24+
25+
test('transforms strings', function(){
26+
const msg = ctx.getMessage('foo');
27+
const val = ctx.format(msg, {}, errs);
28+
const attr = ctx.format(msg.attrs["bar"], {foo: "arg"}, errs);
29+
assert(val.includes("FAA"));
30+
assert(attr.includes("BAr"));
31+
assert(attr.includes("BAz"));
32+
assert.equal(errs.length, 0);
33+
});
34+
});

0 commit comments

Comments
 (0)