-
[Resource(
"
resx
"
,
"
TestSiteNVelocity.Controllers.ResourceFile
"
)]
public
class
ResourcedController : SmartDispatcherController
{
public void GetResources()
{
}
}
Resource属性第一个参数resx是用来在vm中使用的,第二个参数指定要使用的资源文件的名称,这个例子中使用的是程序集
TestSiteNVelocity.Controllers中的ResourceFile文件。
vm代码:
$resx.testKey
然后再加入
ResourceFile.resx和ResourceFile.zh-cn.resx两个资源文件,都包含名称为testKey的键值
那么在中文环境下浏览就会显示ResourceFile.zh-cn.resx里面定义的值,在英文或其他环境下浏览时就会显示ResourceFile.resx里面定义的值
另外还可以使用如下方式:
[Resource(
"
resx
"
,
"
TestSiteNVelocity.Controllers.ResourceFile
"
, CultureName
=
"
zh-cn
"
)]
来强制指定使用中文的资源文件,即
ResourceFile.zh-cn.resx
2、结合LocalizationFilter属性实现多语言
Controller代码:
[Resource(
"
resx
"
,
"
TestSiteNVelocity.Controllers.ResourceFile
"
)]
[LocalizationFilter(Castle.MonoRail.Framework.Filters.RequestStore.Cookie,
"
locale
"
)]
public
class
ResourcedController : SmartDispatcherController
{
public void GetResources()
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void SetLanguage(String langCode)
{
Response.CreateCookie("locale", langCode);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RedirectToAction("GetResources");
}
}
vm代码:
$resx.testKey
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
form method
=
"
post
"
action
=
"
SetLanguage.rails
"
id
=
"
form1
"
>
<
select name
=
"
langCode
"
onchange
=
"
document.getElementById('form1').submit();
"
>
<
option value
=
""
></
option
>
<
option value
=
"
zh-cn
"
>
中文
</
option
>
<
option value
=
"
en
"
>
English
</
option
>
</
select
>
</
form
>
和ASP.NET一样,MonoRail也可以方便的使用资源文件,以及利用资源文件来实现多语言的支持。
主要是利用了Resource和LocalizationFilter这两个属性,具体使用方法如下:
1、使用资源文件
Controller代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
vm代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
那么在中文环境下浏览就会显示ResourceFile.zh-cn.resx里面定义的值,在英文或其他环境下浏览时就会显示ResourceFile.resx里面定义的值
另外还可以使用如下方式:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、结合LocalizationFilter属性实现多语言
Controller代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
浏览效果如下:
就可以动态的选择语言了
解释:
当选择下拉列表中的一个语言后,调用Controller中的SetLanguage方法,将选择的语言放入cookie中,cookie名为:locale,然后重新进入此页面。进入此页面时由LocalizationFilter属性决定根据cookie中的locale的值来设定当前的本地化。
当然这里LocalizationFilter属性也可以根据Session、Cookie、QueryString、Form、Params中的值来决定本地化
本文转自永春博客园博客,原文链接:http://www.cnblogs.com/firstyi/archive/2007/11/21/967369.html,如需转载请自行联系原作者