Skip to content

Commit 5ebc8e2

Browse files
hkasemirstasm
authored andcommitted
add(getString): add third argument to getString for fallback message (#147)
1 parent b27cab7 commit 5ebc8e2

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

fluent-react/src/localization.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ export default class ReactLocalization {
6565
/*
6666
* Find a translation by `id` and format it to a string using `args`.
6767
*/
68-
getString(id, args) {
68+
getString(id, args, fallback) {
6969
const mcx = this.getMessageContext(id);
7070

7171
if (mcx === null) {
72-
return id;
72+
return fallback || id;
7373
}
7474

7575
const msg = mcx.getMessage(id);

fluent-react/src/with_localization.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ export default function withLocalization(Inner) {
1212
/*
1313
* Find a translation by `id` and format it to a string using `args`.
1414
*/
15-
getString(id, args) {
15+
getString(id, args, fallback) {
1616
const { l10n } = this.context;
1717

1818
if (!l10n) {
19-
return id;
19+
return fallback || id;
2020
}
2121

22-
return l10n.getString(id, args);
22+
return l10n.getString(id, args, fallback);
2323
}
2424

2525
render() {

fluent-react/test/with_localization_test.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,26 @@ foo = FOO
4646

4747
const getString = wrapper.prop('getString');
4848
// Returns the translation.
49-
assert.equal(getString('foo'), 'FOO');
49+
assert.equal(getString('foo', {}), 'FOO');
50+
});
51+
52+
test('getString with access to the l10n context, with fallback value', function() {
53+
const mcx = new MessageContext();
54+
const l10n = new ReactLocalization([mcx]);
55+
const EnhancedComponent = withLocalization(DummyComponent);
56+
57+
mcx.addMessages(`
58+
foo = FOO
59+
`);
60+
61+
const wrapper = shallow(
62+
<EnhancedComponent />,
63+
{ context: { l10n } }
64+
);
65+
66+
const getString = wrapper.prop('getString');
67+
// Returns the translation, even if fallback value provided.
68+
assert.equal(getString('bar', {}, 'fallback'), 'fallback');
5069
});
5170

5271
test('getString without access to the l10n context', function() {
@@ -63,7 +82,25 @@ foo = FOO
6382
);
6483

6584
const getString = wrapper.prop('getString');
66-
// Returns the id.
85+
// Returns the id if no fallback.
6786
assert.equal(getString('foo', {arg: 1}), 'foo');
6887
});
88+
89+
test('getString without access to the l10n context, with fallback value', function() {
90+
const mcx = new MessageContext();
91+
const l10n = new ReactLocalization([mcx]);
92+
const EnhancedComponent = withLocalization(DummyComponent);
93+
94+
mcx.addMessages(`
95+
foo = FOO
96+
`);
97+
98+
const wrapper = shallow(
99+
<EnhancedComponent />
100+
);
101+
102+
const getString = wrapper.prop('getString');
103+
// Returns the fallback if provided.
104+
assert.equal(getString('foo', {arg: 1}, 'fallback message'), 'fallback message');
105+
});
69106
});

0 commit comments

Comments
 (0)