Sergii Vorushylo
Jul 25, 2012
  9805
(3 votes)

Moving to a new LocalizationService API in EPiServer 7

This is a short note about replacing the LanguageManager with a new LocalizationService API when upgrading from EPiServer 6 to EPiServer 7. Actually such replacement is not mandatory, since LanguageManager is still there, but it is marked as obsolete and delegates all calls to the LocalizationService.

Replacements

The LocalizationService class is located in EPiServer.Framework assembly in EPiServer.Framework.Localization namespace, so you might need to add a reference to your project.

Here is a summary of what needs to be replaced in order to use the new API.

Simple translation:

EPiServer 6
LanguageManager.Instance.Translate(key)
EPiServer 7 General usage:
LocalizationService.Current.GetString(key)

In Web Forms pages/controls:*
this.Translate(key)

In MVC view:
Html.Translate(key)

Translate with fallback value:

EPiServer 6
LanguageManager.Instance.TranslateFallback(key, fallback)
EPiServer 7 General usage:
LocalizationService.Current.GetString(key, fallback)

In Web Forms pages/controls:*
this.TranslateFallback(key, fallback)

In MVC view:
Html.TranslateFallback(key, fallback)

Translate and adapt value for use in a client script:

EPiServer 6
LanguageManager.Instance.TranslateForScript(key)
EPiServer 7 General usage:
ScriptResourceHelper.PrepareResourceForScript(
LocalizationService.Current.GetString(key))

In Web Forms pages/controls:*
this.TranslateForScript(key)

In MVC view:
Html.TranslateForScript(key)

* There is also a protected helper property named LocalizationService that can be used in page templates and user controls that inherit from one of these base classes:

EPiServer.PageBase
EPiServer.ContentBaseWebForm
EPiServer.UserControlBase
EPiServer.ContentBaseUserControl

How to mock the LocalizationService

As a general recommendation, mainly because of testability, it is better to use a constructor dependency injection instead of static LocalizationService.Current.

If order to mock the LocalizationService in a unit test you need to setup GetStringByCulture method:

var localizationServiceMock = new Mock<LocalizationService>(null);
localizationServiceMock.Setup(s => s.GetStringByCulture(It.IsAny<String>(),
It.IsAny<FallbackBehaviors>(), It.IsAny<CultureInfo>())).Returns(String.Empty);

 

More details about the new localization API can be found in “Using the Localization Service” chapter in SDK Documentation EPiServer 7 – Framework.

Jul 25, 2012

Comments

Alfredo Barrios
Alfredo Barrios Oct 31, 2014 12:09 AM

Excuse me please Mr. Vorushilo, Unable to install CMO.

I have problem in the installation of CMO, the installer only says "Installing - Waiting for user input" but there is not dialog window or console running, I attemped it several times in many ways, reboot the computer, uninstall and re-install EPiServer, at administrator mode, with no process running at the same time, etc, etc and etc too, the problem may be is like this:
http://world.episerver.com/Forum/Developer-forum/EPiServer-7-CMS/Thread-Container/2013/1/Unable-to-install-CMO/

In the forum you saw to Janne it is solved, how can I to contact with support, in support page there is not an email or list with bugs with the number #109167, do you have any information about of the solution?

Oct 31, 2014 09:01 AM

Hello Alfredo,
You can register a developer incident here: http://world.episerver.com/Support/Register-Support-Incident/
In the description, please, request a fix for this bug: http://world.episerver.com/Support/Bug-list-beta/bug/109167

Please login to comment.
Latest blogs
Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024