Try our conversational search powered by Generative AI!

Sergii Vorushylo
Jul 25, 2012
  9688
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog