問題描述
在切換語言時,我正在努力從我的 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 的更好位置.
編輯根據彼得的要求提供代碼示例
全球.asax
首先,我會檢測和存儲文化:
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
我只能建議您使用 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
Default.aspx.vb(隱藏代碼)
在后面的代碼中,您可以像這樣以編程方式修改網頁的內容:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 處理 Me.LoadLabel2.Text = Me.GetLocalResourceObject("label2")結束子
出于許多原因,我不推薦這種方法(尤其是它不應該在頁面加載處理程序中使用),但有時需要它.
默認.aspx
終于到了我們漂亮(而且非常有用)的網頁:
<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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!