【问题标题】:asp.net localize text from files in \bin folder filesasp.net 本地化 \bin 文件夹文件中文件的文本
【发布时间】:2011-04-01 18:36:29
【问题描述】:

在切换语言时,我正在努力从我的 \bin 文件夹中的资源文件中的正确 strings.txt 中获取值。

[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 文件夹中:

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

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

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

为什么?

【问题讨论】:

  • 好奇 ... www.domain.nl 有效吗?

标签: vb.net resources localization globalization


【解决方案1】:

我在 .NET 4.0 Windows 2008 计算机上尝试了您的代码 sn-p。它按您的意愿工作。

我能想到的唯一一件事……我尝试过的计算机安装了 Windows MUI。想知道这是否是一个因素?

【讨论】:

  • 我在 Windows 7 上运行并且这里只有英语,所以我希望这部分至少可以工作。我又调试了一下,发现在 mu InitializeCulture 方法中: Protected Overrides Sub InitializeCulture() SetLanguage(Request.Url.ToString) End Sub 当我检查当前 System.Threading.Thread.CurrentThread.CurrentUICulture.ToString 时它等于“ en”,所以似乎文化设置正确,只是没有使用正确的 strings.dll ......这有助于进一步帮助我吗? :)
  • 但是发生了更多奇怪的事情:我的 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,但 CORRECT .resx 文件……对我来说似乎有点太不一致了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-10
  • 2021-03-03
  • 2023-03-29
  • 2022-10-02
  • 1970-01-01
  • 2011-08-27
相关资源
最近更新 更多