0

In a solution with Item level language fallback enabled, how do I tell if this is in effect for the current item? I would like to set the html element lang attribute to the language that is being displayed.

For example, if Spanish is requested and a Spanish version exists, it should be set to lang='es'. But if the item falls back to English, it should be set to lang='en'.

I tried:

var language = Sitecore.Context.Item.Versions.Count > 0 ?
            Sitecore.Context.Item.Language :
            Sitecore.Context.Item.GetFallbackItem().Language;

But this always returns the requested language, as the count appears to be greater than zero due to Language fallback.

1 Answer 1

0

Update: Just use IsFallback property on the item. Duh. (Thanks Amish Naik!)

The hard way:

Use the LanguageFallbackItemSwitcher class to temporarily disable fallback:

Language language;
Language itemLanguage = Sitecore.Context.Item.Language;
Language fallbackLanguage = Sitecore.Context.Item.GetFallbackItem().Language;
using (new LanguageFallbackItemSwitcher(false))
{
    language = Sitecore.Context.Item.Versions.Count > 0 ? itemLanguage: fallbackLanguage; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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