如何在Sitecore-uCommerce网址中嵌入语言?

时间:2016-07-28 13:50:08

标签: sitecore sitecore8 sitecore-ucommerce

我使用的是uCommerce的默认配置,并看到uCommerce网址不支持语言:http://sitename/catalogname/productname/c-XX/p-YY

如何在这些网址中使用语言http://sitename/en/catalogname/productname/c-XX/p-YY

以下是配置:

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

以下是我如何使用它:

public WebshopProduct Map(UCommerceProduct uProduct)
{
    ProductCatalog catalog = CatalogLibrary.GetCatalog(25);
    IUrlService urlService = ObjectFactory.Instance.Resolve<IUrlService>();
    ...
    var url = urlService.GetUrl(catalog, uProduct) // this returns "/catalogname/productname/c-XX/p-YY"

    //And I would like to have "/en/catalogname/productname/c-XX/p-YY"
}

2 个答案:

答案 0 :(得分:0)

向URL添加语言取决于您呈现链接的方式。如果您没有传递特定参数而不是Sitecore(和uCommerce作为Sitecore的一部分)使用LinkManager配置sitecore&gt; linkManager&gt;提供者:languageEmbedding和languageLocation属性。你应该有languageEmbedding =“always”和languageLocation =“filePath”

P.S。 但是,请注意,如果您使用他们的演示或基于他们的演示(例如来自认证课程)的东西:他们使用常规的ASP.Net MVC(不是Sitecore MVC)。链接不是通过LinkManager呈现的,您应该自己将语言放到URL。注册路由使用嵌入其中的语言代码。

答案 1 :(得分:0)

以下是我的想法:

public static class TemplateIDs
{
    // sitecore/ucommerce item's template id       
    public static ID UCommerce => new ID("{AABC1CFA-9CDB-4AE5-8257-799D84A8EE23}");
}

public static class ItemExtensions
{
    public static bool IsUCommerceItem(this Item item)
    {
        var items = item.Axes.GetAncestors();
        return items.Any(x => x.TemplateID.Equals(TemplateIDs.UCommerce));
    }
}

public static string GetItemUrlByLanguage(Sitecore.Globalization.Language language)
{
    if (Context.Item.IsUCommerceItem() && SiteContext.Current.CatalogContext.CurrentProduct != null && SiteContext.Current.CatalogContext.CurrentProduct.Guid == Context.Item.ID.Guid)
    {
        ProductCatalog catalog = CatalogLibrary.GetCatalog(25);
        IUrlService urlService = ObjectFactory.Instance.Resolve<IUrlService>();
        var url = "/" + language.CultureInfo.TwoLetterISOLanguageName + urlService.GetUrl(catalog, SiteContext.Current.CatalogContext.CurrentProduct);
        return url;
    }
    else
    {
        //Normal URL creation
        using (new LanguageSwitcher(language))
        {
            var options = new UrlOptions
            {
                AlwaysIncludeServerUrl = true,
                LanguageEmbedding = LanguageEmbedding.Always,
                LowercaseUrls = true
            };
            var url = LinkManager.GetItemUrl(Context.Item, options);
            url = StringUtil.EnsurePostfix('/', url).ToLower();
            return url;
        }
    }
}