Commit af96f9e1 by 小明

删除无用 i18 相关代码

parent 43853cf7
......@@ -49,7 +49,7 @@
"coveralls": "cat ./coverage/lcov/lcov.info | coveralls"
},
"lint-staged": {
"*.js": "lint:eslint"
"*.jsx": "lint:eslint"
},
"pre-commit": "lint:staged",
"dllPlugin": {
......
......@@ -19,12 +19,8 @@ import { Provider } from 'react-redux';
import { applyRouterMiddleware, Router, browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import { useScroll } from 'react-router-scroll';
import LanguageProvider from 'containers/LanguageProvider';
import configureStore from './store';
// Import i18n messages
import { translationMessages } from './i18n';
// Import the CSS reset, which HtmlWebpackPlugin transfers to the build folder
import 'sanitize.css/sanitize.css';
......@@ -39,6 +35,7 @@ const store = configureStore(initialState, browserHistory);
// is under the non-default key ("routing"), selectLocationState
// must be provided for resolving how to retrieve the "route" in the state
import { selectLocationState } from 'containers/App/selectors';
const history = syncHistoryWithStore(browserHistory, store, {
selectLocationState: selectLocationState(),
});
......@@ -52,55 +49,27 @@ const rootRoute = {
};
const render = (translatedMessages) => {
const render = () => {
ReactDOM.render(
<Provider store={store}>
<LanguageProvider messages={translatedMessages}>
<Router
history={history}
routes={rootRoute}
render={
// Scroll to top when going to a new page, imitating default browser
// behaviour
applyRouterMiddleware(useScroll())
}
/>
</LanguageProvider>
<Router
history={history}
routes={rootRoute}
render={
// Scroll to top when going to a new page, imitating default browser
// behaviour
applyRouterMiddleware(useScroll())
}
/>
</Provider>,
document.getElementById('app')
);
};
// Hot reloadable translation json files
if (module.hot) {
// modules.hot.accept does not accept dynamic dependencies,
// have to be constants at compile-time
module.hot.accept('./i18n', () => {
render(translationMessages);
});
}
// Chunked polyfill for browsers without Intl support
if (!window.Intl) {
(new Promise((resolve) => {
resolve(System.import('intl'));
}))
.then(() => Promise.all([
System.import('intl/locale-data/jsonp/de.js'),
]))
.then(() => render(translationMessages))
.catch((err) => {
throw err;
});
} else {
render(translationMessages);
}
render();
// Install ServiceWorker and AppCache in the end since
// it's not most important operation and if main code fails,
// we do not want it installed
import { install } from 'offline-plugin/runtime';
install();
System.import('main.js');
......@@ -9,15 +9,17 @@
* the linting exception.
*/
import React from 'react';
import { FormattedMessage } from 'react-intl';
import messages from './messages';
import React, { PureComponent } from 'react';
export default class HomePage extends PureComponent { // eslint-disable-line react/prefer-stateless-function
constructor(props) {
super(props);
}
export default class HomePage extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
render() {
return (
<h1>
<FormattedMessage {...messages.header} />
This is HomePage components !
</h1>
);
}
......
/*
* HomePage Messages
*
* This contains all the text for the HomePage component.
*/
import { defineMessages } from 'react-intl';
export default defineMessages({
header: {
id: 'app.components.HomePage.header',
defaultMessage: 'This is HomePage components !',
},
});
/*
*
* LanguageProvider actions
*
*/
import {
CHANGE_LOCALE,
} from './constants';
export function changeLocale(languageLocale) {
return {
type: CHANGE_LOCALE,
locale: languageLocale,
};
}
/*
*
* LanguageProvider constants
*
*/
export const CHANGE_LOCALE = 'app/LanguageToggle/CHANGE_LOCALE';
export const DEFAULT_LOCALE = 'en';
/*
*
* LanguageProvider
*
* this component connects the redux state language locale to the
* IntlProvider component and i18n messages (loaded from `app/translations`)
*/
import React from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { IntlProvider } from 'react-intl';
import { selectLocale } from './selectors';
export class LanguageProvider extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
render() {
return (
<IntlProvider locale={this.props.locale} key={this.props.locale} messages={this.props.messages[this.props.locale]}>
{React.Children.only(this.props.children)}
</IntlProvider>
);
}
}
LanguageProvider.propTypes = {
locale: React.PropTypes.string,
messages: React.PropTypes.object,
children: React.PropTypes.element.isRequired,
};
const mapStateToProps = createSelector(
selectLocale(),
(locale) => ({ locale })
);
function mapDispatchToProps(dispatch) {
return {
dispatch,
};
}
export default connect(mapStateToProps, mapDispatchToProps)(LanguageProvider);
/*
*
* LanguageProvider reducer
*
*/
import { fromJS } from 'immutable';
import {
CHANGE_LOCALE,
} from './constants';
import {
DEFAULT_LOCALE,
} from '../App/constants'; // eslint-disable-line
const initialState = fromJS({
locale: DEFAULT_LOCALE,
});
function languageProviderReducer(state = initialState, action) {
switch (action.type) {
case CHANGE_LOCALE:
return state
.set('locale', action.locale);
default:
return state;
}
}
export default languageProviderReducer;
import { createSelector } from 'reselect';
/**
* Direct selector to the languageToggle state domain
*/
const selectLanguage = () => (state) => state.get('language');
/**
* Select the language locale
*/
const selectLocale = () => createSelector(
selectLanguage(),
(languageState) => languageState.get('locale')
);
export {
selectLanguage,
selectLocale,
};
/**
* NotFoundPage
*
* This is the page we show when the user visits a url that doesn't have a route
*
* NOTE: while this component should technically be a stateless functional
* component (SFC), hot reloading does not currently support SFCs. If hot
* reloading is not a necessity for you then you can refactor it and remove
* the linting exception.
*/
import React from 'react';
import { FormattedMessage } from 'react-intl';
import messages from './messages';
export default class NotFound extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
render() {
return (
<h1>
<FormattedMessage {...messages.header} />
</h1>
);
}
}
/*
* NotFoundPage Messages
*
* This contains all the text for the NotFoundPage component.
*/
import { defineMessages } from 'react-intl';
export default defineMessages({
header: {
id: 'app.components.NotFoundPage.header',
defaultMessage: 'This is NotFoundPage component !',
},
});
/**
* i18n.js
*
* This will setup the i18n language files and locale data for your app.
*
*/
import { addLocaleData } from 'react-intl';
import { DEFAULT_LOCALE } from './containers/App/constants'; // eslint-disable-line
import enLocaleData from 'react-intl/locale-data/en';
export const appLocales = [
'en',
];
import enTranslationMessages from './translations/en.json';
addLocaleData(enLocaleData);
export const formatTranslationMessages = (locale, messages) => {
const defaultFormattedMessages = locale !== DEFAULT_LOCALE ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages) : {};
const formattedMessages = {};
const messageKeys = Object.keys(messages);
for (const messageKey of messageKeys) {
if (locale === DEFAULT_LOCALE) {
formattedMessages[messageKey] = messages[messageKey];
} else {
formattedMessages[messageKey] = messages[messageKey] || defaultFormattedMessages[messageKey];
}
}
return formattedMessages;
};
export const translationMessages = {
en: formatTranslationMessages('en', enTranslationMessages),
};
import React, { Component } from 'react';
export default class TestPage extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (<p>Hello World!</p>);
}
}
......@@ -6,7 +6,6 @@
import { combineReducers } from 'redux-immutable';
import { fromJS } from 'immutable';
import { LOCATION_CHANGE } from 'react-router-redux';
import languageProviderReducer from 'containers/LanguageProvider/reducer';
/*
* routeReducer
......@@ -42,7 +41,6 @@ function routeReducer(state = routeInitialState, action) {
export default function createReducer(asyncReducers) {
return combineReducers({
route: routeReducer,
language: languageProviderReducer,
...asyncReducers,
});
}
......@@ -33,14 +33,6 @@ export default function createRoutes(store) {
importModules.catch(errorLoading);
},
}, {
path: '*',
name: 'notfound',
getComponent(nextState, cb) {
System.import('containers/NotFoundPage')
.then(loadModule(cb))
.catch(errorLoading);
},
},
}
];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment