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);
}
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
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.
Make sure you have uiShowGlobalizationUserInterface set to true in site settings.
Hi Johan
Found it now, I hadnt spotted that dropdown. Got it working nicely now.
Thanks very much
Dave
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