Skip to content
sergeyshushlyapin edited this page Mar 6, 2015 · 10 revisions

Important:

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

Important:

to be released in 0.25.0...
Auto-translate is disabled by default

FakeDb supports a simple localization mechanism. You can call the Translate.Text() or Translate.TextByLanguage() methods to get a fake localized version of the original text.

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

  • Translate.Text("Hello!) -> "Hello!*" if the FakeDb.AutoTranslate setting is set to true
  • Translate.Text("Hello!) -> "en:Hello!" if the FakeDb.AutoTranslatePrefix setting is set to {lang}:
  • Translate.Text("Hello!) -> "Hello!_en" if the FakeDb.AutoTranslateSuffix setting is set to _{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 when needed

Samples

[Fact]
public void HowToCheckTranslateTextIsCalled()
{
  const string Phrase = "Welcome!";

  // Enable the 'FakeDb.AutoTranslate' setting.
  // It can be done either statically in the 'App.config' file or 
  // dynamically in a particular test.
  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db())
  {
    db.Configuration.Settings.AutoTranslate = true;

    // translate
    string translatedPhrase = Sitecore.Globalization.Translate.Text(Phrase);

    // note the '*' symbol at the end of the translated phrase
    Xunit.Assert.Equal("Welcome!*", translatedPhrase);
  }
}
[Fact]
public void HowToUnitTestLocalization()
{
  // init languages
  Sitecore.Globalization.Language en = Sitecore.Globalization.Language.Parse("en");
  Sitecore.Globalization.Language da = Sitecore.Globalization.Language.Parse("da");

  const string Phrase = "Welcome!";

  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db())
  {
    db.Configuration.Settings.AutoTranslate = true;
    db.Configuration.Settings.AutoTranslatePrefix = "{lang}:";

    // translate
    string enTranslation = Sitecore.Globalization.Translate.TextByLanguage(Phrase, en);
    string daTranslation = Sitecore.Globalization.Translate.TextByLanguage(Phrase, da);

    Xunit.Assert.Equal("en:Welcome!", enTranslation);
    Xunit.Assert.Equal("da:Welcome!", daTranslation);
  }
}
Clone this wiki locally