CurrentThread.CurrentUICulture设置正确,但似乎是asp.net被忽略 [英] CurrentThread.CurrentUICulture is set correctly but seems to be ignored by asp.net
问题描述
林与gettings从正确的strings.txt在我的\\ bin文件夹中的值在我的资源文件切换语言时的挣扎。
Im struggling with gettings the values from the correct strings.txt in my resource files in my \bin folder when switching languages.
我有一个domain.com(英文)和domain.nl(荷兰)。然而犯规它似乎不管我用哪个域以及如何设置的CurrentUICulture,显示给用户的语言始终是相同的!
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]
[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中]
[global.asax]
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application("RM") = New ResourceManager("strings", Assembly.Load("strings"))
End Sub
在我的bin文件夹我有:
In my bin folder I have:
斌\\ strings.txt
BIN \\ NL \\ strings.nl.txt
BIN \\ EN \\ strings.en.txt
bin\strings.txt bin\nl\strings.nl.txt bin\en\strings.en.txt
我生成的dll像这样:
I generate the dlls like so:
resgen strings.txt strings.resources
al /embed:strings.resources,strings.resources /out:strings.dll
resgen nl\strings.nl.resources
al /embed:nl\strings.nl.resources,strings.nl.resources /out:nl\strings.resources.dll /c:nl
resgen en\strings.en.resources
al /embed:en\strings.en.resources,strings.en.resources /out:en\strings.resources.dll /c:en
现在,所有的文件似乎正确创建。
Now, all files seem to be created correctly.
然而,当我去www.domain.com,用于从斌\\ strings.txt值和NOT(就像我希望和愿望),从仓值\\ EN \\ strings.en.txt
However, when I go to the www.domain.com, the values from bin\strings.txt are used and NOT (like I would expect and desire), the values from bin\en\strings.en.txt
林在Windows 7上运行IIS7.5使用
当我调试,我发现,在我InitializeCulture方式:
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
当我检查电流 System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
它等于恩,这样看来,文化是否设置正确,它只是没有使用正确的strings.dll。
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.
但还有一个更奇怪的事情怎么回事:我有这个在我的Default.aspx < ASP:文字ID =Literal1=服务器文本=<%$资源: lookingfor%gt;中/>
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%>" />
-
在我的aproach www.domain.nl(荷兰域!),则Literal1控制显示从App_LocalResources文件\\ default.aspx.en.resx的价值和rm.getstring显示我从斌的值\\ strings.txt
当我检查当前System.Threading.Thread.CurrentThread.CurrentUICulture.ToString它等于NL
When I aproach www.domain.nl (the Dutch domain!), the Literal1 controls displays the value from "App_LocalResources\default.aspx.en.resx" and the rm.getstring shows me a value from bin\strings.txt And when I check the current System.Threading.Thread.CurrentThread.CurrentUICulture.ToString its equal to "nl"
在我的aproach www.domain.com(中英文域名!),则Literal1控制显示从App_LocalResources文件\\ default.aspx.en.resx的价值和rm.getstring显示我从斌的值\\ strings.txt
当我检查当前的 System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
中的平等为en
When I aproach www.domain.com (the English domain!), the Literal1 controls displays the value from "App_LocalResources\default.aspx.en.resx" and the rm.getstring shows me a value from bin\strings.txt
And when I check the current System.Threading.Thread.CurrentThread.CurrentUICulture.ToString
its equal to "en"
这是怎么回事呢?
因此,在使用时www.domain.nl,错strings.dll被使用,但正确的.resx
So when using www.domain.nl, the WRONG strings.dll is used, but the CORRECT .resx
推荐答案
您可以使用ResourceManager的的GetString(字符串,CultureInfo的)
重载来获得正确的语言字符串。
You can use ResourceManager's GetString(string, CultureInfo)
overload to get strings in correct language.
BTW。 Global.asax的可能是更好的地方设置的CurrentCulture和的CurrentUICulture。
BTW. global.asax is probably the better place to set CurrentCulture and CurrentUICulture.
修改每彼得的请求,提供了code样品
EDIT Providing code sample per Peter's request
对于初学者来说,我会检测并存储的文化:
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
根据您想要做什么,它可能不是超载最好的功能,在你的情况,我相信在session_start
是更好的(也就是,除非你想给用户切换语言)的能力。结果
另一件事是,我没有域名测试,我使用的查询参数,并没有正确地验证他们,但它只是为例子的目的只。
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.
相反ResourceManager的实例并手动编译你的资源,我只能建议你使用App_LocalResources文件。在解决方案资源管理器中你的项目,你只需要单击鼠标右键,选择添加 - >添加文件夹Asp.Net - > 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.
创建适当命名的资源文件:为Default.aspx.resx,Default.aspx.nl.resx并添加要本地化给他们的内容。在我的情况是:
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:
label1 Some label
label2 Some other label
Default.aspx.nl.resx(对不起,我可怜的荷兰人)
Default.aspx.nl.resx (Sorry for my poor Dutch)
label1 Dit is een etiket
label2 Dit is ander etiket
Default.aspx.pl.resx
Default.aspx.pl.resx
label1 Jakaś tam labelka
label2 Jakaś inna labelka
Default.aspx.vb($ C $后面C)
在code后面可以通过编程修改你的网页内容是这样的:
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.
最后,我们在我们的美丽(和非常有用的)网页:
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>
正如你所看到的,你可以用下面的前pression访问的资源:&LT;%$资源:卷标%&GT;
。这会给你带LABEL1的资源键关联的字符串。重要的是要注意的:=服务器不可选。
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.
心连心。您可能需要阅读上Asp.Net本地化教程的为好。
HTH. You may want to read the tutorial on Asp.Net Localization as well.
这篇关于CurrentThread.CurrentUICulture设置正确,但似乎是asp.net被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!