我在Session ["lang"]中有目标语言,它是"en"或"it".我已将此添加到Site.master:
I have my target language in Session["lang"], which is either "en" or "it". I have added this to the Site.master:
<script runat="server"> void Page_Load(object sender, EventArgs e) { string lang = Session["lang"].ToString(); System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang); System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang); } </script>然后,我想调用这样的资源字符串:
Then I'd like to invoke a resource string like this:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:Global, test %>"></asp:Label>App_GlobalResources中有两个文件,分别名为Global.resx和Global.en.resx.
I have two files in the App_GlobalResources, named Global.resx and Global.en.resx.
问题在于,无论lang变量中的内容是什么,我总是从主Global.resx中获得结果,而我从未从Global.en.resx中获得英文版本.
The problems is that no matter what is in the lang variable, I always get the results from the main Global.resx, and I never get the english version from Global.en.resx
我完全错了吗?
我尝试将System.Threading ...部分放在Global.asax.cs的Application_PreRequestHandlerExecute方法中,但是结果是相同的.
I tried putting the System.Threading... part in the Application_PreRequestHandlerExecute method in Global.asax.cs but the result was the same.
谢谢
PS:我想问一个简单的方法来完成这项工作.如果要使用复杂的方式,请使用以下方法: helios.ca/2009/05/27/aspnet-mvc-and-localization/
PS: I am asking about a way to make this work in a simple way. If I was to use the complicate way, I'd go with this: helios.ca/2009/05/27/aspnet-mvc-and-localization/
推荐答案我在asp mvc应用程序中具有相同的困境(如何实现本地化).
i had the same dilema(how to implement localization) in my asp mvc app.
我遵循了此处发布的说明,并且它就像一种魅力.
I followed the instructions posted here and it works like a charm.
因此,我在内容"下创建了一个名为本地化"的文件夹,然后为我要翻译的每种语言创建了资源resx文件.请记住,resx文件名有一个约定.即
So i created a folder named Localization under Content and then i create Resources resx files for each language i want to translate. Keep in mind that there is a convention for the resx file names. ie
Resources.resx是所有内容的默认后备.
Resources.resx is the default fall back for everything.
Resources.en-GB.resx适用于英语GB
Resources.en-GB.resx is for english GB
Resources.zh-CN.resx适用于英语US
Resources.en-US.resx is for english US
等
只需确保您按照链接中发布的说明进行嵌入,并使资源在应用程序的所有位置(视图,控制器等)可用
Just make sure you follow the instructions posted in the link to embed and make the Resources available in all places in your app (views, controllers etc)
我想补充一点,因为我想从我的应用程序手动设置本地,所以我从web.config中省略了这一行.
I want to add that i ommited this line from web.config since i wanted to manually set the local from my app.
<globalization uiCulture="auto" culture="auto"/>相反,我创建了以下类:
Instead i have created the following class:
public class SmartController : Controller { public SmartController() { System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); } }所有控制器都继承自此类.
All controllers inherit from this class.
由于这是语言环境的管理集,因此必须从我的应用程序设置中进行设置.您可以从Cookies读取并设置它,否则可以.这是imo,这是我到目前为止遇到的最简单的本地化解决方案.
Since this is an administrative set of the locale i have to set it from my apps settings. You could read it from Cookies and set it, or otherwise. This is imo the simplest solution for localization that i have encountered so far.
一旦实现,您就可以通过以下简单的代码行引用添加的任何字符串,无需额外的代码.
Once implemented you can refer to any string you add by the following simple line of code, no extra code needed.
<%= Resources.Strings.TranslatedTerm %>更多推荐
asp.net MVC:本地化
发布评论