语言回退/在Sitecore.Globalization.Translate.TextByLanguage(键,语言)中设置语言;?

语言回退/在Sitecore.Globalization.Translate.TextByLanguage(键,语言)中设置语言;?,sitecore,sitecore-mvc,sitecore8,Sitecore,Sitecore Mvc,Sitecore8,我正在使用语言回退提供程序。这似乎对存储在系统>词典中的项目没有任何影响 我希望它回退到语言项的fallback属性中指定的语言 更糟糕的是,我想回到英语上来 我从字典中提取文本的方法可以称之为 Globalization.Translate.TextByLanguagekey 我被困在: 如何找到当前指定语言的Fallback属性的值 如何将其传递到TextByLanguage。它似乎需要一个Language属性,但Language似乎没有 任何构造函数。 历史:按照@Jammykam的建议,

我正在使用语言回退提供程序。这似乎对存储在系统>词典中的项目没有任何影响

我希望它回退到语言项的fallback属性中指定的语言

更糟糕的是,我想回到英语上来

我从字典中提取文本的方法可以称之为

Globalization.Translate.TextByLanguagekey

我被困在:

如何找到当前指定语言的Fallback属性的值 如何将其传递到TextByLanguage。它似乎需要一个Language属性,但Language似乎没有 任何构造函数。 历史:按照@Jammykam的建议,我看了一看Verndale的例子,但我对它没有任何运气。它似乎永远不会着火

我在解决方案中添加了以下类:

using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Pipelines.GetTranslation;
using Sitecore.SharedSource.PartialLanguageFallback.Extensions;
using System;
using System.Collections.Generic;

namespace Sitecore.Web.Pipelines
{
    public class DictionaryFallback : TryGetFromFallbackDomains
    {
        public static bool EnableFallback
        {
            get
            {
                return Sitecore.Context.Site != null &&
                       Sitecore.Context.Site.SiteInfo.Properties["enableFallback"] != null &&
                       Sitecore.Context.Site.SiteInfo.Properties["enableFallback"].Equals("true", StringComparison.InvariantCultureIgnoreCase);
            }
        }

        /// <summary>
        /// Runs the processor.
        /// 
        /// </summary>
        /// <param name="args">The arguments.
        ///             </param>
        public void Process(GetTranslationArgs args)
        {
            Assert.ArgumentNotNull((object)args, "args");
            List<string> processedDomains = new List<string>();
            if (args.HasResult || Context.Site == null || string.IsNullOrEmpty(Context.Site.DictionaryDomain))
                return;
            this.Args = args;
            this.Database = args.Options.Database ?? args.ContentDatabase;
            DictionaryDomain domain;
            if (!DictionaryDomain.TryParse(Context.Site.DictionaryDomain, this.Database, out domain) || domain == null) {
                Log.Info("Error Parsing Dictionary: " + domain.FullyQualifiedName, this);
                return;
            }


            string result;
            if (this.TryGetTranslation(domain, processedDomains, out result) && result != null)
            {
                Log.Info("Normal Translation: " + domain.FullyQualifiedName + ": " + result, this);
                args.Result = result;
            }
            else if (EnableFallback)
            {
                if (this.TryTranslateTextByFallbackLanguage(args, domain, out result) && result != null)
                {
                    Log.Info("Fallback Translation: " + domain.FullyQualifiedName + ": " + result, this);
                    args.Result = result;
                }
            }

        }

        protected virtual bool TryTranslateTextByFallbackLanguage(GetTranslationArgs args, DictionaryDomain domain, out string result)
        {
            result = null;
            List<string> processedDomains = new List<string>();

            // check if the the language passed in with the args has fallback assigned
            // if so, then get that fallback language
            // must try to get the translation based on that language
            var languageFallsBack = args.Language.HasFallbackAssigned(args.ContentDatabase);
            if (languageFallsBack)
            {
                Language fallbackLanguage = args.Language.GetFallbackLanguage(args.ContentDatabase);

                // the following cannot be called from here, because it is an internal method to the Sitecore.Kernel library
                //Translate.TryTranslateTextByLanguage(args.Key, domain, fallbackLanguage, out result, args.Parameters);

                // therefore, we set Args.Language to the fallbacklanguage
                // this.Args is the Args object in TryGetFromFallbackDomains processor
                // then we call this.TryGetTranslation, which is a method in the TryGetFromFallbackDomains processor, 
                // which IS in the Sitecore.Kernel library and therefore can make the call to TryTranslateTextByLanguage

                this.Args.Language = fallbackLanguage;

                if (this.TryGetTranslation(domain, processedDomains, out result) && result != null)
                {
                    return true;
                }
                else
                {
                    // if no results if found, try to see if this fallback language falls back itself to another language
                    // and then if so, try the translation with that
                    // pass into the recursive call this.Args (instead of args), since the language has been updated in this.Args
                    if (result == null)
                    {
                        var isSuccess = TryTranslateTextByFallbackLanguage(this.Args, domain, out result);
                        return isSuccess;
                    }
                    else
                        return false;
                }
            }
            else
            {
                return false;
            }

        }


    }
}
我将以下代码添加到修补程序文件中:

