我使用ember-i18n库进行国际化,并希望在路由中保存当前的区域设置,如:
domain.com -默认en为空
doman.com/es,doman.com/de-另一个
为此,我尝试使用rootURL
Router.reopen({
rootURL: '/' ('es' or 'de')
});Problem:当rootURL不为空时,应用程序无法重定向并因错误而失败:ember.debug.js:4903 Uncaught Error: Assertion Failed: Path / does not start with the provided rootURL /es/
问题:什么是进行重定向的最佳解决方案,所有的逻辑都将保存在emebr中,所以我不能将这个逻辑移到nginx中等等。
另一种选择是创建包装器路由,如下所示:
this.route(
'lang', { path: '/:lang' }, function (){..}
);这个解决方案看起来不太好:
link-to助手将需要lang paramlang不能为空(对于默认语言)UPDATE:我了解如何动态更改rootURL,但无法自动重定向。
发布于 2016-09-16 16:08:14
找到解决方案,请在实例初始化器中描述此逻辑。
export function initialize(app) {
var router = app.lookup('router:main');
var i18n = app.lookup('service:i18n');
var path = window.location.pathname;
var currentLang = ENV.i18n.defaultLocale;
var newPath = '';
var LangFromPath = path.match('^/([a-z]{2})(?:/|$)');
if (LangFromPath && LangFromPath[1]){
currentLang = (ENV.i18n.allowedLocales.indexOf(LangFromPath[1]) > -1) ? LangFromPath[1] : currentLang;
}
if (currentLang != ENV.i18n.defaultLocale) {
var newPath = '/' + currentLang + '/';
}
router.rootURL = newPath;
i18n.set('locale', currentLang);
if (newPath && path.indexOf(newPath) === -1) {
window.location.pathname = newPath;
}
}https://stackoverflow.com/questions/39467567
复制相似问题