Skip to content

Commit af4cadd

Browse files
committed
enable lookup of text messages outside of the prefixed scope
1 parent 8a6d190 commit af4cadd

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/lib/messageSource.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ function enhanceWithMessages(keyPrefix, WrappedComponent) {
3232
* @param key the key of the message.
3333
* @param params an optional parameters (param0, param1 ...).
3434
*/
35-
getMessage = (key, ...params) => getMessageWithParams(this.context, normalizedKeyPrefix + key, ...params);
35+
getMessage = (key, ...params) => {
36+
const textKey = normalizedKeyPrefix + key;
37+
const message = getMessageWithParams(this.context, textKey, ...params);
38+
if (message === textKey) {
39+
return getMessageWithParams(this.context, key, ...params);
40+
}
41+
42+
return message;
43+
};
3644

3745
/**
3846
* Retrieves a text message with named parameters.
@@ -47,8 +55,15 @@ function enhanceWithMessages(keyPrefix, WrappedComponent) {
4755
* @param key the key of the message.
4856
* @param namedParams a map of named parameters.
4957
*/
50-
getMessageWithNamedParams = (key, namedParams) =>
51-
getMessageWithNamedParams(this.context, normalizedKeyPrefix + key, namedParams);
58+
getMessageWithNamedParams = (key, namedParams) => {
59+
const textKey = normalizedKeyPrefix + key;
60+
const message = getMessageWithNamedParams(this.context, textKey, namedParams)
61+
if (message === textKey) {
62+
return getMessageWithNamedParams(this.context, key, namedParams);
63+
}
64+
65+
return message;
66+
};
5267

5368
render() {
5469
if (process.env.NODE_ENV !== 'production') {

src/lib/messageSource.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as MessageSource from './messageSource';
77
describe('MessageSource', () => {
88
const translations = {
99
'hello.world': 'Hello World',
10+
'greeting.normal': 'Hi',
1011
'greeting.named': 'Hello {name}',
1112
};
1213

@@ -104,6 +105,32 @@ describe('MessageSource', () => {
104105
expect(nestedComponentInstance.children[0]).toBe('Hello World');
105106
});
106107

108+
it('[curried] retrieves the correct translated value in mixed mode', () => {
109+
function Nested(props) {
110+
const { getMessage } = props;
111+
return (
112+
<React.Fragment>
113+
{getMessage('world')}
114+
{getMessage('greeting.normal')}
115+
</React.Fragment>
116+
);
117+
}
118+
119+
const NestedHOC = MessageSource.withMessages('hello')(Nested);
120+
121+
const renderer = TestRenderer.create(
122+
<MessageSource.Provider value={translations}>
123+
<NestedHOC />
124+
</MessageSource.Provider>,
125+
);
126+
127+
const { root } = renderer;
128+
const nestedComponentInstance = root.findByType(Nested);
129+
130+
expect(nestedComponentInstance.children[0]).toBe('Hello World');
131+
expect(nestedComponentInstance.children[1]).toBe('Hi');
132+
});
133+
107134
it('supports nested overrides', () => {
108135
const levelOne = {
109136
'hello.world': 'Hello World',

0 commit comments

Comments
 (0)