<pipelines>
            <getTranslation>
                <processor patch:after="*[@type='Sitecore.Pipelines.GetTranslation.TryGetFromCoreDatabase, Sitecore.Kernel']" type="Sitecore.Web.Pipelines.DictionaryFallback, Sitecore.Web" />
            </getTranslation>
        </pipelines>
我可以使用showconfig.aspx查看它:

<getTranslation>
<processor type="Sitecore.Pipelines.GetTranslation.ResolveContentDatabase, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromDomain, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromFallbackDomains, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromSiteDomain, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromContextDatabase, Sitecore.Kernel"/>
<processor type="Sitecore.Pipelines.GetTranslation.TryGetFromCoreDatabase, Sitecore.Kernel"/>
<processor type="Sitecore.Web.Pipelines.DictionaryFallback, Sitecore.Web" patch:source="SitecoreSettings.config"/>
</getTranslation>
你会在我的课堂上看到我加入了一些日志记录,但我从来没有看到一个项目被记录。具有所选语言版本的项目将正确渲染。但是如果他们没有版本,它会显示密钥,而不是后退

以下是通过showconfig.aspx显示的部分我的真实域在此处更改为example.com:

<sites><site name="shell" virtualFolder="/sitecore/shell" physicalFolder="/sitecore/shell" rootPath="/sitecore/content" startItem="/home" language="en" database="core" domain="sitecore" loginPage="/sitecore/login" content="master" contentStartItem="/Home" enableWorkflow="true" enableAnalytics="false" analyticsDefinitions="content" xmlControlPage="/sitecore/shell/default.aspx" browserTitle="Sitecore" htmlCacheSize="10MB" registryCacheSize="15MB" viewStateCacheSize="1MB" xslCacheSize="25MB" disableBrowserCaching="true" itemwebapi.mode="StandardSecurity" itemwebapi.access="ReadWrite" itemwebapi.allowanonymousaccess="false" /><site name="login" virtualFolder="/sitecore/login" physicalFolder="/sitecore/login" enableAnalytics="false" database="core" domain="sitecore" disableXmlControls="true" /><site name="admin" virtualFolder="/sitecore/admin" physicalFolder="/sitecore/admin" enableAnalytics="false" enableWorkflow="true" domain="sitecore" loginPage="/sitecore/admin/login.aspx" /><site name="service" virtualFolder="/sitecore/service" physicalFolder="/sitecore/service" /><site name="modules_shell" virtualFolder="/sitecore modules/shell" physicalFolder="/sitecore modules/shell" rootPath="/sitecore/content" startItem="/home" language="en" database="core" domain="sitecore" content="master" enableAnalytics="false" enableWorkflow="true" /><site name="modules_website" virtualFolder="/sitecore modules/web" physicalFolder="/sitecore modules/web" rootPath="/sitecore/content" startItem="/home" language="en" database="web" domain="extranet" allowDebug="true" cacheHtml="true" /><!-- ITEM WEB API SETTINGS FOR A SITE
           Supported attributes (first is default):
             itemwebapi.mode: [Off|StandardSecurity|AdvancedSecurity]
               If set to Off, Item Web API is turned off.
               If set to StandardSecurity, Item Web API is turned on. Default Sitecore security model is used.
               If set to AdvancedSecurity, Item Web API is turned on. Default Sitecore security model is extended with a requirement to explicitely set the 'remote:fieldread' access right for content fields.
             itemwebapi.access: [ReadOnly|ReadWrite]
               If set to ReadOnly, then only READ operation is allowed.
               If set to ReadWrite, then CREATE, READ, UPDATE, and DELETE operations are allowed.
             itemwebapi.allowanonymousaccess: [false|true].
               Defines if access is allowed for non-authenticated user.
      --><!--<site name="mysite" patch:before="site[@name='website']"
            virtualFolder="/"
            physicalFolder="/"
            rootPath="/sitecore/content"
            startItem="/home"
            database="web"
            domain="extranet"
            allowDebug="true"
            cacheHtml="true"
            htmlCacheSize="50MB"
            enablePreview="true"
            enableWebEdit="true"
            enableDebugger="true"
            disableClientData="false"/>--><site name="english" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="stage.example.com|127.0.0.1|localhost" language="en" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="1GB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="chinese" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="chinesestage.example.com" language="zh-CN" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="german" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="germanstage.example.com" language="de-DE" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="spanish" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="spanishstage.example.com" language="es-ES" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="french" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="frenchstage.example.com" language="fr-FR" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="italian" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="italianstage.example.com" language="it-IT" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="japanese" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="japanesestage.example.com" language="ja-JP" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="portuguese" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="portuguesestage.example.com" language="pt-BR" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="russian" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" hostName="russianstage.example.com" language="ru-RU" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="500MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" enableFallback="true" patch:source="SiteDefinition.config" /><site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="25MB" filteredItemsCacheSize="10MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" disableClientData="false" cacheRenderingParameters="true" renderingParametersCacheSize="10MB" itemwebapi.mode="Off" itemwebapi.access="ReadOnly" itemwebapi.allowanonymousaccess="false" htmlCacheSize="1GB" /><site name="scheduler" enableAnalytics="false" domain="sitecore" /><site name="system" enableAnalytics="false" domain="sitecore" /><site name="publisher" domain="sitecore" enableAnalytics="false" enableWorkflow="true" /></sites>

