如何使用Catberry框架通过URIpath段设置应用程序语言?

例如,我使用catberry-ln插件我想定义应用程序的语言是这样的:

www.example.com/en www.example.com/de www.example.com/ru 

因此,使用Catberry的路由规则并添加从URI获取语言环境参数的LocaleStore ,所描述的方法在您的项目中非常容易实现。 商店数据可以被任何其他商店使用,而不会影响性能。

例如,你有这样的路线规则:

 module.exports = [ '/:locale[LocaleStore]/somecrazysegment' ]; 

另外,你有LocaleStore:

 LocaleStore.prototype.load = function () { // here can be a profile's language request or something else return this.$context.state.locale; }; 

所以,我们有LocaleStore,现在我们可以使用它。

 SomeStore.prototype.load = function () { return Promise.all([ this.$context.getStoreData('LocaleStore'), this._uhr.get('http://api.some.org/data') ]) .then(function (results) { return { locale: results[0], obj: results[1] }; }); }; 

之后,您可以像使用示例一样在组件中使用这些数据:

 Component.prototype.render = function () { var self = this; return this.$context.getStoreData() .then(function (data) { return { localizedEat: self._l10n.get(data.locale, 'EAT'), localizedApple: util.format( self._l10n.pluralize(data.locale, 'APPLE', data.obj.appleCount), appleCount ) }; }); };