WPF翻译只需要一种语言? [英] WPF translate only takes one language?

查看:90
本文介绍了WPF翻译只需要一种语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我试图用多语言来创建wpf应用程序。首先,我创建了2个资源词典,第一个是英语,第二个是spansih。具有以下结构:



english.xaml

Hello,

I am trying to craete wpf application with multi language. First I created 2 resource dictionaries the first is english and the second is spansih. The have the following structure:

english.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="UserID">User ID</system:String>
    <system:String x:Key="UserPassword">User Password</system:String>
    <system:String x:Key="Login">Login</system:String>

</ResourceDictionary>





spanish.xaml



spanish.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="UserID">ID de usuario</system:String>
    <system:String x:Key="UserPassword">usuario Contraseña</system:String>
    <system:String x:Key="Login">Iniciar Sesión</system:String>

</ResourceDictionary>





在MainWindow.xaml中。 cs



In MainWindow.xaml.cs

string language = "english";

public MainWindow()
        {
            InitializeComponent();
            SetLanguageDictionary();
        }

private void SetLanguageDictionary()
        {
                ResourceDictionary dict = new ResourceDictionary();
                switch (language)
                { 
                     case "english":
                     dict.Source = new Uri("Resources/english.xaml",UriKind.Relative);
                     break;

                     case "spanish":
                     dict.Source = new Uri("Resources/spanish.xaml",UriKind.Relative);
                     break;

                     default :
                     dict.Source = new Uri("Resources/english.xaml",UriKind.Relative);
                     break;
                }
                this.Resources.MergedDictionaries.Add(dict);
        } 







在App.XAML






In App.XAML

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/english.xaml" />
                <ResourceDictionary Source="Resources/spanish.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>





在我的表格中,我为每个标签执行以下操作



and In my form I do the following for each label

<Label Margin="5" Content="{DynamicResource UserID}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="274"/>





现在的问题是我只获得西班牙语翻译前夕n如果我将语言改为英语。



The problem now that I get only spanish translation even if I change the language to english.

推荐答案

首先,你做错了:手动本地化,一般是.NET,特别是WPF建议基于卫星装配的更加强大,可靠和自动化的全球化和本地化方式。你可以从这里开始: https://msdn.microsoft.com/en -us / library / ms788718%28v = vs.110%29.aspx [ ^ ]。



你正在做的一件坏事是编码立即常数,如英语,西班牙语等。这是不可支持的。很明显,使用这种方法,你必须至少写两次这样的字符串。如果拼错,编译器将永远不会警告你。



不仅你的方法是手动和自烤,它只是非常糟糕,特别是与真实相比时.NET全球化/本地化。您甚至可以在运行时切换文化,并且在某些情况下,可以指定错误文化,这在本地化集合中并未完全实现。然后 fallback 机制将始终为您提供最接近您想要的UI(和非UI资源)。例如,如果你想要缺少en-GB,你可以得到en。



这是文化代码表:https://msdn.microsoft.com/en-us/library/ee825488%28v= cs.20%29.aspx [ ^ ]。



我总是试图鼓励包括我自己在内的人重新发明轮子,但你需要通过将您的发明与已知的方法进行比较来检查您的发明,这些方法可能会或可能不会比您的方法更好。为此,你真的需要阅读文档和其他来源,而不仅仅是编写代码...



-SA
To start with, you are doing wrong thing: "manual" localization, while .NET in general and WPF in particular suggest much more robust, reliable and "automated" way of globalization and localization based on satellite assemblies. You can start here: https://msdn.microsoft.com/en-us/library/ms788718%28v=vs.110%29.aspx[^].

One particular bad thing you are doing is is hard-coding immediate constants like "english", "spanish", etc. This is not supportable. It's apparent that, with such approach, you have to write such string at least twice. If you misspell, the compiler will never warn you.

Not only your approach is "manual" and self-baked, it's just very bad, especially when compared with real .NET globalization/localization. You can switch culture even during run-time, and, in certain cases, specify "wrong" culture, which is not exactly implemented in the set of localizations. Then the fallback mechanism will always get you some UI (and non-UI resources), closest to what you wanted. For example, if you want "en-GB" which is missing, you can get "en".

This is the table of culture codes: https://msdn.microsoft.com/en-us/library/ee825488%28v=cs.20%29.aspx[^].

I always try to encourage people, including myself, to "reinvent the wheel", but you need to check your invention by comparing it with known approaches which may or may not appear to be better than yours. For this purpose, you really need to read documentation and other source, not only write code…

—SA


App.xaml 中删除​​合并的词典。



目前,您已将两个词典合并,最后合并西班牙语版本。因此,西班牙语资源将始终取代英语资源。
Remove the merged dictionaries from App.xaml.

Currently, you have both dictionaries merged, with the Spanish version merged last. As a result, the Spanish resources will always replace the English resources.


这篇关于WPF翻译只需要一种语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