mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-26 22:47:49 +02:00
Translation language respect current url (#734)
This commit is contained in:
parent
d04b3ca87b
commit
ff93ba9853
1 changed files with 25 additions and 5 deletions
|
@ -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;
|
||||
|
@ -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"
|
||||
/>
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue