在切换语言时,我正在努力从我的 in 文件夹中的资源文件中的正确 strings.txt 中获取值.
我有一个 domain.com(英语)和一个 domain.nl(荷兰语).然而,我使用哪个域以及如何设置 currentUICulture 似乎并不重要,向用户显示的语言总是相同的!
[default.aspx.vb]
部分类 _Default继承 System.Web.UI.Page共享 rm 作为 ResourceManager = HttpContext.Current.Application("RM")Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Me.LoadLabel.Text = rm.GetString("homewelcome")结束子受保护的覆盖子 InitializeCulture()SetLanguage(Request.Url.ToString)结束子公共共享子集语言(ByVal URL 作为字符串)Dim lang As String = ""如果 URL.Contains("www.domain.nl") 那么lang = "nl"ElseIf URL.Contains("www.domain.com") 然后lang = "en"万一System.Threading.Thread.CurrentThread.CurrentUICulture = 新 System.Globalization.CultureInfo(lang)System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang)结束子结束类
[global.asax]
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)应用程序(RM")=新资源管理器(字符串",Assembly.Load(字符串"))结束子
在我的 bin 文件夹中:
binstrings.txtbin lstrings.nl.txtbinenstrings.en.txt
我像这样生成 dll:
resgen strings.txt strings.resourcesal/embed:strings.resources,strings.resources/out:strings.dllresgen nlstrings.nl.resourcesal/embed:nlstrings.nl.resources,strings.nl.resources/out:nlstrings.resources.dll/c:nlresgen enstrings.en.resourcesal/embed:enstrings.en.resources,strings.en.resources/out:enstrings.resources.dll/c:en
现在,所有文件似乎都已正确创建.
但是,当我访问 www.domain.com 时,使用的是 binstrings.txt 中的值,而不是(如我所期望的那样) binenstrings.en.txt 中的值
我在带有 IIS7.5 的 Windows 7 上运行当我调试时,我发现在我的 InitializeCulture 方法中:
Protected Overrides Sub InitializeCulture()SetLanguage(Request.Url.ToString)结束子
当我检查当前的 System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
时,它等于en",所以似乎文化设置正确,只是不正确使用了strings.dll.
但是发生了更奇怪的事情:我的 default.aspx <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:lookingfor%>"/>
当我接近 www.domain.nl(荷兰域!)时,Literal1 控件显示来自App_LocalResourcesdefault.aspx.en.resx"的值,而 rm.getstring 显示来自 bin 的值strings.txt当我检查当前 System.Threading.Thread.CurrentThread.CurrentUICulture.ToString 它等于nl"
当我接近 www.domain.com(英文域!)时,Literal1 控件显示来自App_LocalResourcesdefault.aspx.en.resx"的值,而 rm.getstring 显示来自 bin 的值strings.txt当我检查当前 System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
它等于en"
这是怎么回事?
所以在使用 www.domain.nl 时,使用了错误的 strings.dll,但使用了正确的 .resx
您可以使用 ResourceManager 的 GetString(string, CultureInfo)
重载来获取正确语言的字符串.
顺便说一句.global.asax 可能是设置 CurrentCulture 和 CurrentUICulture 的更好位置.
编辑根据彼得的要求提供代码示例
首先,我会检测和存储文化:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)'在每个请求开始时触发'作为 CultureInfo 的暗淡文化Dim uri As Uri = Request.Url如果 uri.ToString().Contains("nederlands") 那么文化 = 新 CultureInfo("nl-NL")ElseIf uri.ToString().Contains("polski") Then文化 = 新 CultureInfo("pl-PL")别的文化 = 新 CultureInfo("en-US")万一Thread.CurrentThread.CurrentCulture = 文化Thread.CurrentThread.CurrentUICulture = 文化结束子
根据您想要做什么,它可能不是重载的最佳功能,在您的情况下,我相信 Session_Start
更好(也就是说,除非您想让用户能够切换语言).
另一件事是,我没有用域名测试它,我使用了查询参数并且没有正确验证它们,但这只是为了举例.
我只能建议您使用 App_LocalResources,而不是实例化 ResourceManager 并手动编译您的资源.您只需在解决方案资源管理器中右键单击您的项目,选择添加 -> 添加 Asp.Net 文件夹 -> App_LocalResources.
创建适当命名的资源文件:Default.aspx.resx、Default.aspx.nl.resx 并将您想要本地化的内容添加到它们.在我的情况下是:
Default.aspx.resx(中性又名后备文化资源文件)label1 一些标签label2 其他一些标签
Default.aspx.nl.resx(对不起我可怜的荷兰语)
label1 Dit is een etiketlabel2 Dit is aner etiket
默认.aspx.pl.resx
label1 Jakaś tam labelkalabel2 Jakaś inna labelka
在后面的代码中,您可以像这样以编程方式修改网页的内容:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 处理 Me.LoadLabel2.Text = Me.GetLocalResourceObject("label2")结束子
出于许多原因,我不推荐这种方法(尤其是它不应该在页面加载处理程序中使用),但有时需要它.
终于到了我们漂亮(而且非常有用)的网页:
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="服务器"><标题></标题></头><身体><form id="form1" runat="server"><asp:Label ID="Label1" runat="server" Text="<%$ Resources:label1 %>"></asp:Label><br/><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div></表格></身体></html>如您所见,您可以使用以下表达式访问资源:<%$ Resources:label1 %>
.这将为您提供与 label1 资源键关联的字符串.需要注意的重要事项: runat="server" 不是可选的.
HTH.您可能还想阅读 关于 Asp.Net 本地化的教程.
Im struggling with gettings the values from the correct strings.txt in my resource files in my in folder when switching languages.
I have a domain.com (for English) and a domain.nl (for Dutch). It however doesnt seem to matter which domain I use and how I set the currentUICulture, the language shown to the user is always the same!
[default.aspx.vb]
Partial Class _Default
Inherits System.Web.UI.Page
Shared rm As ResourceManager = HttpContext.Current.Application("RM")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label.Text = rm.GetString("homewelcome")
End Sub
Protected Overrides Sub InitializeCulture()
SetLanguage(Request.Url.ToString)
End Sub
Public Shared Sub SetLanguage(ByVal URL As String)
Dim lang As String = ""
If URL.Contains("www.domain.nl") Then
lang = "nl"
ElseIf URL.Contains("www.domain.com") Then
lang = "en"
End If
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(lang)
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang)
End Sub
End Class
[global.asax]
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application("RM") = New ResourceManager("strings", Assembly.Load("strings"))
End Sub
In my bin folder I have:
binstrings.txt
bin
lstrings.nl.txt
binenstrings.en.txt
I generate the dlls like so:
resgen strings.txt strings.resources
al /embed:strings.resources,strings.resources /out:strings.dll
resgen nlstrings.nl.resources
al /embed:nlstrings.nl.resources,strings.nl.resources /out:nlstrings.resources.dll /c:nl
resgen enstrings.en.resources
al /embed:enstrings.en.resources,strings.en.resources /out:enstrings.resources.dll /c:en
Now, all files seem to be created correctly.
However, when I go to the www.domain.com, the values from binstrings.txt are used and NOT (like I would expect and desire), the values from binenstrings.en.txt
Im running on Windows 7 with IIS7.5
When I debugged I found that in my InitializeCulture method:
Protected Overrides Sub InitializeCulture()
SetLanguage(Request.Url.ToString)
End Sub
when I check for the current System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
its equal to "en", so it seems that the culture is SET correctly, its only that not the correct strings.dll is used.
But there's a more strange things going on: I have this in my default.aspx <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:lookingfor%>" />
When I aproach www.domain.nl (the Dutch domain!), the Literal1 controls displays the value from "App_LocalResourcesdefault.aspx.en.resx" and the rm.getstring shows me a value from binstrings.txt
And when I check the current System.Threading.Thread.CurrentThread.CurrentUICulture.ToString its equal to "nl"
When I aproach www.domain.com (the English domain!), the Literal1 controls displays the value from "App_LocalResourcesdefault.aspx.en.resx" and the rm.getstring shows me a value from binstrings.txt
And when I check the current System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
its equal to "en"
What is going on here?
So when using www.domain.nl, the WRONG strings.dll is used, but the CORRECT .resx
解决方案 You can use ResourceManager's GetString(string, CultureInfo)
overload to get strings in correct language.
BTW. global.asax is probably the better place to set CurrentCulture and CurrentUICulture.
EDIT Providing code sample per Peter's request
Globabal.asax
For starters, I would detect and store culture:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request '
Dim culture As CultureInfo
Dim uri As Uri = Request.Url
If uri.ToString().Contains("nederlands") Then
culture = New CultureInfo("nl-NL")
ElseIf uri.ToString().Contains("polski") Then
culture = New CultureInfo("pl-PL")
Else
culture = New CultureInfo("en-US")
End If
Thread.CurrentThread.CurrentCulture = culture
Thread.CurrentThread.CurrentUICulture = culture
End Sub
Depending on what you want to do, it might not be the best Function to overload, in your case I believe Session_Start
is better (that is unless you want to give users an ability to switch language).
Another thing is, I didn't test it with domain names, I used query parameters and haven't validate them properly but it was just for the purpose of example only.
Create App_LocalResources
Instead of instantiating ResourceManager and compiling your resources manually, I can only suggest you to use App_LocalResources. You just need to right-click on your project in the Solution Explorer, choose Add -> Add Asp.Net Folder -> App_LocalResources.
Create resource files
Create appropriately named resource files: Default.aspx.resx, Default.aspx.nl.resx and add the contents you want to localize to them. In my case it was:
Default.aspx.resx (neutral aka fall-back culture resource file)
label1 Some label
label2 Some other label
Default.aspx.nl.resx (Sorry for my poor Dutch)
label1 Dit is een etiket
label2 Dit is ander etiket
Default.aspx.pl.resx
label1 Jakaś tam labelka
label2 Jakaś inna labelka
Default.aspx.vb (code behind)
In code behind you can programmatically modify contents of your web page like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label2.Text = Me.GetLocalResourceObject("label2")
End Sub
For many reasons I would not recommend this method (especially it should not be used in page load handler), however it is sometimes needed.
Default.aspx
Finally we are at our beautiful (and very useful) web page:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:label1 %>"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
As you can see, you can access the resources with following expression: <%$ Resources:label1 %>
. This will give you the string associated with label1 resource key. Important thing to note: runat="server" is not optional.
HTH. You may want to read the tutorial on Asp.Net Localization as well.
这篇关于CurrentThread.CurrentUICulture 设置正确,但似乎被 asp.net 忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!