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

6

17

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 TextBlock. Should I separate the hyphen from "Organization -" and put it in a separate label?

How do I do that?

Germanize answered 4/10, 2010 at 11:10 Comment(1)
'Organizzazione' is not french ! -> 'organisation'Trudytrue
T
12

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)

Trudytrue answered 27/9, 2012 at 10:49 Comment(3)
Well Your approach is not working. I am facing this error A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Markup.StaticExtension' threw an exception.' Line number '30' and line position '26'.Earring
I just created this project and it's working like a charm (vs 2013, net 4.5 and net 4 as well). Did you add the 'OrganizationText' Name and value in the string.resx ? (answer edited with more details)Trudytrue
Alright. I found that resource should be public. It is internal by default.Earring
T
2

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

Tinsmith answered 6/10, 2010 at 20:46 Comment(1)
For changing at runtime, check out LocalizationExtension.Graubert
C
1

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.

Circumcision answered 4/10, 2010 at 11:42 Comment(0)
P
1

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

Pneumatophore answered 26/5, 2014 at 19:34 Comment(0)
B
1

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.

Bombacaceous answered 20/4, 2020 at 14:45 Comment(0)
P
0

I had the same issue with own WPF application and decided to write a lightweight localization library. It allows translating xaml resource dictionary files and switching between supported languages at runtime.

You can look into Armat.Localization GitHub repository for more details and refer to "armat.localization.wpf" NuGet package in WPF applications. Cloning / Building the source code with appropriate demo application will give you enough understanding about how the Armat.Localization library works.

The main idea is following:

  • Extract all localizable contents from WPF controls into WPF Resource Dictionary file(s)
  • Make Resource Dictionaries localizable by changing the root xaml element to "Armat.Localization.Wpf.LocalizableResourceDictionary"
  • Use the Localization Designer (from the same repo) to create translation file for all supported languages
  • Provide a language selector in WPF application for switching between supported languages

Markdown files in Localization.Core and Localization.Wpf projects will guide you through the general usage patterns, and the Localization.Demo application will serve you as a sample localizable WPF project.

Preferable answered 10/10, 2023 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.