Skip to content

Commit de35e58

Browse files
committed
fluent-react: Support HTML entities in translations
Fix #183.
1 parent e81a0cd commit de35e58

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

fluent-react/src/localized.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { isReactLocalization } from "./localization";
55
import { parseMarkup } from "./markup";
66
import VOID_ELEMENTS from "../vendor/voidElementTags";
77

8+
// Match the opening angle bracket (<) in HTML tags, and HTML entities like
9+
// &amp;, &#0038;, &#x0026;.
10+
const reMarkup = /<|&#?\w+;/;
11+
812
/*
913
* Prepare props passed to `Localized` for formatting.
1014
*/
@@ -127,9 +131,9 @@ export default class Localized extends Component {
127131
return cloneElement(elem, localizedProps);
128132
}
129133

130-
// If the message value doesn't contain any markup, insert it as the only
131-
// child of the wrapped component.
132-
if (!messageValue.includes("<")) {
134+
// If the message value doesn't contain any markup nor any HTML entities,
135+
// insert it as the only child of the wrapped component.
136+
if (!reMarkup.test(messageValue)) {
133137
return cloneElement(elem, localizedProps, messageValue);
134138
}
135139

fluent-react/test/localized_overlay_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,50 @@ true = 0 < 3 is true.
2929
));
3030
});
3131

32+
test('& in text', function() {
33+
const mcx = new MessageContext();
34+
const l10n = new ReactLocalization([mcx]);
35+
36+
mcx.addMessages(`
37+
megaman = Jumping & Shooting
38+
`)
39+
40+
const wrapper = shallow(
41+
<Localized id="megaman">
42+
<div />
43+
</Localized>,
44+
{ context: { l10n } }
45+
);
46+
47+
assert.ok(wrapper.contains(
48+
<div>
49+
Jumping & Shooting
50+
</div>
51+
));
52+
});
53+
54+
test('HTML entity', function() {
55+
const mcx = new MessageContext();
56+
const l10n = new ReactLocalization([mcx]);
57+
58+
mcx.addMessages(`
59+
two = First &middot; Second
60+
`)
61+
62+
const wrapper = shallow(
63+
<Localized id="two">
64+
<div />
65+
</Localized>,
66+
{ context: { l10n } }
67+
);
68+
69+
assert.ok(wrapper.contains(
70+
<div>
71+
First · Second
72+
</div>
73+
));
74+
});
75+
3276
test('one element is matched', function() {
3377
const mcx = new MessageContext();
3478
const l10n = new ReactLocalization([mcx]);

0 commit comments

Comments
 (0)