Is it possible to have the English version of a page with multiple rich text editors and each rich text editor will show text in a different language? Or even in the same rich text editor?
How would I make this happen?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityIs it possible to have the English version of a page with multiple rich text editors and each rich text editor will show text in a different language? Or even in the same rich text editor?
How would I make this happen?
Have you tried using Sitecore's LanguageSwitcher
? It's a fantastic and IMHO very underrated and underused little IDisposable
that effectively allows you to temporarily change the Sitecore.Context.Language
until the LanguageSwitcher
is disposed.
LanguageSwitcher
You can use the LanguageSwitcher
like the following:
// context language: "en-US"
var multilingualItem = Sitecore.Context.Database.GetItem(id); // en-US version
using (new Sitecore.Globalization.LanguageSwitcher("fr-CA"))
{
// context language: "fr-CA"
multilingualItem = Sitecore.Context.Database.GetItem(id); // fr-CA version
}
// context language: "en-US"
multilingualItem = Sitecore.Context.Database.GetItem(id); // en-US version
LanguageSwitcher
for Your Use-CaseFirst, let's align on some assumptions:
You can use something similar to the following code to add the multilingual content for the MyContent field to your view model:
// get all of the languages that the item has a version in
var myItemLanguages =
// get all of the languages of the item
ItemManager.GetContentLanguages(myItem)
.Select(
lang =>
new
{
Language = lang,
// indicates whether the item has a version in the language
HasLanguage = ItemManager.GetVersions(myItem, lang).Count > 0
})
// only select languages that the item has a version in
.Where(langInfo => langInfo.HasLanguage)
.Select(langInfo => langInfo.Language);
// Tuple is used for demonstration only; feel free to change as needed
viewModel.MyContentModels = new List<Tuple<Language, string>>();
// iterate through the languages and add the field's value for each to the list
foreach (var language in myItemLanguages)
{
// change the context language to the current language
using (new Sitecore.Globalization.LanguageSwitcher(language))
{
// get the item in the current language
var myItemInLang = myItem.Database.GetItem(myItem.ID, language);
var myContentInLang = myItemInLang.Fields["MyContent"].Value;
// add a new Tuple to the list with the item's content and its associated language
myContentModels.Add(new Tuple<Language, string>(language, myContentInLang));
}
}
return viewModel;
In your rendering, you would then do something like the following to render content on your page similarly to how it was rendered on the page you liked to:
@foreach (var myContentModel in Model.MyContentModels)
{
<div>
<h3>@(myContentModel.Item1.Name)</h3>
<div>@Html.Raw(myContentModel.Item2)</div>
</div>
}
Of course, if you would like to have the language written in its own language you can always add support for that, but this should get you started.
Sitecore does not care what text or language you enter in the fields, whether that is Single Line Text field, Rich Text field or any of the other fields. There are no checks made, out of the box, to validate that you have for example only entered English text when Item lanuageen
is selected or Japanese is entered when ja-jp
is selected.
The simplest out of the box solution for what you are trying to achieve is to simply enter the text into the single Rich Text field in as many languages as you require one after the other:
Entering the text this way allows you to control the order of inclusion of paragraphs of text as well - maybe you want Chinese first on one page, but Japanese first on another.
You also do not need to deal with Language versions and/or fallback. Language versions may add an extra layer of complexity for your content authors which may not be needed, esp since looking at your site all the content is in English apart from a few blocks of text. Adding additional languages will also impact your publishing times, since items will be published for all languages, at least some checks need to be made whether an item in that language exists etc. This would be a big consideration for me when deciding whether to add additional languages given the scope of your multi-lingual content (i.e. a single text block).
If you are having issues with text being cut off after entering/pasting it, then it may be due to some customisations in your implemention of Sitecore, maybe something to now allow more than a certain length of text to be entered. You should investigate this issue rather than trying to solve using complex code in my opinion.