简体   繁体   English

插件自定义异常消息的翻译类

[英]Translation class for plugin custom exception message

I want to create an class which translates my plugin custom exception messages. 我想创建一个类来翻译我的插件自定义异常消息。

I was able to achieve this for javascript with the below code : 我能够使用以下代码来实现此功能:

  LocalizedLabels: {
    AlertMessages: {
     EmaiTemplateInvitation: {
            '1033': 'Please select an Email Template for invitations and try again.',
            '1031': 'Bitte wählen Sie eine E-Mail-Vorlage für Einladungen aus und versuchen Sie es erneut.'
        },
        TypeForecastInfo: {
            '1033': 'Please type Forecast information.',
            '1031': 'Please type Forecast information.'
        }
    },    
 // call by
 Alert.show(LocalizedLabels.AlertMessages.EmaiTemplateInvitation[Xrm.Page.context.getUserLcid()], null, null, "WARNING", 500, 200);

I want something similar for csharp. 我想要类似的csharp。 Thankyou 谢谢

There is number of methods. 有很多方法。 Here is how to determine user language: 这是确定用户语言的方法:

int GetUserLanguageCode(IPluginExecutionContext context) 
{
  var userSettingsQuery = new QueryExpression("usersettings");
  userSettingsQuery.ColumnSet.AddColumns("uilanguageid", "systemuserid");
  userSettingsQuery.Criteria.AddCondition("systemuserid", 
    ConditionOperator.Equal, 
    context.InitiatingUserId);
  var userSettings = this.orgService.RetrieveMultiple(userSettingsQuery);

  return (int)userSettings.Entities[0]["uilanguageid"];
}

Next thing is you need to store localization somewhere. 接下来,您需要将本地化存储在某个地方。 Options are: 选项有:

1) Static dictionary (simple, but gross - since you're pouting code with static text content) 1)静态字典(简单,但是粗略-因为您要使用静态文本内容来注入代码)

2) Embedded resources (if you're ok to ship localization always along with your plugin code) 2)嵌入式资源(如果您可以始终将本地化与插件代码一起发布)

3) Declaring separate web resource in CRM (say in xml or json format), load it dynamically (in case you need to change localization separate from your plugin release 3)在CRM中声明单独的Web资源(例如xml或json格式),动态加载它(以防您需要将本地化版本与插件版本分开更改)

Then when you need to throw an exception you go just like: 然后,当您需要引发异常时,就像:

int languageCode = GetUserLanguageCode(context);
throw new InvalidPluginExecutionException(GetResources(languageCode, "TypeForecastInfo"));

How to read embedded resources (just a sample, in real life you probably want to cache them in-memory): 如何读取嵌入式资源(实际上,只是一个示例,您可能希望将其缓存在内存中):

public string GetResources(int languageCode, string key)
{
    var serializer = new DataContractJsonSerializer(typeof(Dictionary<string, string>));
    using (var stream = this.GetType().Assembly.GetManifestResourceStream($"Namespace.{languageCode}.json"))
    {
        if (stream != null)
        {
            var map = (Dictionary<string, string>)serializer.ReadObject(stream );
            string value;
            if (map.TryGetValue(key, out value)) 
            {
               return value;
            }
        }
    }

    return result;
}

And say 1033.json 并说1033.json

{
   "TypeForecastInfo": "Foobar"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM