Adding Localization in CRM 2013 USD

The blog explains how to implement localization in custom made hosted controls of USD. For implementing localization we need to follow the below mentioned steps

1. Create a project and add all the required the required strings or messages to be localized as resource files. Following screenshot describes the implementation.

ResourceProject

The name should of the format Strings.{lang code}. For example for English it should be Strings.en-US.resx and for portugal it should be Strings.pt-BR.resx. Create a resource file for neutral culture as well.

2. Now create another project. The purpose of this project is to identify dynamically the current culture profile of the user, load the corresponding resource dll and then to translate the text passed to it. You need to add the reference of the project created in first step. Mentioned below is the pseudo code of the classes used in it.

a) Create an interface ITranslationProvider with the mentioned below declaration.

ITranslationProvider

b) Firstly class should implement the interface “ITranslationProvider” and should have a member variable of the above created interface. It should also have a method to return the translated text as per the loaded resource assembly. Mentioned below is the screenshot for the same.

 

TranslationManager

 

Refer below for the constructor and the translation function.

private static TranslationManager _translationManager;

private ITranslationProvider translationProvider;

/// <summary>
/// Gets or sets the translation provider.
/// </summary>
/// <value>The translation provider.</value>
public ITranslationProvider TranslationProvider
{
get
{
return this.translationProvider;
}
set
{
this.translationProvider = value;
}
}

/// <summary>
/// Gets or sets the current language.
/// </summary>
/// <value>The current language.</value>
public CultureInfo CurrentLanguage
{
get
{
return Thread.CurrentThread.CurrentUICulture;
}
set
{
if (value != Thread.CurrentThread.CurrentUICulture)
{
Thread.CurrentThread.CurrentUICulture = value;
}
}
}

/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static TranslationManager Instance
{
get
{
if (_translationManager == null)
_translationManager = new TranslationManager();

return _translationManager;
}
}

/// <summary>
/// Translates the specified key.
/// </summary>
/// <param name=”key”>The key.</param>
/// <returns></returns>
public object Translate(string key)
{
if (TranslationProvider == null)
{
TranslationManager.Instance.TranslationProvider = new ResourceTranslationProvider(“Cielo.USD.LocalizationResources.Resources.Strings”, typeof(LocalizationResources.Resources.Strings).Assembly);
string value = System.Configuration.ConfigurationManager.AppSettings[“LanguageCulture”];
if(value == null || value == “”)
{
value = “pt-br”;
}

TranslationManager.Instance.CurrentLanguage = new System.Globalization.CultureInfo(value);
}

if (TranslationProvider != null)
{
object translatedValue = TranslationProvider.Translate(key);
if (translatedValue != null)
{
return translatedValue;
}
}
return string.Format(“{0}”, key);
}
}

Refer to the name of the localization resource project assembly dll, which is used wile making the object of TranslationProvider.

c) Now create a new file TranslationExtention that should invoke the above mentioned class for translation

public static class TranslationExtensions
{
/// <summary>
/// Converts a string to the localized string.
/// </summary>
/// <param name=”sourceString”>The source string.</param>
/// <returns></returns>
public static string ToLocalizedString(this string sourceString)
{
object obj = TranslationManager.Instance.Translate(sourceString);
if (obj == null)
{
return sourceString;
}
else
{
return obj.ToString();
}
}
}

d) Similar files needs to be made for xaml conversion as well.

e) Build the above mentioned project and add the required dll’s in all the hosted control projects. Now use the below mentioned code snippet for implementing translation in both cs and xaml files.

XamlAssembly

XamlAssemblyTextLabel

“SalesProcedureText” is the key of the text label defined in resource file.

CSTextLabel

Advertisements