Skip to content

Commit

Permalink
Translation language respect current url (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey authored and JoelMarcey committed Jun 7, 2018
1 parent d04b3ca commit ff93ba9
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/core/nav/HeaderNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,29 @@ class LanguageDropDown extends React.Component {
const enabledLanguages = env.translation
.enabledLanguages()
.filter(lang => lang.tag !== this.props.language)
.map(lang => (
<li key={lang.tag}>
<a href={siteConfig.baseUrl + lang.tag}>{lang.name}</a>
</li>
));
.map(lang => {
// build the href so that we try to stay in current url but change the language.
let href = siteConfig.baseUrl + lang.tag;
if (
this.props.current &&
this.props.current.permalink &&
this.props.language
) {
href =
siteConfig.baseUrl +
this.props.current.permalink.replace(
`/${this.props.language}/`,
`/${lang.tag}/`
);
} else if (this.props.current.id && this.props.current.id !== 'index') {
href = siteConfig.baseUrl + lang.tag + '/' + this.props.current.id;
}
return (
<li key={lang.tag}>
<a href={utils.getPath(href, this.props.cleanUrl)}>{lang.name}</a>
</li>
);
});
// if no languages are enabled besides English, return null
if (enabledLanguages.length < 1) {
return null;
Expand Down Expand Up @@ -131,6 +149,8 @@ class HeaderNav extends React.Component {
<LanguageDropDown
baseUrl={this.props.baseUrl}
language={this.props.language}
current={this.props.current}
cleanUrl={this.props.config.cleanUrl}
key="languagedropdown"
/>
);
Expand Down

0 comments on commit ff93ba9

Please sign in to comment.