在运行时本地化资源

时间:2014-04-25 14:37:01

标签: .net wpf vb.net localization

我设法在运行时翻译我的WPF应用程序,但.resx中的字符串不会翻译。

我有一个Login.resx和Login-pt.resx,它们都有一个带有不同值的“prueba”条目,但它们总是加载默认的resx(Login.resx)。

Private Sub btnAr_Click (sender As System.Object, e As System.EventArgs) Handles btnAr.Click
    Thread.CurrentThread.CurrentCulture =
        CultureInfo.CreateSpecificCulture("es-AR")
    Thread.CurrentThread.CurrentUICulture = New CultureInfo("es-AR")
    ChangeLanguage("es-AR")
    MessageBox.Show(Login.Prueba)
End Sub


Private Sub btnPt_Click(sender As System.Object, e As System.EventArgs) Handles btnPt.Click
    Thread.CurrentThread.CurrentCulture =
      CultureInfo.CreateSpecificCulture("pt-BR")
    Thread.CurrentThread.CurrentUICulture = New CultureInfo("pt-BR")
    ChangeLanguage("pt-BR")
    MessageBox.Show(Login.Prueba)
End Sub

Private Sub ChangeLanguage(ByVal lang As String)
    For Each c As Control In Me.Controls
        Dim resources As ComponentResourceManager = New     ComponentResourceManager(GetType(LoginUsuario))
        resources.ApplyResources(c, c.Name, New CultureInfo(lang))
    Next c
End Sub

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的资源名称 Login-pt.resx 是错误的。它应该是 Login.pt.resx Login.pt-BR.resx