Skip to content

Commit 9baf53b

Browse files
add initialize prop to LocaizeProvider to support SSR (#127)
1 parent a2da3fb commit 9baf53b

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

src/LocalizeProvider.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
getOptions,
88
getTranslationsForActiveLanguage,
99
type LocalizeState,
10-
type Action
10+
type Action,
11+
INITIALIZE,
12+
InitializePayload
1113
} from './localize';
1214
import {
1315
LocalizeContext,
@@ -23,6 +25,7 @@ type LocalizeProviderState = {
2325
export type LocalizeProviderProps = {
2426
store?: Store<any, any>,
2527
getState?: Function,
28+
initialize?: InitializePayload,
2629
children: any
2730
};
2831

@@ -43,8 +46,16 @@ export class LocalizeProvider extends Component<
4346

4447
this.getContextPropsSelector = getContextPropsFromState(dispatch);
4548

49+
const initialState =
50+
this.props.initialize !== undefined
51+
? localizeReducer(undefined, {
52+
type: INITIALIZE,
53+
payload: this.props.initialize
54+
})
55+
: localizeReducer(undefined, ({}: any));
56+
4657
this.state = {
47-
localize: localizeReducer(undefined, ({}: any))
58+
localize: initialState
4859
};
4960
}
5061

src/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Component as ReactComponent,
55
ComponentType
66
} from 'react';
7-
import { Store } from 'redux';
87

98
export as namespace ReactLocalizeRedux;
109

@@ -81,8 +80,9 @@ export interface LocalizeContextProps {
8180
}
8281

8382
export interface LocalizeProviderProps {
84-
store?: Store<any>;
83+
store?: any;
8584
getState?: (state: any) => LocalizeState;
85+
initialize?: InitializePayload;
8686
children: any;
8787
}
8888

src/localize.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
22
import * as React from 'react';
3-
import { combineReducers } from 'redux';
43
import { flatten } from 'flat';
54
import {
65
createSelector,

src/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22
import React from 'react';
33
import { type Store } from 'redux';
4-
import { flatten } from 'flat';
54
import {
65
defaultTranslateOptions,
76
type MultipleLanguageTranslation

tests/LocalizeProvider.test.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
2+
import ReactDOMServer from "react-dom/server";
23
import Enzyme, { shallow, mount } from 'enzyme';
34
import { createStore, combineReducers } from 'redux';
45
import Adapter from 'enzyme-adapter-react-16';
56
import { Map } from 'immutable'
67
import { LocalizeProvider } from '../src/LocalizeProvider';
78
import { localizeReducer } from '../src/localize';
8-
import { getTranslate, getLanguages, initialize } from '../src';
9+
import { getTranslate, getLanguages, initialize, withLocalize, Translate } from '../src';
910
import { defaultTranslateOptions } from '../src/localize';
1011

1112
Enzyme.configure({ adapter: new Adapter() });
@@ -86,4 +87,56 @@ describe('<LocalizeProvider />', () => {
8687
)
8788
}).not.toThrow();
8889
});
90+
91+
it('should work with SSR', () => {
92+
class App extends React.Component {
93+
constructor(props) {
94+
super(props);
95+
96+
this.props.initialize({
97+
languages: [
98+
{ name: "English", code: "en" },
99+
{ name: "French", code: "fr" }
100+
],
101+
translation: {
102+
hello: ['hello', 'alo']
103+
},
104+
options: {
105+
defaultLanguage: "en",
106+
renderToStaticMarkup: ReactDOMServer.renderToStaticMarkup
107+
}
108+
});
109+
}
110+
111+
render() {
112+
return (
113+
<div>
114+
<Translate id="hello" />
115+
</div>
116+
);
117+
}
118+
}
119+
120+
const LocalizedApp = withLocalize(App);
121+
122+
const result = ReactDOMServer.renderToString(
123+
<LocalizeProvider initialize={{
124+
languages: [
125+
{ name: "English", code: "en" },
126+
{ name: "French", code: "fr" }
127+
],
128+
translation: {
129+
hello: ['hello', 'alo']
130+
},
131+
options: {
132+
defaultLanguage: "en",
133+
renderToStaticMarkup: ReactDOMServer.renderToStaticMarkup
134+
}
135+
}}>
136+
<LocalizedApp />
137+
</LocalizeProvider>
138+
);
139+
140+
expect(result).toEqual('<div>hello</div>');
141+
});
89142
});

0 commit comments

Comments
 (0)