How to install and process the language in response mode?

I’m trying to solve this whole day, and finally I will come to you.

The task is simple, I need to set the type of language in the URL, so it looks something like this: domain.com/{langVarasket/other/paths

And you can change it by clicking / selecting the language in the application header or any other component.

Important: the language variable must always remain in the URL.

I use "agent-router": "^ 2.7.0", "react": "^ 15.3.1".

Here's what my router configurator looks like:

export default (
  <Router history={browserHistory}>
    <Route path="/:lang" component={MainApp}>
      <IndexRoute component={Home} />
      <Route component={OtherPage} />
    </Route>
    <Route path='*' component={NotFound} />
  </Router>
);

I hope this will be if I do not update my question. But to me, this seems like a pretty normal use case for site URLs.

thank

+4
2

, userRedirect, , . . / :lang <Route path=":lang/" > , - * ( .

import React from 'react';
import { Route } from 'react-router';
import { App, About } from './containers';

function userRedirect(nextState, replace) {
  var defaultLanguage = 'en-gb';
  var redirectPath = defaultLanguage + nextState.location.pathname
  replace({
    pathname: redirectPath,
  })
};

<Route path="/" component={App}>
  <Route path=":lang/" >
    <Route path="about">
      <Route path="show" component={About}/>
    </Route>
  </Route>
  <Route path="*" onEnter={userRedirect} />
</Route>

URL <domain>/about/show, <domain>/en-gb/about/show. , , .

+2

 function userRedirect(nextState, replace) {
  var defaultLanguage = 'en-gb';
  var redirectPath = defaultLanguage + nextState.location.pathname
  replace({
    pathname: redirectPath,
  })
};

<Route path="/" component={App}>
  <Route path=":lang/" >
    <Route path="about">
      <Route path="show" component={About}/>
    </Route>
  </Route>
  <Route path="*" onEnter={userRedirect} />
</Route>

.

    var redirectPath = defaultLanguage + '/' + nextState.location.pathname

, , , "/link-to-page", "en/link-to-page", userRedirect . , . - url "... en/en/en/en/link-to-page"

0

Source: https://habr.com/ru/post/1655860/


All Articles