以编程方式更改语言就像英语到法语 wpf

Posted

技术标签:

【中文标题】以编程方式更改语言就像英语到法语 wpf【英文标题】:Change Language programmatically Like english to french wpf 【发布时间】:2015-07-08 12:14:02 【问题描述】:

我找到了一些教程并将两个资源文件添加到我的项目的 Properties 文件夹中,并将它们命名为“Resources.tr-TR.resx”和“Resources.en-US.resx”,默认情况下我的“Resources.resx”是文件也在那里。我将访问修饰符设置为“公共”。我在我的 xaml 代码中调用它,例如

Content="x:Static p:Resources.MainWindow"

在我的文件中它有值,最初我可以看到它读取正确。我有一个菜单按钮,可以更改语言和我编写的操作方法

private void englishLanguageMenuButton_Click(object sender, RoutedEventArgs e)

      System.Threading.Thread.CurrentThread.CurrentUICulture = new   System.Globalization.CultureInfo("en-US");

或者在另一个动作中说

private void macedonianLanguageMenuButton_Click(object sender, RoutedEventArgs e)

    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("tr-TR");

但是系统不工作。缺什么 ?我可以动态更改语言吗?或者如何动态更改 resx 文件

谢谢

【问题讨论】:

【参考方案1】:

试试这个

将此代码添加到您的应用程序中

/// <summary>
    /// Wraps up XAML access to instance of WPFLocalize.Properties.Resources, list of available cultures, and method to change culture
    /// </summary>
    public class CultureResources
    
        /// <summary>
        /// Backing filed for provider
        /// </summary>
        private static ObjectDataProvider provider;

        /// <summary>
        /// Gets Resource provider
        /// </summary>
        public static ObjectDataProvider ResourceProvider
        
            get
            
                if (provider == null)
                
                    provider = (ObjectDataProvider)App.Current.FindResource("Resources");
                

                return provider;
            
        

        /// <summary>
        /// Change the current culture used in the application.
        /// If the desired culture is available all localized elements are updated.
        /// </summary>
        /// <param name="culture">Culture to change to</param>
        public static void ChangeCulture(CultureInfo culture)
        
            ////remain on the current culture if the desired culture cannot be found
            //// - otherwise it would revert to the default resources set, which may or may not be desired.

            V_Parcel.Properties.Resources.Culture = culture;
            ResourceProvider.Refresh();
        

        /// <summary>
        /// The Resources ObjectDataProvider uses this method to get an instance of the WPFLocalize.Properties.Resources class
        /// </summary>
        /// <returns>Returns resource instance</returns>
        public V_Parcel.Properties.Resources GetResourceInstance()
        
            return new V_Parcel.Properties.Resources();
        
    

添加 XAML 文化字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:resource="clr-namespace:Cultures">

    <!-- Resources ODP contains the current instance of the WPFLocalize.Properties.Resources class.
       Used in bindings to get localized strings and automatic updates when the culture is updated -->
    <ObjectDataProvider x:Key="Resources" ObjectType="x:Type resource:CultureResources" MethodName="GetResourceInstance" />

</ResourceDictionary>

在您的 App.XAml 中

 <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="CultureDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

绑定应该是这样的

Content="Binding ShowTerminalName,Source=StaticResource Resources"

在文化变革活动中写下这个

Thread.CurrentThread.CurrentCulture = new CultureInfo(currentCulture);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentCulture);

                    CultureInfo cultureInfo = new CultureInfo(currentCulture);
                    CultureResources.ChangeCulture(cultureInfo);

.resx 访问修饰符应该是公共的

【讨论】:

U 在资源右侧写“ShowTerminalName”,并将它们命名为“Resource.en-US.resx”和“Resource.tr-TR.resx”?我是这样做的,但它不起作用我的意思是在运行时它不起作用 我在两个资源中都有一个名为 ShowTerminalName 的资源。它在运行时对我来说很好。 这个,我会再检查一遍现在我还有其他事情要做:/ 谢谢你 Justine,非常简单的解释,包含所有需要的步骤。

以上是关于以编程方式更改语言就像英语到法语 wpf的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式更改应用程序语言 - UWP

在 iOS 中以编程方式更改应用程序中的语言

以编程方式使用 C# 更改网格面板边框颜色 - WPF

如何以编程方式更改 WPF 中的数据网格行颜色?

WPF DataGrid - 以编程方式将单元格设置为编辑模式

以编程方式将控件添加到 WPF 表单