3

We are developing a multi regional and multilingual site in sitecore 8.2

We have following URL structure:

www.local.com/ar-bh
www.local.com/en-bh

www.local.com/ar-kw
www.local.com/en-kw

and following sites definition in sitecore.config

<site name="Bahrain" hostname="sitecore/" virtualFolder="/en-bh" physicalFolder="/en-bh" rootPath="/sitecore/content" startItem="/Bahrain" database="master" domain="extranet" language ="en"/>

<site name="Bahrain_Arabic" hostname="sitecore/" virtualFolder="/ar-bh" physicalFolder="/ar-bh" rootPath="/sitecore/content" startItem="/Bahrain" database="master" domain="extranet" language ="ar"/>

and

<setting name="Languages.AlwaysStripLanguage" value="false"/>

Sites are working perfectly, but I am having issues in language switcher, I need suggestion from experts what would be the optimal way to achieve this. Each regional site has two languages English and Arabic.

I am using following code to change site context

    var myItem = Sitecore.Context.Item;
    string siteurl = string.Empty;
    var sitesList = Sitecore.Configuration.Factory.GetSiteInfoList();
    var currentSiteName = Sitecore.Context.GetSiteName();
    var website = Sitecore.Configuration.Factory.GetSite("Bahrain_arabic");
    using (new Sitecore.Sites.SiteContextSwitcher(website))
    {
        var options = LinkManager.GetDefaultUrlOptions();
        options.AlwaysIncludeServerUrl = true;
        options.SiteResolving = true;
        siteurl = LinkManager.GetItemUrl(myItem, options);
    }   

LinkManager is always returning the current site url, I am lookin for some dynamic solution.

2
  • Please explain what issues you would like answered. Right now this is a very broad question and not really answerable in this format.
    – Richard Seal
    Commented Sep 19, 2017 at 17:16
  • Hi Richard I have updated the description. Commented Sep 19, 2017 at 17:29

1 Answer 1

3

For the link manager to get the right link for an item in a different language, you need to get the item out of the database in that language. In your code, you are always getting the Sitecore.Context.Item - that will be in whatever language your current site is loaded in. To get the link for the site in another language, you need to get the item out again, specifying the language. Like this:

string siteurl = string.Empty;
var sitesList = Sitecore.Configuration.Factory.GetSiteInfoList();
var currentSiteName = Sitecore.Context.GetSiteName();
var website = Sitecore.Configuration.Factory.GetSite("Bahrain_arabic");

// Get the item from the context database in the language of the site.
var myItem = Sitecore.Context.Database.GetItem(Sitecore.Context.Item.ID, website.Language);

using (new Sitecore.Sites.SiteContextSwitcher(website))
{
    var options = LinkManager.GetDefaultUrlOptions();
    options.AlwaysIncludeServerUrl = true;
    options.SiteResolving = true;
    siteurl = LinkManager.GetItemUrl(myItem, options);
}  

This will generate a URL to the item, in the language of the website.

0

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.