-
Notifications
You must be signed in to change notification settings - Fork 36
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 theSitecore.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!*"
ifFakeDb.AutoTranslate
istrue
-
Translate.Text("Hi!")
returns"en:Hi!"
ifFakeDb.AutoTranslatePrefix
is{lang}:
-
Translate.Text("Hi!")
returns"Hi!_en"
ifFakeDb.AutoTranslateSuffix
is_{lang}
There are a couple of ways to enable the auto translation:
- Set the
FakeDb.AutoTranslate*
settings statically in theApp.config
file - Set the settings dynamically in tests using the
db.Configuration
context
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);
}
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);
}