离开页面后如何保持语言更改

时间:2019-06-04 12:46:24

标签: vb.net

我有一个按钮可以将英语更改为法语,反之亦然。该按钮可以正常工作,但是,当我移至应用程序中的新页面时,该语言将重置为第一页上的原始语言。例如,我在主菜单上。我将其更改为法语。一切都很好,但是当我单击一个按钮以打开子菜单时,将打开新页面,并且主菜单变回英语。我需要一种方法来保持语言更改,直到用户将其更改回。

当我更改语言时,我尝试将当前的文化设置为法语,但这不能解决问题。我有.resx文件,其中包含我需要的所有单词(这就是我的翻译方式)。

Private Sub btnChangeLanguage_Click(sender As Object, e As RoutedEventArgs) Handles btnChangeLanguage.Click
        Try
            If CurrentLanguageSelection = enumLanguage.English Then
                CurrentLanguageSelection = enumLanguage.French
                Dim newCulture As CultureInfo = New CultureInfo("fr-CA")
                Thread.CurrentThread.CurrentCulture = newCulture

            ElseIf CurrentLanguageSelection = enumLanguage.French Then
                CurrentLanguageSelection = enumLanguage.English
                Dim newCulture As CultureInfo = New CultureInfo("en-CA")
                Thread.CurrentThread.CurrentCulture = newCulture

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

2 个答案:

答案 0 :(得分:0)

您可以做的是存储一个引用当前所选语言的应用程序设置,然后在构造函数或load事件中引用该设置。类似于:

-t project/tags/*_project1 -T project/trunk/project1

这是My.Settings上的MSDN文章:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/objects/my-settings-object

基本上,您将创建一个新的应用程序设置,它是一个名为SelectedCulture的字符串,并将其设置为等于您的默认区域性(例如“ fr-CA”)。然后,在您的Sub New() InitializeComponent() Try Dim existingCulture As CultureInfo = New CultureInfo(My.Settings.SelectedCulture) Thread.CurrentThread.CurrentCulture = existingCulture Catch ex As Exception MessageBox.Show("There was an issue loading the existing culture. The language will be the default language.", "Error Loading Culture", MesssageBoxButtons.Ok, MessageBoxIcons.Warning) End Try End Sub 单击事件中,您将在重置线程的CurrentCulture之前更新设置。

答案 1 :(得分:0)

事实证明,我还有其他未更改的全局变量。我将它们添加到该方法中,并且效果很好。感谢您的帮助

Private Sub btnChangeLanguage_Click(sender As Object, e As RoutedEventArgs) Handles btnChangeLanguage.Click
        Try
            If CurrentLanguageSelection = enumLanguage.English Then
                CurrentLanguageSelection = enumLanguage.French
                Dim newCulture As CultureInfo = New CultureInfo("fr-CA")
                Thread.CurrentThread.CurrentCulture = newCulture
                gCurrentStrLanguage = "FR"

            ElseIf CurrentLanguageSelection = enumLanguage.French Then
                CurrentLanguageSelection = enumLanguage.English
                Dim newCulture As CultureInfo = New CultureInfo("en-CA")
                Thread.CurrentThread.CurrentCulture = newCulture
                gCurrentStrLanguage = "EN"

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub