Try our conversational search powered by Generative AI!

Language Selector

Vote:
 

Hi all

I am working on a Multi-Language site and I need to be able to generate a list of all of the available languages to output to allow the user to toggle between sites. However this needs to be split out into different regions of the world e.g. Europe, Asia etc so I cannot just grab a list of enabled languages from the system.

My thoughts were to effectively create a menu with a shortcut page in to the home page for each language site. Alternatively use a LinkItemCollection for each region and link from their to the home page of each language. With both options though in the editor I cannot find a way to link to a page within another language. Is there something I need to enable to allow this or can anyone else suggest a better option for doing this?

Many thanks
Dave

#73885
Aug 13, 2013 16:54
Vote:
 

Hi Dave,

 

Here is a short snippet that does redirect the user to the same page if it is translated or has a fallback language.

private List<LanguageBranch> availableLanguages;

protected List<LanguageBranch> AvailableLanguages
{
    get
    {
        if (this.availableLanguages == null)
        {
            this.availableLanguages = new List<LanguageBranch>();

            var service = ServiceLocator.Current.GetInstance<ILanguageBranchRepository>();

            foreach (LanguageBranch item in service.ListAll())
            {
                if (item.Enabled)
                {
                    this.availableLanguages.Add(item);                            
                }
            }

            this.availableLanguages = this.availableLanguages.OrderBy(l => l.SortIndex).ToList();
        }

        return this.availableLanguages;
    }
}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    if (string.IsNullOrWhiteSpace(Request["change-language"]) == false)
    {
        // Load the language we want to redirect to
        var preferredLanguage = this.AvailableLanguages.FirstOrDefault(l => l.LanguageID == Request["change-language"]);
                
        if (preferredLanguage != null)
        {
            Response.Redirect(CurrentPage.GetPageLanguageUrl(preferredLanguage.LanguageID), true);
        }
    }
}

And then an extension method for fetching the page url:

public static string GetPageLanguageUrl(this PageData page, string languageId)
{
    var languageBranch = ServiceLocator.Current.GetInstance<ILanguageBranchRepository>().Load(languageId);

    if (languageBranch.Enabled == false)
    {
        return string.Empty;
    }

    // Check if the page is translated to the language
    if (page.ExistingLanguages.Any(c => c.Name == languageBranch.LanguageID))
    {
        return UriSupport.AddLanguageSelection(page.LinkURL, languageBranch.LanguageID);
    }

    // The page was not translated, check if the page has a fallback set on the preferred language
    var languageFallbacks = ServiceLocator.Current.GetInstance<IContentLanguageSettingsHandler>()
                    .GetFallbackLanguages(page.ContentLink, languageBranch.LanguageID);

    if (languageFallbacks.Contains(page.LanguageBranch))
    {
        return UriSupport.AddLanguageSelection(page.LinkURL, languageBranch.LanguageID);
    }

    var startPage = ServiceLocator.Current.GetInstance<IContentRepository>().Get<PageData>(PageReference.StartPage);

    // No fallback either, redirect to startpage
    return UriSupport.AddLanguageSelection(startPage.LinkURL, languageBranch.LanguageID);
}

    

 

#73895
Aug 14, 2013 0:47
Vote:
 

Hi Johan

Thank you for your response, that looks like a really useful bit of code that I will no doubt use. Unfortunately its not quite what im looking for at the moment, apologies I probably did not explain very well. I am trying to generate a page more like this:

http://www.nintendo.com/countryselector

It would just link to the home page of each country rather than translating the current page into another language.

Many Thanks
Dave

#73938
Aug 14, 2013 17:16
Vote:
 

There should be a dropdown menu in the LinkItemCollection, where you can choose the language for the link.

From the Nintendo example I would do three LinkItemCollection, one for each continent.

 

#73952
Aug 15, 2013 10:09
Vote:
 

Make sure you have uiShowGlobalizationUserInterface set to true in site settings.

#73953
Aug 15, 2013 10:10
Vote:
 

Hi Johan

Found it now, I hadnt spotted that dropdown. Got it working nicely now.

Thanks very much
Dave

#73962
Aug 15, 2013 13:55
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.