What kind of use case would you use this for? I suspect there might be another solution..
It is possible to change template with code. You could use somekinde of dropdown for the editors to choose layout or you could force them by add code looking at the current language of a page and with that choose a masterpage or something like that.
If the templates/functionallity in the templet differ much between the languages you might choose another approach. For instance have a new pagetype/template.
Arild,
The use case is that we have an existing website runs on 6 european languages all works fine. However client now requires site in arabic creating language etc is fine. However as arabic is right to left some elements of the site need to switch as well so as to make it a good user experience for native arabic speaker e.g left hand navigation becomes right hand nav also search box top right now moves top left.
If you view aljazeera or bbc site in english and arabic you can see the flip. So rather than having to re do a whole load of templates i am looking to create arabic templates then if site language chosen is arabic i can make use of those templates.
Eric I would prefer to swap out templates rather than create new page types for arabic and then templates.
Regards
Ismail
So is it not possible to create your own urlrewriteprovider that inherits from FriendlyUrlRewriteProvidor then swap out the template e.g
protected override bool ConvertToExternalInternal(UrlBuilder url, object internalObject, Encoding toEncoding)
{
try
{
if (IsTemplate(url) && IsArabicExternal(url))
{
//Templates/Pages/Home.aspx?id=3&epslanguage=ar-AE
string newTemplatePath = url.Path.Replace(DefaultTemplateRoot, ArabicTemplateRoot);
if (HostingEnvironment.VirtualPathProvider.FileExists(newTemplatePath))
{
url.Path = newTemplatePath;
}
}
return base.ConvertToExternalInternal(url, internalObject, toEncoding);
}
catch (Exception)
{
return false;
}
}
Tried this but it still uses the template set in admin.
Regards
Ismail
Hi Ismail
Turns out a colleague of mine has done this using an InitializableModule that hooks onto the UrlRewriteProvider_ConvertedToInternal event. Something like this:
[InitializableModule]
[ModuleDependency((typeof(EPiServer.Web.InitializationModule)))]
public class SwitchTemplateProviderInitializableModule : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
Global.UrlRewriteProvider.ConvertedToInternal += new EventHandler<UrlRewriteEventArgs>(UrlRewriteProvider_ConvertedToInternal);
}
void UrlRewriteProvider_ConvertedToInternal(object sender, UrlRewriteEventArgs e)
{
string path = e.Url.Path;
// set English or Arabic template path depending on active language
e.Url.Path = pathToActiveLanguagesAspxFileGoesHere;
}
}
Obviously you need to do the bits that I left out of my pseudo code, like checking language and verifying that the aspx paths exist. But that should do the trick.
Arild,
WOOHOOO result. It works many thanks for this. Just saved me a lot of hassle.
Regards
Ismail
Hey Ismail/Arild,
I am actually trying to do something quite similar to you, as in I have an existing site that now wants Arabic, and I'd like to avoid having to create a whole new set of controllers (page types). I am using Episerver 7, and MVC - not sure if InitializableModule have changed?.
I can't seem to get the example that Arild posted to work.. I first had to add the other required methods for the interface.
But, it only hits the Initilize once, and never seems to hit the UrlRewriteProvider_ConvertedToInternal method.
Is there something I am missing? At what point should this load in? I am assuming it should happen each time a page is requested, in either CMS or View mode?
Any advise would be helpful
Thanks
Danny
Danny,
See below for my class btw i am using episerver6 but in theory it should work on 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using EPiServer;
using EPiServer.Framework;
namespace MySite.Extensions
{
[InitializableModule]
[ModuleDependency((typeof(EPiServer.Web.InitializationModule)))]
public class ArabicTemplateSwitcherModule : IInitializableModule
{
static readonly List<string> ArabicCultures = new List<string> { "ar-dz",
"ar-bh",
"ar-eg",
"ar-iq",
"ar-jo",
"ar-kw",
"ar-lb",
"ar-ly",
"ar-ma",
"ar-om" ,
"ar-qa",
"ar-sa",
"ar-sy",
"ar-tn",
"ar-ae",
"ar-ye"};
private const string ArabicTemplateRoot = "Templates/Arabic/Pages/";
private const string DefaultTemplateRoot = "Templates/Pages/";
public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
{
Global.UrlRewriteProvider.ConvertedToInternal += UrlRewriteProvider_ConvertedToInternal;
}
void UrlRewriteProvider_ConvertedToInternal(object sender, EPiServer.Web.UrlRewriteEventArgs e)
{
UrlBuilder url = e.Url;
if (IsTemplate(url) && IsArabicExternal(url))
{
string newTemplatePath = url.Path.Replace(DefaultTemplateRoot, ArabicTemplateRoot);
if (HostingEnvironment.VirtualPathProvider.FileExists(newTemplatePath))
{
url.Path = newTemplatePath;
}
}
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
{
}
private bool IsTemplate(UrlBuilder url)
{
if (url.Path.Contains(DefaultTemplateRoot))
{
return true;
}
return false;
}
private bool IsArabicExternal(UrlBuilder url)
{
if (url.QueryCollection["epslanguage"] != null && ArabicCultures.Contains(url.QueryCollection["epslanguage"].ToLower()))
{
return true;
}
return false;
}
}
}
Regards
Ismail
Is there any particular reason you can't just use dir="rtl" on the body element or surrounding div (if the current language is right to left)?
Thanks for the code Ismail, I'll look at implementing that tomorrow, but it may work differently in MVC now I think about it...
Peter, to answer your question, obviously I'll add in the dir="rtl" into my body tag.. However, due to site design, certain modules may not work correctly without HTML updates..
Is it possible to dynamically set a page template on a page / page type? In my scenario I want to use a particular template for one page in swedish and norwegian. I cannot see how todo this in the editor so I suspect I need to do it in code? If so how?
Regards
Ismail