-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from mircobe87/issue-32
#32 multilanguage support
- Loading branch information
Showing
15 changed files
with
626 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var assign = require('object-assign'); | ||
var keymirror = require('keymirror'); | ||
|
||
var Dispatcher = require('../dispatchers/Dispatcher'); | ||
|
||
var I18NActions = { | ||
launch(actionType, args) { | ||
Dispatcher.dispatch(assign({type: actionType}, args)); | ||
} | ||
}; | ||
|
||
assign(I18NActions, keymirror({ | ||
CHANGE_LANG: null | ||
})); | ||
|
||
module.exports = I18NActions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var expect = require('expect'); | ||
var assign = require('object-assign'); | ||
var Dispatcher = require('../../dispatchers/Dispatcher'); | ||
|
||
describe('This test for I18NActions', () => { | ||
var I18NActions; | ||
|
||
beforeEach((done) => { | ||
I18NActions = require('../I18NActions'); | ||
setTimeout(done); | ||
}); | ||
|
||
afterEach((done) => { | ||
var name = require.resolve('../I18NActions'); | ||
delete require.cache[name]; | ||
setTimeout(done); | ||
}); | ||
|
||
it('checks launch(actionType, args)', () => { | ||
const aType = "test"; | ||
const aArgs = { | ||
k0: "v0", | ||
k1: "v1" | ||
}; | ||
const testArgs = assign({type: aType}, aArgs); | ||
const spy = expect.spyOn(Dispatcher, 'dispatch'); | ||
I18NActions.launch(aType, aArgs); | ||
|
||
expect(spy.calls.length).toBe(1); | ||
expect(spy.calls[0].context).toBe(Dispatcher); | ||
expect(spy.calls[0].arguments).toEqual([ testArgs ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
module.exports.Message = require('./Message'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var React = require('react'); | ||
var ReactIntl = require('react-intl'); | ||
var FormattedMessage = ReactIntl.FormattedMessage; | ||
|
||
var I18NStore = require('../../stores/I18NStore'); | ||
|
||
var Message = React.createClass({ | ||
propTypes: { | ||
msgId: React.PropTypes.string.isRequired, | ||
msgParams: React.PropTypes.object | ||
}, | ||
getInitialState() { | ||
const currentLocale = I18NStore.getCurrentLocale(); | ||
const msg = I18NStore.getMsgById(this.props.msgId); | ||
return { | ||
locales: currentLocale, | ||
msg: msg | ||
}; | ||
}, | ||
// it makes this component reactive when a new language is loaded in | ||
// language store. | ||
componentDidMount() { | ||
I18NStore.register(I18NStore.Event.LANG_CHANGED, this.onLangChanged); | ||
}, | ||
componentWillUnmount() { | ||
I18NStore.unregister(I18NStore.Event.LANG_CHANGED, this.onLangChanged); | ||
}, | ||
// it updates the state of this component when the language store loads a new one. | ||
onLangChanged() { | ||
const currentLocale = I18NStore.getCurrentLocale(); | ||
const msg = I18NStore.getMsgById(this.props.msgId); | ||
this.setState({ | ||
locales: currentLocale, | ||
msg: msg | ||
}); | ||
}, | ||
render() { | ||
return <FormattedMessage locales={this.state.locales} message={this.state.msg} {...this.props.msgParams}/>; | ||
} | ||
}); | ||
|
||
module.exports = Message; |
53 changes: 53 additions & 0 deletions
53
web/client/components/I18N/__tests__/I18N.Message-test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var expect = require('expect'); | ||
|
||
var React = require('react/addons'); | ||
var I18NStore = require('../../../stores/I18NStore'); | ||
var I18N = require('../I18N'); | ||
var I18NActions = require('../../../actions/I18NActions'); | ||
|
||
var ita = require('../../../stores/data.it-IT'); | ||
var eng = require('../../../stores/data.en-US'); | ||
|
||
describe('This test for I18N.Message', () => { | ||
afterEach((done) => { | ||
React.unmountComponentAtNode(document.body); | ||
document.body.innerHTML = ''; | ||
setTimeout(done); | ||
}); | ||
|
||
it('checks if the component reacts at I18NStore edits', () => { | ||
var testMsg; | ||
var currentData; | ||
const msgId = "aboutLbl"; | ||
const data = { | ||
"en-US": eng, | ||
"it-IT": ita | ||
}; | ||
currentData = data[I18NStore.getCurrentLocale()]; | ||
testMsg = currentData.messages[msgId]; | ||
|
||
const cmp = React.render(<I18N.Message msgId={msgId}/>, document.body); | ||
expect(cmp).toExist(); | ||
|
||
const cmpDom = React.findDOMNode(cmp); | ||
expect(cmpDom).toExist(); | ||
expect(cmpDom.innerHTML).toBe(testMsg); | ||
|
||
const nextLocale = I18NStore.getCurrentLocale() === "it-It" ? "en-US" : "it-IT"; | ||
I18NStore._emulate_dispatcher({ | ||
locale: nextLocale, | ||
type: I18NActions.CHANGE_LANG | ||
}); | ||
|
||
currentData = data[I18NStore.getCurrentLocale()]; | ||
testMsg = currentData.messages[msgId]; | ||
expect(cmpDom.innerHTML).toBe(testMsg); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var React = require('react'); | ||
var BootstrapReact = require('react-bootstrap'); | ||
var Input = BootstrapReact.Input; | ||
|
||
var I18NStore = require('../../stores/I18NStore'); | ||
var I18NActions = require('../../actions/I18NActions'); | ||
|
||
var LangSelector = React.createClass({ | ||
propTypes: { | ||
locales: React.PropTypes.object | ||
}, | ||
getDefaultProps() { | ||
return { | ||
locales: I18NStore.getSupportedLocales() | ||
}; | ||
}, | ||
render() { | ||
var val; | ||
var label; | ||
var list = []; | ||
for (let lang in this.props.locales) { | ||
if (this.props.locales.hasOwnProperty(lang)) { | ||
val = this.props.locales[lang]; | ||
label = lang; | ||
list.push(<option value={val} key={val}>{label}</option>); | ||
} | ||
} | ||
return ( | ||
<Input type="select" bsSize="small" onChange={this.launchNewLangAction}> | ||
{list} | ||
</Input> | ||
); | ||
}, | ||
launchNewLangAction() { | ||
var element = React.findDOMNode(this); | ||
var selectNode = element.getElementsByTagName('select').item(0); | ||
I18NActions.launch(I18NActions.CHANGE_LANG, {locale: selectNode.value}); | ||
} | ||
}); | ||
|
||
module.exports = LangSelector; |
59 changes: 59 additions & 0 deletions
59
web/client/components/LangSelector/__tests__/LangSelector-test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var expect = require('expect'); | ||
|
||
var React = require('react/addons'); | ||
var I18NStore = require('../../../stores/I18NStore'); | ||
var I18NActions = require('../../../actions/I18NActions'); | ||
var LangSelector = require('../LangSelector'); | ||
|
||
describe('LangSelector', () => { | ||
afterEach((done) => { | ||
React.unmountComponentAtNode(document.body); | ||
document.body.innerHTML = ''; | ||
setTimeout(done); | ||
}); | ||
|
||
it('checks default', () => { | ||
var lbl; | ||
var value; | ||
|
||
const cmp = React.render(<LangSelector/>, document.body); | ||
expect(cmp).toExist(); | ||
|
||
const cmpDom = React.findDOMNode(cmp); | ||
expect(cmpDom).toExist(); | ||
expect(cmpDom.id).toNotExist(); | ||
|
||
const select = cmpDom.getElementsByTagName("select").item(0); | ||
const opts = select.childNodes; | ||
const langs = I18NStore.getSupportedLocales(); | ||
|
||
for (let i = 0; i < opts.length; i++) { | ||
lbl = opts[i].innerHTML; | ||
value = opts[i].value; | ||
expect(langs.hasOwnProperty(lbl)).toBe(true); | ||
expect(langs[lbl]).toBe(value); | ||
} | ||
}); | ||
|
||
it('checks if a change of the compo fires the proper action', () => { | ||
const spy = expect.spyOn(I18NActions, 'launch'); | ||
|
||
const cmp = React.render(<LangSelector/>, document.body); | ||
const cmpDom = React.findDOMNode(cmp); | ||
const select = cmpDom.getElementsByTagName("select").item(0); | ||
|
||
select.value = "it-IT"; | ||
React.addons.TestUtils.Simulate.change(select, {target: {value: 'it-IT'}}); | ||
// select.children[1].click(); | ||
|
||
expect(spy.calls.length).toBe(1); | ||
expect(spy.calls[0].arguments).toEqual([I18NActions.CHANGE_LANG, {locale: "it-IT"}]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright 2015, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
var Dispatcher = require('flux').Dispatcher; | ||
|
||
module.exports = new Dispatcher(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.