Sergii Vorushylo
Jul 25, 2012
  9873
(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
Copy Optimizely SaaS CMS Settings to ENV Format Via Bookmarklet

Do you work with multiple Optimizely SaaS CMS instances? Use a bookmarklet to automatically copy them to your clipboard, ready to paste into your e...

Daniel Isaacs | Dec 22, 2024 | Syndicated blog

Increase timeout for long running SQL queries using SQL addon

Learn how to increase the timeout for long running SQL queries using the SQL addon.

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Overriding the help text for the Name property in Optimizely CMS

I recently received a question about how to override the Help text for the built-in Name property in Optimizely CMS, so I decided to document my...

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Resize Images on the Fly with Optimizely DXP's New CDN Feature

With the latest release, you can now resize images on demand using the Content Delivery Network (CDN). This means no more storing multiple versions...

Satata Satez | Dec 19, 2024