#Sitecore’s Fallback Language Switcher

So for me and many others it was that time of year to set your clocks back in October. This time it was Fall back. Which reminded me. I recently discovered a method that lets me turn off fallback language when I need to in code. There are certain thing I wanted language fallback on and certain things I didn’t want it on for. Sitemap/Alternative Links was something that I only wanted to show links that have language versions for. Using this switcher I only get the items that we have a version a created a language for.

using (new LanguageFallbackItemSwitcher(false))
{
     var itemChildren = Sitecore.Context.Item.Children;

     foreach (Item childItem in itemChildren)
     {
          var urlOpt = new ItemUrlBuilderOptions
          {
               LanguageEmbedding = LanguageEmbedding.Always
          };

          foreach (var lang in item.Languages)
          {
               var langItem = childItem.Database.GetItem(childItem.ID, lang);

//If the switcher is set to true you would get all language versions of an item including fallback
//In this case we just want versions that are created and not using fallback.
      if (langItem.Versions.Count <= 0)
      {
          continue;
      }

urlOptions.Language = lang;
var itemUrl = LinkManager.GetItemUrl(langItem, urlOpt);
      links.Add(new KeyValuePair<string, string>(lang.Name, SiteHelper.EnsureScheme(itemUrl)));
                    }
                }
}

Simple yet effective.