I have a very common question. What is the best way to do localization in a WPF app. Well, I searched in SO and Binged a lot too. But I could not find any pointers which is really simple and elegant.
Assume that I have to display in UI something like:
In English: Orgnanization - Beoing
In French: Organizzazione - Beoing
<StackPanel Orientation="Horizontal">
<TextBlock Text="Organization -"></TextBlock>
<TextBlock Text="{Binding Path=OrganizationName}">
</TextBlock>
</StackPanel>
Basically, I need to assign the localized text to Organization Label TextB开发者_如何学Pythonlock. Should I separate the hyphen from "Organization -" and put it in a separate label?
How do I do that?
Create a project WpfApplication1
Create a folder 'Localization'
Add a resource file (Strings.resx) in 'localization\' and add the string 'OrganizationText' and value 'Organisation'
When you compile, a designer.cs file is generated. Unfortunatly, all methods in this file are internal and we need public acces to enable string references from wpf. To do that, replace the .resx 'custom tool' with 'PublicResXFileCodeGenerator' (>=vs2008) and build again. Use this xaml in MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:localization="clr-namespace:WpfApplication1.Localization"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Static localization:Strings.OrganizationText}"/>
</StackPanel>
</Window>
To add the ' - ', you must use multibinding (see here)
I Bind all the labels, etc. in my application to an object that I use for displaying the different languages. I store all the strings in a database and just retrieve the proper strings based on the language the user has selected. This has the advantage of instantly updating during run-time. This way the user does not need to restart the application like you would if you chose to make the windows 'Localizable'.
We have an application that can switch UI languages at runtime and has the ability to use new UI languages by copying the appropriate resources to a certain directory. Using some sort of compiled resoures for this is way too inflexible in terms of distributuon etc. So we have our language resources in a ResourceDictionary as System:Strings - one ResourceDictionary in a separate XAML file for each language. The XAML files are tagged as Content in VS and copied. You can use them as DynamicResources in XAML or via a Localizer instance in C#. This concept has proofed very useful in our application.
I made a complete localisation solution (incl. translation editor) that also supports XAML translation through a number of MarkupExtensions. It uses a dictionary with all the translated texts (including singular/plural forms) that can be accessed through MarkupExtensions from XAML or regular methods from other code. The MarkupExtensions also support changing the language at runtime and even update when the underlying dictionary file is modified.
Other features are localised date/time and number formatting and typographic helpers.
I'm using this library successfully in a few applications and plan to employ it in more apps in the future.
http://dev.unclassified.de/source/txlib
Please go through this article and download the example: https://www.codeproject.com/Articles/249369/Advanced-WPF-Localization?msg=5715971#xx5715971xx
All work has been done. You've just implement following the doc and example project. Very little work.
精彩评论