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

时间:2015-07-08 12:14:03

标签: c# wpf localization cultureinfo culture

我找到了一些教程,并将两个资源文件添加到我项目的Properties文件夹中,并命名为" Resources.tr-TR.resx"和#34; Resources.en-US.resx"作为默认我的" Resources.resx"是文件也在那里。我将Access Modifier设置为" Public"。我在我的xaml代码中称之为

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");
}

或在另一个动作中让我们说

{{1}}

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

由于

1 个答案:

答案 0 :(得分: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 CultureDictionary

<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访问修饰符应该是公开的