【问题标题】:locale as URL parameter with ReactRouter v4 and redux使用 ReactRouter v4 和 redux 作为 URL 参数的语言环境
【发布时间】:2018-07-11 21:52:39
【问题描述】:

我正在使用 React 和 Redux 构建一个多语言应用程序:

{
    "react": "16.2.0",
    "redux": "3.7.2",
    "react-redux": "^5.0.6",
    "react-router-redux": "^5.0.0-alpha.9",
    "react-router": "^4.2.0",
    "react-redux-i18n": "^1.9.1"
}

已解决(使用path-to-regexp):https://codesandbox.io/s/307nz4p9nm

编辑:终于可以渲染组件了。最后一个问题是路径生成,对于我的语言选择器。

我正在使用依赖于createBrowserHistory()<ConnectedRouter />

我有以下结构:

// containers/App.jsx
<div>
    <ul>
        <li>
            <NavLink to={`${match.url}/`}>Home</NavLink>
        </li>
        <li>
            <NavLink to={`${match.url}/ABC`}>ABC</NavLink>
        </li>
        <li>
            <NavLink to={`${match.url}/DEF`}>DEF</NavLink>
        </li>
        <li>
            <NavLink to={`${match.url}/GHI`}>GHI</NavLink>
        </li>
        <li>
            <NavLink to={`${match.url}/JKL`}>JKL</NavLink>
        </li>
    </ul>
    <div>
        <Router />
    </div>
    <footer>
        <Link to={ (???) }>English</Link>
        <Link to={ (???) }>French</Link>
    </footer>
</div>

// containers/Router.jsx
<Switch>
    <Route exact path={`{match.url}/`} component={Home} />
    <Route exact path={`{match.url}/abc`} component={AbcComponent} />
    <Route exact path={`{match.url}/def`} component={DefComponent} />
    <Route exact path={`{match.url}/ghi`} component={GhiComponent} />
    <Route exact path={`{match.url}/jkl`} component={JklComponent} />
</Switch>

// containers/RootContainer.jsx
<Provider store={store}>
    <ConnectedRouter history={history}>
        <Switch>
            <Route path='/:locale(en|fr)' component={App} />
            <Redirect to='/en' />
        </Switch>
    </ConnectedRouter>
</Provider>

这样渲染:

const store = createStore(combineReducers({}))
const history = createBrowserHistory()

ReactDOM.render(
    <RootContainer store={store} history={history} />,
    document.getElementById('app-container')
)

这会导致以下 URI:

/
    /(en|fr)
        /abc
        /def
        /ghi
        /jkl

现在,我正在寻找一种方法来实现我的语言环境选择器:生成当前页面的路径,只需更新:locale 参数。

无论我当前访问的路线是什么(/en/en/abc),match.path 总是返回 /:locale(en|fr),而且我从不从我的 App 组件访问子路径(例如 /abc)。

那么如何挂钩 react-redux-i18n 以自动从 URL 获取语言环境,而不是在自己的状态下管理它?

【问题讨论】:

    标签: redux internationalization react-router-v4 react-router-redux react-redux-i18n


    【解决方案1】:

    &lt;Switch&gt; 仅适用于 Route 和 Redirect 作为直接后代。所以LocalizedRoute 组件不起作用。

    <ConnectedRouter basename=/:locale/ history={history} />
    

    basename 属性对我来说是未知的,并且是不必要的 imo,请将其删除。

    你可以做的是使用动态路由:

    <Route path="/:locale/<path>"/> 
    

    并减少 LOCATION_CHANGE 操作中的语言环境参数。但是还有更多的解决方案,例如:

    <Switch>
       <Route path="/en" component={SwitchMyRoutes} /> // No exact!!
       <Route path="/fr" render={(match,location)=> <SwitchMyRoutes locale="fr" match={match}/>} />
       <Route render={({location})=> <Redirect to={`/en/${location}`}/>}/>
    </Switch> 
    

    【讨论】:

    • 嗨,Dennie,非常感谢,我遇到了非常相似的事情,只是使用了一个参数,而不是使用硬编码的语言环境复制 &lt;Router /&gt; ;但是,找不到可靠的方式生成&lt;Link /&gt;目标路径...codesandbox.io/s/zrjzkjvmj4
    • 这个例子有什么问题?
    • 嗯,实际上使用 path-to-regexp 和 path.split('/') 使它可以工作... IMO 这样做真是太糟糕了?!
    • codesandbox.io/s/wllon0wlk,你的问题是/字符的编码,它呈现为unicode字符,所以react-router无法匹配url。
    • 好吧,我猜location.pathname.substring() 也可以,只要你的/:locale 参数总是具有相同的长度。否则:codesandbox.io/s/307nz4p9nm
    猜你喜欢
    • 2020-01-18
    • 2020-04-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多