本文介紹了Sailsjs 更改本地化的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我使用 Sails.js 已經有一段時間了,想知道是否有辦法根據 url 手動更改控制器的本地化.
I've been using Sails.js for quite some time and was wondering if there is a way to manually change the localization from the controllers depending on the url.
示例:http://example.com/en
將返回英文版本,http://example.com/de
將返回德文版本.
Example: http://example.com/en
will return the English version and http://example.com/de
will return the German one.
感謝您的幫助!!
推薦答案
您始終可以使用 req.setLocale()
或通過設置 <代碼>req.locale.您還可以使用策略在全局范圍內處理此問題:
You can always change the locale in a controller action by using req.setLocale()
or by setting the value of req.locale
. You can also handle this more globally by using a policy:
// config/routes.js
module.export.routes = {
'/:lang/': 'MyController.index',
'/:lang/help': 'MyController.help',
'/:lang/contact': 'MyController.contact',
...etc...
}
<小時>
// config/policies.js
module.exports.policies = {
'*' : 'localize'
}
<小時>
// api/policies/localize.js
module.exports = function(req, res, next) {
req.locale=req.param('lang');
next();
};
這篇關于Sailsjs 更改本地化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!