Sergii Vorushylo
Jul 25, 2012
  9760
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024