Files
freeCodeCamp/common/app/utils/Language-Link.jsx
Berkeley Martinez 078560c3ba Add language routing
2016-07-28 23:40:01 -07:00

43 lines
935 B
JavaScript

import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router';
import supportedLanguages from '../../utils/supported-languages';
const toLowerCase = String.prototype.toLowerCase;
function addLang(url, lang) {
const maybeLang = toLowerCase.call(url.split('/')[1]);
if (supportedLanguages[maybeLang]) {
return url;
}
if (supportedLanguages[lang]) {
return `/${lang}${url}`;
}
return `/en${url}`;
}
const mapStateToProps = state => ({ lang: state.app.lang });
export class LangLink extends React.Component {
static displayName = 'LangLink';
static propTypes = {
to: PropTypes.string,
lang: PropTypes.string
};
render() {
const {
to,
lang,
...props
} = this.props;
return (
<Link
to={ addLang(to, lang) }
{ ...props }
/>
);
}
}
export default connect(mapStateToProps)(LangLink);