关闭更新:我使用了下面接受的答案,并对其进行了修改以使其适用于我:

字典项不适用于语言回退模块默认情况下,您必须使用附加处理器修补getTranslate管道,以进行回退语言检查并获得回退语言中的正确字典项


您可以在本文中找到有关的其他详细信息,之后您可以继续正常使用词典,而不必通过全球化的语言。Translate.TextByLanguagekey。

默认情况下,词典项不适用于语言回退模块,您必须使用额外的处理器修补getTranslate管道,以进行回退语言检查并获得回退语言中的正确字典项


您可以在本文中找到更多关于的详细信息,之后您可以继续正常使用词典,而不必通过全球化的语言。Translate.TextByLanguagekey。

我以前已经解决了这个问题,请查看以下博客文章,与词典项目相关的部分:


我还为此构建了一个模块,上传到sitecore仍在等待审核。

我之前已经解决了这个问题,请查看以下博客文章,与词典项目相关的部分:


我还为此构建了一个模块,上传到sitecore,仍在等待审核。

我尝试过。即使在修复了损坏的引用之后,代码也无法成功生成。我的Sitecore.Kernel.dll来自Sitecore 8。我不确定这是否与此有关。您所需要的代码的唯一部分是TryGetFromFallbackLanguage.cs和对getTranslate管道配置的添加。将这些添加到您自己的解决方案中,然后尝试重建。代码中似乎还有其他部分引用了过时的库搜索contrib,这些库在Sitecore 7+中不再有效。愚蠢的问题。。。使用此方法从字典中获取文本时,我会在视图中使用什么语法?在页面顶部@using Sitecore.Globalization,然后使用@Translate检索字典。Textkey@eat-sleep code该代码要求站点节点上具有dictionaryDomain属性。试试这个过程方法,我添加了回退:我试过了。即使在修复了损坏的引用之后,代码也无法成功生成。我的Sitecore.Kernel.dll来自Sitecore 8。我不确定这是否与此有关。您所需要的代码的唯一部分是TryGetFromFallbackLanguage.cs和对getTranslate管道配置的添加。将这些添加到您自己的解决方案中,然后尝试重建。代码中似乎还有其他部分引用了过时的库搜索contrib,这些库在Sitecore 7+中不再有效。愚蠢的问题。。。使用此方法从字典中获取文本时,我会在视图中使用什么语法?在页面顶部@using Sitecore.Globalization,然后使用@Translate检索字典。Textkey@eat-sleep code该代码要求站点节点上具有dictionaryDomain属性。我添加了回退:我可以在代码中看到一些问题。由于您已硬编码字典项ID,因此它将不适用于字典域。也没有对回退的回退进行递归调用。这是我需要的解决方案,我只需要一个级别的回退,否则词汇项的键将br retrieved上一次更新的代码检查上下文域extranet,即域

我希望我的代码能够工作,任何想法或补充都是非常肯定的,这只是一个观察。字典域不同于安全域,它允许您在multisite Implementation@jammykam中为每个网站指定不同的字典。谢谢,但我认为博客中的代码加上一点tweeks将解决问题。您认为如何?我可以在您的代码中看到一些问题。由于您已硬编码字典项ID,因此它将不适用于字典域。也没有对回退的回退进行递归调用。这是我需要的解决方案,我只需要一个级别的回退,否则将检索词汇项的键br retrieved上一次更新的代码检查上下文域extranet,这是我希望我的代码处理的域,任何想法或添加都将非常确定,这只是一个观察。字典域不同于安全域,它允许您在multisite Implementation@jammykam中为每个网站指定一个不同的字典谢谢,但我认为博客中的代码加上一点tweeks将解决问题。您认为如何?您是否在配置中的节点上设置了“enableFallback=true”?@jammykam是的。我在上面添加了从showconfig.aspx输出的部分。在我看来这些都是正确的。尝试将处理器向上/首先移动,并检查其运行是否正确。我添加了更多日志行。Context.Site.DictionaryDomain似乎是空字符串?这将导致进程退出。如何解决这个问题?@jammykam您发送的Gist中有一个未定义的变量,名为database。我尝试了这个。数据库在它的位置,它对此不满意。我尝试不使用任何东西,但还是不满意。您是否在配置中的节点上设置了“enableFallback=true”?@jammykam是的。我在上面添加了从showconfig.aspx输出的部分。在我看来这些都是正确的。尝试将处理器向上/首先移动,并检查其运行是否正确。我添加了更多日志行。Context.Site.DictionaryDomain似乎是空字符串?这将导致进程退出。如何解决这个问题?@jammykam您发送的Gist中有一个未定义的变量,名为database。我尝试了这个。数据库在它的位置,它对此不满意。我试着什么都不用,但还是不开心。