没有语言代码,Sitecore显示名称网址无法正常工作 [英] Sitecore display name url does not work without language code

查看:115
本文介绍了没有语言代码,Sitecore显示名称网址无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此Sitecore网站(6.5.0修订版120706)上,我有一个名为XYZ的Sitecore项目.所以我有 http://example.com/XYZ/.

On this Sitecore website (6.5.0 rev. 120706), I have this sitecore item called XYZ. So I have http://example.com/XYZ/.

我添加了法语本地化功能,因此使用显示名称,我现在有了:

I've added french localization, so using display names I now have:

http://example.com/XYZ-zh/ http://example.com/XYZ-fr/

英语版本效果很好,但是法语版本无法解决,除非我先使用英语,否则解析为404,首先单击我的语言切换器按钮.当我单击它时,我将重定向到 http://example.com/fr-CA /XYZ-fr/,可以正常工作.从那时起,英文网址停止工作,法语网址停止工作.当我像这样切换语言时,我总是只有两种有效的语言之一.

The english version works well, but the french does not and resolves to 404 unless I go to the english first, click on my language switcher button first. When I click on it, I'm redirected to http://example.com/fr-CA/XYZ-fr/, which works. From then on, the english url stops working, the french one works. As I switch languages like that, I always only have one of the two that work.

该按钮将运行以下代码:

That button runs this code:

protected void LanguageLinkClick(object sender, EventArgs e)
{
    var lang = (Sitecore.Context.Language.Name == "en") ? "fr-ca" : "en";
    Tools.RedirectToLanguage(lang, Response);
}

该工具功能运行以下代码:

That Tools function runs the following code:

public static void RedirectToLanguage(string pStrLangToSet, HttpResponse pResponse)
{
    if (!string.IsNullOrEmpty(pStrLangToSet))
    {
        var newLang = Language.Parse(pStrLangToSet);
        if (newLang != null)
        {
            Sitecore.Context.SetLanguage(newLang, true);
            var itm = Sitecore.Context.Item;
            if (Sitecore.Context.Item != null)
            {
                var itemInLang = Sitecore.Context.Database.Items[itm.ID, newLang];
                if (itemInLang != null)
                {
                    pResponse.Redirect(BuildUrl(itemInLang));
                }
            }
        }
    }
}

这是旧项目中的旧代码.

This is somewhat old code that is in this old project.

我是否应该寻找可以拦截默认显示名称行为的内容?还是显示名称的这种行为不是开箱即用的?

Is there anything I should look for that would intercept default display name behavior? Or isthis behavior with display names something that's not managed out of the box?

感谢您的帮助!

推荐答案

这是预期的行为.这

This is the expected behaviour. This article from John West describes the steps involved in the language resolving process and details of the ItemResolver process can be found here.

我假设您在LinkProvider中为站点设置的是useDisplayName=true,也许是languageEmbedding=false,如下所示:

What I assume you have set in the LinkProvider for your site is useDisplayName=true and maybe languageEmbedding=false, something like the following:

<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" 
  alwaysIncludeServerUrl="false" addAspxExtension="true" encodeNames="true"
  languageLocation="filePath" lowercaseUrls="false" shortenUrls="true" 
  languageEmbedding="never" useDisplayName="true" />

这告诉LinkManager使用项目的显示名称来构建URL,因此您具有多语言URL.您的RedirectToLanguage方法将上下文语言从EN切换为FR-CA,反之亦然,这会将Sitecore设置为用户的特定语言模式.

This tells the LinkManager to build URLs using the display name of the Item, hence you have multilingual URLs. Your RedirectToLanguage method switches the context language, from EN to FR-CA and vice versa, which puts Sitecore into that specific language mode for the user.

<httpRequestBegin>中的以下管道尝试从您请求的URL解析项目:

The following pipeline in <httpRequestBegin> tries to resolve an Item from your requested URL:

<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel" />

该方法中的一种尝试是ResolveUsingDisplayName(args).因此它将尝试解析具有EN显示名称的项目,但是上下文语言设置为FR-CA,因此实际上它将永远找不到该项目,因此找不到404.

And one of the attempts in that method is to ResolveUsingDisplayName(args). So it will be trying to resolve an Item with an EN display name, but the Context Language is set to FR-CA so in fact it will never find the item, hence the 404.

您在这里有2个选择:

  1. 在LinkProvider中设置languageEmbedding="always",这意味着您的URL的格式将类似于"/en/Nice-Product-With-Lots-Of-Options"和"/fr-ca/Mon-Produit-Avec-Plusieurs-选项".
  2. 在默认的ItemResolver之后添加一个自定义管道处理器,该处理器尝试使用备用语言进行解析,如果找到匹配项,则设置上下文项并切换语言.
  1. Set languageEmbedding="always" in your LinkProvider, which mean your URLs will be formatted like "/en/Nice-Product-With-Lots-Of-Options" and "/fr-ca/Mon-Produit-Avec-Plusieurs-Options".
  2. Add a custom pipeline processor after the default ItemResolver which tries to resolve using the alternate language, and if it finds a match sets the Context Item and switches language.

在切换到其他语言后,以下内容将调用默认的Sitecore ItemResolver,然后尝试解析,这将尝试使用显示名称查找该项目:

The following will call the default Sitecore ItemResolver after you switch to the alternate language and then attempt to resolve, which will try to find the Item using display name:

public class AlternateLanguageItemResolver : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0)
          return;

        var origLanguage = Sitecore.Context.Language;
        Sitecore.Context.Language = AltLanguage;

        // try to find it using default ItemResolver with alternate language
        var itemResolver = new Sitecore.Pipelines.HttpRequest.ItemResolver();
        itemResolver.Process(args);

        if (Context.Item == null)
        {
            // well we didn't find it, so switch the context back so everyting can continue as normal
            Sitecore.Context.Language = origLanguage;
            return;
        }

        // We found the Item! Switch the User Language for future requests
        Sitecore.Context.SetLanguage(AltLanguage, true);
    }

    private Language _altLanguage = null;
    private Language AltLanguage
    {
        get
        {
            if (_altLanguage == null)
            {
                var altLang = (Sitecore.Context.Language.Name == "en") ? "fr-ca" : "en";
                _altLanguage = Language.Parse(altLang);
            }
            return _altLanguage;
        }
    }
}

并在默认的ItemResolver之后对其进行修补:

And patch it in after the default ItemResolver:

<processor type="Sitecore.Sample.AlternateLanguageItemResolver, Sitecore.Sample"
           patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>

这是未经测试的原型,请检查是否以正确的切换语言返回了后续请求.否则,您将像原始代码中一样重定向回自身.

This is an untested prototype, check that subsequent requests are returned in the correct switched language. Otherwise redirect back to itself like you in your original code.

这篇关于没有语言代码,Sitecore显示名称网址无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