Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Umbraco如何按语言获取词典项?_C#_Umbraco - Fatal编程技术网

C# Umbraco如何按语言获取词典项?

C# Umbraco如何按语言获取词典项?,c#,umbraco,C#,Umbraco,在Umbraco v6中,可以使用以下命令获取dictionaryitem: umbraco.library.GetDictionaryItem("EmailSubject"); 这将根据用户访问umbraco网站的文化检索“EmailSubject”的正确值 现在我正在编写一个简单的电子邮件类库,其中我不关心System.Threading.Thread.CurrentThread.CurrentCulture,也不想在获取值之前一直设置CurrentCulture。这是可行的,但我不喜欢这

在Umbraco v6中,可以使用以下命令获取dictionaryitem:

umbraco.library.GetDictionaryItem("EmailSubject");
这将根据用户访问umbraco网站的文化检索“EmailSubject”的正确值

现在我正在编写一个简单的电子邮件类库,其中我不关心System.Threading.Thread.CurrentThread.CurrentCulture,也不想在获取值之前一直设置CurrentCulture。这是可行的,但我不喜欢这种方法。我正在写一个简单的邮件库。对于每个邮件收件人,我认为这样设置文化并不是很有效

我找到的解决方案(在线搜索,我丢失了源代码抱歉)如下所示:

//2 = the 2nd language installed under Settings > Languages, which is German in my case
var sometext = new umbraco.cms.businesslogic.Dictionary.DictionaryItem("SomeText").Value(2);
我创建了一些帮助器方法以使其更简单:

private string GetDictionaryText(string dictionaryItem, string language)
{
    //try to retrieve from the cache
    string dictionaryText = (string)HttpContext.Current.Cache.Get(dictionaryItem + language);

    if (dictionaryText == null)
    {
        dictionaryText = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(dictionaryItem).Value(GetLanguageId(language));
        //add to cache
        HttpContext.Current.Cache.Insert(dictionaryItem + language, dictionaryText, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
    }

    return dictionaryText;
}

private int GetLanguageId(string language)
{
    int languageId = 1; //1 = english, 2 = german, 3 = french, 4 = italian

    switch (language)
    {
        case "de":
            languageId = 2;
            break;  
        case "fr":
            languageId = 3;
            break;
        case "it":
            languageId = 4;
            break;
    }

    return languageId;
}
使用my helpers获取德语“EmailSubject”的示例:

string emailSubject = GetDictionaryText("EmailSubject", "de");
这是可行的(使用umbraco 6.2.x进行了测试),但正如您所注意到的,每次需要这样的文本时,都必须创建umbraco.cms.businesslogic.Dictionary.DictionaryItem类的新实例。。。这不是必须的,但我想知道是否有一个静态方法可以用于此,也许允许指定语言或文化(作为字符串),而不是语言或文化id,这在不同的环境中可能会有所不同

由于umbraco API的功能非常强大(有时一些很酷的功能没有文档记录),我找不到更好的解决方案,我想知道是否有更好的umbraco“原生”方法来实现这一点,而不需要上面列出的其他辅助方法


在您的回答中,请列出您正在使用的umbraco版本。

据我所知,umbraco目前没有通过特定语言获取词典项的静态方法。我必须像您一样按语言获取字典项(我使用了Umbraco版本7.2.8)。但是,我通过Umbraco提供的函数获取语言列表

我希望Umbraco在将来的版本中添加此功能。我认为这是必要的,正如你所说。

使用按语言获取词典项。我创建了一个静态方法,可以执行以下操作:

public static string GetDictionaryValue(string key, CultureInfo culture, UmbracoContext context)
{
    var dictionaryItem = context.Application.Services.LocalizationService.GetDictionaryItemByKey(key);
    if (dictionaryItem != null)
    {
        var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.CultureInfo.Equals(culture));
        if (translation != null)
            return translation.Value;
    }
    return key; // if not found, return key
}

那么你得到了整个列表并将其加载到缓存中?每种语言?umbraco函数指的是什么?