26.05.2009, 17:05 | #1 |
Участник
|
mscrm4ever: CRM 4.0 Multi Lingual Support in Plug-ins
Источник: http://mscrm4ever.blogspot.com/2009/...t-in-plug.html
============== 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 /// /// Resource Dictionary /// public static Dictionary Resources; public void Execute(IPluginExecutionContext context) { if (Resources == null) { Resources = new Dictionary(); Dictionary English = new Dictionary(); 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 Spanish = new Dictionary(); 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; /// /// override default message prop /// public override string Message { get { return this.GetResource(this.GetType()); } } /// /// Get the resource by exception type /// /// /// 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 } } Источник: http://mscrm4ever.blogspot.com/2009/...t-in-plug.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|