Try our conversational search powered by Generative AI!

Sergii Vorushylo
Jul 25, 2012
  9702
(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
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog