WPF Localization in XAML, what is the simple, easy and elegant way of doing it?

后端 未结 5 1156
既然无缘
既然无缘 2021-02-07 10:14

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 real

相关标签:
5条回答
  • 2021-02-07 10:35

    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)

    0 讨论(0)
  • 2021-02-07 10:45

    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'.

    0 讨论(0)
  • 2021-02-07 10:46

    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.

    0 讨论(0)
  • 2021-02-07 10:47

    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

    0 讨论(0)
  • 2021-02-07 10:55

    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.

    0 讨论(0)
提交回复
热议问题