Click here to Skip to main content
15,885,172 members
Articles / Desktop Programming / WPF

Globalization in WPF using ResourceDictionary

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
31 Jan 2013CPOL4 min read 71.5K   2.7K   38  
Multilingual application using ResourceDictionary in WPF.
using System;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Globalization;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using WPF_Globalization.ViewModel;

namespace WPF_Globalization
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		protected override void OnStartup(StartupEventArgs e)
		{
			SetImportCatalog();
			SetLanguage();
		}

		private void SetImportCatalog()
		{
			try
			{
				string path = AppDomain.CurrentDomain.BaseDirectory;
				DirectoryCatalog catalog = new DirectoryCatalog(path);
				CompositionContainer container = new CompositionContainer(catalog);
				container.ComposeParts(BaseModel.Instance.ImportCatalog);

			}
			catch (Exception ex)
			{

			}
		}

		private void SetLanguage()
		{
			try
			{
				string cultureCode = "en-US";
				CultureInfo cultureInfo = new CultureInfo(cultureCode);
				Thread.CurrentThread.CurrentCulture = cultureInfo;
				Thread.CurrentThread.CurrentUICulture = cultureInfo;
				var dictionary = (from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList
								  where d.Metadata.ContainsKey("Culture")
								  && d.Metadata["Culture"].ToString().Equals(cultureCode)
								  select d).FirstOrDefault();
				if (dictionary != null && dictionary.Value != null)
				{
					this.Resources.MergedDictionaries.Add(dictionary.Value);
				}
			}
			catch (Exception ex)
			{
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Reputed IT Company
India India
Having 9+ years of experience in Microsoft.Net Technology.
Experience in developing applications on Microsoft .NET Platform ( Asp.Net, WPF, Silverlight, Windows Phone 7/8).
Experience and knowledge of software design methodologies (Agile), object oriented design, and software design patterns (MVVM).
Experience in Developing android mobile application using Xamarin (mono for android) framework.

http://hirenkhirsaria.blogspot.com/

Comments and Discussions