AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 26.05.2009, 17:05   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
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, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
mscrm4ever: CRM 4.0 Concatenating Fields Plug-In Blog bot Dynamics CRM: Blogs 0 14.02.2009 14:05
mscrm4ever: CRM 4.0 Creating Interactive Plug-ins Blog bot Dynamics CRM: Blogs 0 21.01.2009 10:05
mscrm4ever: CRM 4.0 Supported Multi Select (Picklist) Control Blog bot Dynamics CRM: Blogs 0 25.12.2008 15:05
Microsoft Dynamics CRM Team Blog: Part Deux: Storing Configuration Data for Microsoft Dynamics CRM Plug-ins Blog bot Dynamics CRM: Blogs 0 15.11.2008 09:07
Microsoft Dynamics CRM Team Blog: Storing Configuration Data for Microsoft Dynamics CRM Plug-ins Blog bot Dynamics CRM: Blogs 0 24.10.2008 22:05
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 09:20.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.