2011-04-01 118 views
0

在切换语言时,我在\ bin文件夹中的资源文件中从正确的strings.txt中获取值,并付出了努力。asp.net从 bin文件夹文件中的文件本地化文本

[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") '"Alles over trouwen, voor je perfecte bruiloft" 
    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 

在我的bin文件夹我有:

斌\ strings.txt
斌\ 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 

现在,所有的文件似乎正确创建。

但是,当我访问www.domain.com时,bin \ strings.txt中的值被使用,而不是(像我期望和渴望的那样),bin \ en \ strings.en.txt中的值

为什么?

+0

好奇... www.domain.nl是否有效? – Naraen 2011-04-01 21:27:49

回答

0

我在.NET 4.0 Windows 2008计算机上试过了您的代码片段。它按照你想要的那样工作。

我能想到的唯一一件事......我试过的电脑上安装了Windows MUI。想知道这是否是一个因素?

+0

我在Windows 7上运行,只有英文,所以我希望这部分工作至少。我调试了一下,发现在亩InitializeCulture方法:保护覆盖Sub InitializeCulture() SetLanguage(Request.Url.ToString) End Sub 当我检查当前的System.Threading.Thread.CurrentThread.CurrentUICulture.ToString它的等于“en”,所以看起来文化是正确设置的,只有不正确的strings.dll被使用....这有助于进一步帮助我吗? :) – Peter 2011-04-03 14:13:49

+0

但还有更奇怪的事情发生: 此外,我有这在我的default.aspx ”/ > 当我接近www.domain.com时,Literal1控件显示来自“App_LocalResources \ default.aspx.en.resx”的值 而当我检查当前的System.Threading.Thread.CurrentThread.CurrentUICulture.ToString时等于“en”,所以看起来文化是正确设置的。 所以当使用www.domain.com时,使用了WRONG strings.dll,但是正确的.resx文件......对我来说似乎有点不一致! – Peter 2011-04-03 21:20:40