Tuesday, May 26, 2009

CRM 4.0 Multi Lingual Support in Plug-ins

The following code presents a simple yet very effective way to handle and return multi-lingual error messages form a plug-in.

So how does it work?

Basically I created a base class for Custom Exceptions. All custom exceptions that you use in your code derive from this class. The base class overrides the Exception class Message property and returns the error message from a resources dictionary.

The resource dictionary holds the translations for each local id e.g. (1033, 3082). When an exception is thrown the user local id (LCID) is taken from the current running thread Culture Info object.

The Inner translation dictionary manages the each error message depending on the exception type. This way when the code throws a specific exception the correct error message is fetched from the dictionary.

Enjoy…


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using System.Web.Services.Protocols;

namespace Empty.PlugIn
{
public class Handler : IPlugin
{
#region IPlugin Members
/// <summary>
/// Resource Dictionary
/// </summary>
public static Dictionary<Int32, Dictionary<Type, String>> Resources;
public void Execute(IPluginExecutionContext context)
{
if (Resources == null)
{
Resources = new Dictionary<Int32, Dictionary<Type, String>>();

Dictionary<Type, String> English = new Dictionary<Type, String>();
English.Add(typeof(Exception), "General Exception");
English.Add(typeof(SoapException), "CRM Exception");
English.Add(typeof(InvalidCustomException), "Invalid Custom Exception");
English.Add(typeof(UnKnownPluginException), "UnKnown Plugin Exception");
Resources.Add(1033, English);

Dictionary<Type, String> Spanish = new Dictionary<Type, String>();
Spanish.Add(typeof(Exception), "Excepciףn general");
Spanish.Add(typeof(SoapException), "Excepciףn de CRM");
Spanish.Add(typeof(InvalidCustomException), "Excepciףn personalizado no vבlido");
Spanish.Add(typeof(UnKnownPluginException), "Excepciףn Desconocida Plugin");
Resources.Add(3082, Spanish);
}

try
{
//throw new Exception();
//throw new SoapException();
throw new UnKnownPluginException();
}
catch(Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}

public abstract class CustomException : Exception
{
private Int32 DefaultLanguage = 1033;
/// <summary>
/// override default message prop
/// </summary>
public override string Message
{
get
{
return this.GetResource(this.GetType());
}
}
/// <summary>
/// Get the resource by exception type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
protected string GetResource(Type type)
{
Int32 uiLcid = System.Threading.Thread.CurrentThread.CurrentUICulture.LCID;

if (Resources.ContainsKey(uiLcid))
{
return Resources[uiLcid][type];
}

return Resources[this.DefaultLanguage][type];
}
}

public class InvalidCustomException : CustomException
{
}

public class UnKnownPluginException : CustomException
{
}

#endregion
}
}

No comments: