Using CMS fallback language for lang-files

In EPiServer

This blog post is about how you can use the fallback language defined in EPiServer CMS together with the lang-files. Instead of manually defining a fallback language in markup or code we’ll retrieve the fallback language defined in the CMS.

Over at EPiServer World…

worldThe idea came from a forum thread on EPiServer World. A fellow EPiServer developer was wondering why the fallback language defined in the CMS did not work together with the Translate web control. EMVP Magnus Paulsson suggested a neat solution by implementing an extension method to the LanguageManager-class in which the user could define a fallback language branch.

The extension method had the following signature:

Though the user would still need to manually define the language branch ID in code. This got me thinking: can’t we get ahold of the fallback language branch ID defined in the CMS?

Of course we can. 🙂

A new extension method

Using the data abstraction layer in EPiServer CMS we can query which fallback languages are set for the current language. We’ll use the PageLanguageSetting-class in the EPiServer.DataAbstraction namespace.

Using the static Load-method we can get a PageLanguageSetting object which contains information about replacement and fallback languages. The signature is as follows.

We need to pass the root page as PageReference and the current page’s language. Like so.

So let’s put all this together. First the extension method (keep this in a separate extensions class!).

That takes care of the actual extension-method to the LanguageManager-class. Now let’s have a go at the GetFallbackLanguageIDFromCMS-method.

Now you can use this method together with the LanguageManager-class.

daniel
daniel
Developer
Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search