0

Our website require fix path structure in different language version, I know when you want to switch language, just put ?sc_lang=xx behind the path. but my condition is language ia part of path and friendly path

for example:

(en) http://xxx.xxx/en/how_this_works

(de) http://xxx.xxx/de/So_funktioniert

The problem is even I can replace the en to de, but how do I know in German version of current page customer display name(how this works -?->So funktioniert.

Additionally, language selector is in Navi page, it with its controller, but each Page Content has its own Controller, therefore Navi controller won't be able to know other rendering's information(ex:item ID) in other placeholder.

0

2 Answers 2

1

To build the URL's you should use the LinkManager.

You can fetch the item in the correct language and pass it to the linkmanager to obtain the correct link like

var langItem = currentItem.Database.GetItem(currentItem.ID, language);
var url = LinkManager.GetItemUrl(langItem);
7
  • Thanks for replay! It is almost working, the issue I stocked is when the display name with space, I can not get correct Item .. Commented Jun 6, 2018 at 14:51
  • @Kun-YaoWang how are you getting the item via displayname? Commented Jun 6, 2018 at 18:56
  • @HishaamNamooya I get from current page addrees and do some string replacement. Commented Jun 7, 2018 at 8:14
  • @HishaamNamooya int segmentTotal = HttpContext.Current.Request.Url.Segments.Length - 1; var curDisplayName = HttpContext.Current.Request.Url.Segments[segmentTotal].Replace("-", " ").Replace(",-d-,", "."); Commented Jun 7, 2018 at 8:14
  • @Kun-YaoWang Can you please share the code you mention above on the question itself and also share how you are fetching the item from Sitecore using the displayname Commented Jun 7, 2018 at 8:20
0

The key is to obtain correct version of link address by passing itemID to Action Link, therefore create a pgLinkRedirect() in controller

    public ActionResult pgRedirect(string itemid)
    {
        var item = Sitecore.Context.Database.GetItem(new ID(itemid));

        UrlOptions op = new UrlOptions();
        op.UseDisplayName = true;
        op.LanguageEmbedding = LanguageEmbedding.Always;
        op.LowercaseUrls = true;

        var url = ComponentHelper.DisplayNameHandler(LinkManager.GetItemUrl(item, op), 2);

        return Redirect(url);
    }

When you need another version of link, pass language name into GetItem as one of parameter.

 var item = Sitecore.Context.Database.GetItem(new ID(itemid), LanguageManager.GetLanguage(lg));

And then

each link in View using Action Link to pass itemID to controller

@Html.ActionLink(@item.MenuItemName, "pgRedirect", new { itemid = @item.itemID })

so you can get the link correctly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.