Skip to content

Localization

sergeyshushlyapin edited this page May 20, 2015 · 10 revisions

Important:

If you are running Sitecore 7.5 or higher, consider using Sitecore.Abstractions.ITranslate interface from the Sitecore.Abstractions assembly.

FakeDb supports a simple localization mechanism. You can call the static Sitecore.Globalization.Translate.Text() method and receive a fake localized version of the original text.

There are a couple of options how the text may look like:

  • Translate.Text("Hi!") returns "Hi!*" if FakeDb.AutoTranslate is true
  • Translate.Text("Hi!") returns "en:Hi!" if FakeDb.AutoTranslatePrefix is {lang}:
  • Translate.Text("Hi!") returns "Hi!_en" if FakeDb.AutoTranslateSuffix is _{lang}

There are a couple of ways to enable the auto translation:

  • Set the FakeDb.AutoTranslate* settings statically in the App.config file
  • Set the settings dynamically in tests using the db.Configuration context

Samples

Translate text by the context language
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db())
{
  db.Configuration.Settings.AutoTranslate = true;
  string translatedPhrase = Sitecore.Globalization.Translate.Text("Welcome!");

  // note the '*' symbol at the end of the translated phrase
  Xunit.Assert.Equal("Welcome!*", translatedPhrase);
}
Translate text by the given language
using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db())
{
  db.Configuration.Settings.AutoTranslate = true;
  db.Configuration.Settings.AutoTranslatePrefix = "{lang}:";

  var lang = Sitecore.Globalization.Language.Parse("da");

  // translate by the given language
  string translatedPhrase
    = Sitecore.Globalization.Translate.TextByLanguage("Welcome!", lang);

  Xunit.Assert.Equal("da:Welcome!", translatedPhrase);
}
You can’t perform that action at this time.