【问题标题】:Translation class for plugin custom exception message插件自定义异常消息的翻译类
【发布时间】:2019-05-17 10:48:29
【问题描述】:

我想创建一个类来翻译我的插件自定义异常消息。

我能够使用以下代码为 javascript 实现这一点:

  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);

我想要 csharp 类似的东西。谢谢你

【问题讨论】:

  • 所以基本上你想传递 int 代码并想要返回该代码的字符串?
  • 不,假设我有一个异常: throw new Exception("something went wrong") 现在我想要一个类,它包含“something went wrong”的翻译,我可以这样称呼它:throw new Exception(Class.SOMETHING_WENT_WRONG),它根据当前用户登录的语言选择文本,如上面的 javascript。

标签: c# dynamic dynamics-crm dynamics-365 language-translation


【解决方案1】:

有很多方法。以下是确定用户语言的方法:

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"];
}

接下来你需要将本地化存储在某个地方。选项是:

1) 静态字典(简单但粗俗 - 因为您正在用静态文本内容噘嘴代码)

2) 嵌入式资源(如果您愿意始终随插件代码一起提供本地化)

3) 在 CRM 中声明单独的 Web 资源(例如以 xml 或 json 格式),动态加载它(以防您需要更改与插件版本分开的本地化

然后当你需要抛出异常时,你可以这样:

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

如何读取嵌入式资源(只是一个示例,在现实生活中您可能希望将它们缓存在内存中):

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;
}

然后说1033.json

{
   "TypeForecastInfo": "Foobar"
}

【讨论】:

  • 您好,我知道所有的可能性,以及如何获取用户语言。我想使用嵌入式资源,但在资源中,我无法使这样的功能可以工作异常(lcid,“消息”)。你能帮我解决这个问题吗? ,我可以使用两种资源,一种用于英语,一种用于德语,并使用 if 在调用异常之前,但我不想使用和做你写的事情。
  • 你能给我这个函数的实现吗:GetResources(languageCode, "TypeForecastInfo")
  • 感谢它的好答案,但我问的是使用 .resx 文件的资源类。我制作了两个 resx 文件,strings.resx 和 strings.en.resx,我使用 'Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); Console.WriteLine(Properties.strings.Hello);但它采用的是原始值而不是翻译后的值。
猜你喜欢
  • 2021-03-17
  • 2016-11-03
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2015-11-04
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多