Introduction

This article introduces how to run a WPF application in difference languages.
We will go through how to create a multilingual application. Sometimes question arises
as to how to create a single version of an application that can be used in multiple languages (different culture).
ResourceDictionary comes in place, and plays a role in running WPF applications in different languages.

Background

ResourceDictionary is based on XML and takes advantage of the globalization support defined in the XML specification. We can create multiple
resource files for each language and add at the root level (App.xaml) to implement in a whole application.

Using the code

Creating resources

Right click on the WPF project and select Add New Item, and select UserControl from list of items in
the opened dialog.

Convert UserControl to ResourceDictionary.

A question arises in our mind that why do we need to add a
UserControl and convert it into a ResourceDictionary instead of adding a
ResourceDictionary directly.

The answer is we are going to use MEF (Import/Export) classes in the next step.

Give a proper name to the ResourceDictionary page based on the language, for example: EnglishLanguage.xaml, and write
a string resource like this:

To use global file resources you have to set DynamicResource for
the local file resource you need to set with StaticResource.

In this application I have created a demo for English and French languages, you can create more resource files based on your requirements.

The same way, we add a ReourceDictionary file for other languages, for example, FrenchLanguage.xaml, like this:

<ResourceDictionaryx:Class="WPF_Globalization.Resources.FrenchLanguage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:s="clr-namespace:System;assembly=mscorlib"mc:Ignorable="d"><s:Stringx:Key="keyEmployeeName">Nom de l'employé:</s:String><s:Stringx:Key="keyAddress">adresse:</s:String><s:Stringx:Key="keyCountry">pays:</s:String><s:Stringx:Key="keyState">état:</s:String><s:Stringx:Key="keyCity">ville:</s:String><s:Stringx:Key="keyPhone">Numéro de téléphone:</s:String><s:Stringx:Key="keyDesignation">désignation:</s:String></ResourceDictionary>

Using MEF

Microsoft .NET Framework has a System.ComponentModel.Composition
namespace which provides classes that constitute the core of MEF (Manage Extensibility Framework). For more details, visit MSDN.

What is MEF?

The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability, and testability of large applications.

It allows application developers to discover and use extensions with no configuration required. By using MEF, developers can easily encapsulate code
and avoid fragile hard dependencies.

Features of MEF

MEF components (classes, methods, properties) specify both its dependencies (Imports) and capabilities (Exports) that
are discovered by the runtime. When an object is created, the MEF composition engine satisfies its imports with what is available from other objects. It provides exports, an object that satisfies imports.

There are a list of attributes available in MEF, apart from them I have used
the following attributes in this application:

ExportAttribute

ImportAttribute

ImportManyAttribute

ImportAttribute specifies that a property, field, or parameter should be populated with matching exports. It will import a list of operations.
ExportAttributespecifies that a type, property, field, or method provides a particular export. Any export declared with a matching contract
will fulfill this import.

Open the EnglishLanguage.xaml.cs file and write the following to export this class (resource):

The above code snippet imports all classes from different assemblies that have
the matching type ResourceDictionary.

Composition Container: it is the core of MEF. It is used to discover parts (objects) by using
a composable part catalog. A catalog can be any given type from hosting (like
DirectoryCatalog, AssemblyCatalog, AggregateCatalog, etc.).

Create a Singleton class and add a property for the ImportModule class.

On change of the selected language, first find out the ResourceDictionary of
the selected language culture from ResourceDictionaryList. Then add
the Selected Resource into Application Resources and set CurrentCulture as
the selected language culture. Last, set the application main window language using
the System.Windows.Markup.XmlLanguage.GetLanguage method.

That's it.

Points of interest

This way developers can create a ResourceDictionary for each language and set the resources for the whole application using MEF.