2012-05-19

Articles

Localizer

 

The localizer helps you manage your localized application. You can just switch the language or replace the resource by the culture.

 

 

Usage

Possibility 1. With satellite assemblies build beside the excutable like .\en-US\ApplicationName.resources.dll

using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows;

namespace DW.SharpTools.SatelliteDemo
{
	public partial class App : Application
	{
		public void SwitchLanguage(CultureInfo newCulture)
		{
			// Set new culture
			var result = Localizer.SetCulture(newCulture);
			if (result.Exception != null)
				Debug.WriteLine(result.Exception);
	
			// Refresh the resources
			Localizer.RefreshResources(Resources.MergedDictionaries);
		}
	}
}

 

Possibility 2. With build in translated resources. The executable contains all resources, the original and all translations like Strings.xaml and Strings_en-US.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="Application_Name">Localization Demo</System:String>
	<System:String x:Key="Hello_World">Hello World</System:String>
	<System:String x:Key="Switch_Language">Switch language to:</System:String>
	<System:String x:Key="English">English</System:String>
	<System:String x:Key="German">German</System:String>
	<System:String x:Key="Language_Changed_Ok">The language is switched successfully</System:String>

</ResourceDictionary>
<Application x:Class="DW.SharpTools.SimpleDemo.App"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 StartupUri="MainWindow.xaml">
	<Application.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="/DW.SharpTools.SimpleDemo;component/Resources/Strings.xaml" />
			</ResourceDictionary.MergedDictionaries>
		</ResourceDictionary>
	</Application.Resources>
</Application>
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;

namespace DW.SharpTools.SimpleDemo
{
	public partial class App : Application
	{
		protected override void OnStartup(StartupEventArgs e)
		{
			// Register resource dictionaries for each culture
			Localizer.RegisterCultureResources(new CultureInfo("de-DE"),
											   new Uri("pack://application:,,,/Resources/Strings_de-DE.xaml", UriKind.Absolute));
			Localizer.RegisterCultureResources(new CultureInfo("en-US"),
											   new Uri("pack://application:,,,/Resources/Strings_en-US.xaml", UriKind.Absolute));

			// First initializing of the resources for the CurrentCulture
			Localizer.InitializeResources(Resources.MergedDictionaries);

			base.OnStartup(e);
		}

		public void SwitchLanguage(CultureInfo newCulture)
		{
			// Set new culture
			var result = Localizer.SetCulture(newCulture);
			if (result.Exception != null)
				Debug.WriteLine(result.Exception);

			// Refresh the resources by the registered (throws the old and adds the new)
			Localizer.RefreshResources(Resources.MergedDictionaries, result.OldCulture, newCulture);
		}
	}
}