2

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?

6
  • Do you mean in the content editor? or being rendered to the page?
    – Richard Seal
    Jul 11, 2017 at 16:18
  • 1
    There is nothing like that, that I know of. The closest thing I can think of is the Translate chunk in the "Versions" tab in the content editor. Which just shows two language versions next to each other. Jul 11, 2017 at 19:18
  • Unless you're relying on Language Fallback, it's not possible, and even then it will be clunky setting up the fallback. There's no reason you can just enter text in whatever language you want though, Sitecore will not care.
    – jammykam
    Jul 11, 2017 at 19:21
  • Better option is to make use of the "Version" tab of Sitecore. You can add multiple languages in the "More Languages" link (besides Version tab), and put in different language contents. While rendering the content, we need to check for the specific country language and get the content for that particular language to be rendered on the site. Jul 12, 2017 at 11:16
  • 1
    @Kizzie That's not a limitation of Sitecore, but maybe something in your implementation? I didn't have any issues copy+pasting text from that site into a clean install of Sitecore: imgur.com/a/tpqfg
    – jammykam
    Jul 12, 2017 at 17:04

2 Answers 2

1

Have you tried using Sitecore's LanguageSwitcher? It's a fantastic and IMHO very underrated and underused little IDisposablethat effectively allows you to temporarily change the Sitecore.Context.Language until the LanguageSwitcher is disposed.

Getting Started with the 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

Using the LanguageSwitcher for Your Use-Case

First, let's align on some assumptions:

  • You have a multi-lingual item MyItem
  • MyItem has a Rich-Text field on it, MyContent, that holds the content meant to be rendered for that item
  • MyItem has one language-version for each language that has content that you wish to render, i.e. you created a language-version and populated the MyContent field for each language that you wish to render content for

Building Your View Model

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;

Rendering the Content

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.

0

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:

Multiple Languages in RTE

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.