Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if items exist in the current language?

Tags:

sitecore

I have a Sitecore solution where there are 3 different languages enabled. On top of the page, there is a link to each language. When you click this link, you get the current page you are standing on, in the selected language.

But not all pages are translated into all languages. So if I am standing on page x in English language, and this page is only available in English and German but not Chinese, then the Chinese link should not be shown.

So the question is - How do I check if the current item has a version of a specific language?

like image 471
brother Avatar asked Nov 22 '11 18:11

brother


1 Answers

To see if there is a version of the current item you can do this: Sitecore.Context.Item.Versions.Count > 0

[updated for comment]

I don't claim that this is the most efficient way to determine if an item has a version in a language, but this will work:

bool hasVersion = HasLanguageVersion(Sitecore.Context.Item, "en");

private bool HasLanguageVersion(Sitecore.Data.Items.Item item, string languageName)
{
    var language = item.Languages.FirstOrDefault(l => l.Name == languageName);
    if (language != null)
    {
        var languageSpecificItem = global::Sitecore.Context.Database.GetItem(item.ID, language);
        if (languageSpecificItem != null && languageSpecificItem.Versions.Count > 0)
        {
            return true;
        }
    }
    return false;
}
like image 86
Sean Kearney Avatar answered Nov 09 '22 11:11

Sean Kearney