资源管理器使用错误的 UICulture 来提供内容!始终回归中立文化
我在很多论坛上问过,但似乎没有人能够帮助我!
default.aspx:
<asp:Literal ID="Literal1" Text="<%$Resources:lookingfor %>" runat="server"/>
default.aspx.vb:
Shared rm As ResourceManager = HttpContext.Current.Application("RM")
Protected Overrides Sub InitializeCulture()
Dim cultureInfo As Globalization.CultureInfo = Globalization.CultureInfo.CreateSpecificCulture("en")
Threading.Thread.CurrentThread.CurrentCulture = cultureInfo
Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo
'Here I log the value of currentculture
ReportError("default.aspx:InitializeCulture.CurrentCulture", System.Threading.Thread.CurrentThread.CurrentCulture.ToString)
ReportError("default.aspx:InitializeCulture.CurrentUICulture", System.Threading.Thread.CurrentThread.CurrentUICulture.ToString)
'log file shows that:
'CurrentCulture = "en-US"
'CurrentUICulture= "en-US"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'again logging the currentculture:
ReportError("default.aspx:page_load.CurrentCulture", System.Threading.Thread.CurrentThread.CurrentCulture.ToString)
ReportError("default.aspx:page_load.CurrentUICulture", System.Threading.Thread.CurrentThread.CurrentUICulture.ToString)
'log file shows that:
'CurrentCulture = "en-US"
'CurrentUICulture= "en-US"
Me.Page.Title = rm.GetString("homewelcome")
End If
End Sub
global.asax:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application("RM") = New ResourceManager("strings", Assembly.Load("strings"))
End Sub
\bin 文件夹: 在我的 bin 文件夹中,我有:
bin\strings.txt bin\nl\strings.nl.txt bin\en\strings.en.txt
我像这样生成 dll:
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
现在,当我加载 default.aspx 时,
Literal1 控件显示:“您在寻找什么?” 这是 App_LocalResources\default.aspx.en.resx 中的值,因此是正确的。
但是页面标题 (Me.Page.Title = rm.GetString("homewelcome")) 显示的是 bin\strings.txt 中的值! 这当然是不正确的,因为我希望它显示 bin\en\strings.txt 中的值,
因此资源管理器忽略区域性,而文字使用正确的区域性!
我在这里缺少什么?
Possible Duplicate:
CurrentThread.CurrentUICulture is set correctly but seems to be ignored by asp.net
I've asked on many forums, but no one seems to be able to help me!
default.aspx:
<asp:Literal ID="Literal1" Text="<%$Resources:lookingfor %>" runat="server"/>
default.aspx.vb:
Shared rm As ResourceManager = HttpContext.Current.Application("RM")
Protected Overrides Sub InitializeCulture()
Dim cultureInfo As Globalization.CultureInfo = Globalization.CultureInfo.CreateSpecificCulture("en")
Threading.Thread.CurrentThread.CurrentCulture = cultureInfo
Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo
'Here I log the value of currentculture
ReportError("default.aspx:InitializeCulture.CurrentCulture", System.Threading.Thread.CurrentThread.CurrentCulture.ToString)
ReportError("default.aspx:InitializeCulture.CurrentUICulture", System.Threading.Thread.CurrentThread.CurrentUICulture.ToString)
'log file shows that:
'CurrentCulture = "en-US"
'CurrentUICulture= "en-US"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'again logging the currentculture:
ReportError("default.aspx:page_load.CurrentCulture", System.Threading.Thread.CurrentThread.CurrentCulture.ToString)
ReportError("default.aspx:page_load.CurrentUICulture", System.Threading.Thread.CurrentThread.CurrentUICulture.ToString)
'log file shows that:
'CurrentCulture = "en-US"
'CurrentUICulture= "en-US"
Me.Page.Title = rm.GetString("homewelcome")
End If
End Sub
global.asax:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Application("RM") = New ResourceManager("strings", Assembly.Load("strings"))
End Sub
\bin folder:
In my bin folder I have:
bin\strings.txt bin\nl\strings.nl.txt bin\en\strings.en.txt
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 when I load default.aspx
The Literal1 control shows: 'What are you looking for?'
this is the value from App_LocalResources\default.aspx.en.resx, so that is correct.
But the Page Title (Me.Page.Title = rm.GetString("homewelcome")) shows the value from bin\strings.txt!
That is ofcourse incorrect as I would want it to show the value from bin\en\strings.txt
So the resource manager is ignoring the culture, whereas the Literal is using the CORRECT culture!
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将字符串文件全部放在同一个文件夹中。我不知道你的设置是什么,但默认情况下,我认为 ResourceManager 会尝试在同一文件夹中查找资源,如果找不到合适的资源,则会回退到默认资源,在你的情况下是 strings.txt。
Why don't you put the strings files all in the same folder. I don't know what are your setup but by default I think the ResourceManager try to look for resource in the same folder and if it doesn't find a appropriate one fall back to the default one which in your case is strings.txt.