Problem statement
1) I have a template "Article" with 3 fields "Title" - Single line text "Description" - rich-text "Category" - drop-link
2) I created a computed field for "category" to get the "categoryvalue"
3) I created a category, Article item in English version everything worked fine
4) I create the same item in different language say "Thai"
Issue - I am always getting "CategoryValue" in English even though I have created "Thai" versions of all the items.
What I researched
I came across some blogs which says that "Indexing always run in default language" and also being suggested to use Sitecore.Globalization.LanguageSwitcher
while writing code for computed index.
What Ii debug
While debugging I found that at the time of indexing Sitecore.Context.Language
is always English
and after using LanguageSwitcher
things worked i.e. I am getting language-specific values
What is want to understand
1) Is it correct the computed indexes are always executed in English language?
2) Is applying "sitecore.Globalization.LanguageSwitcher " only solution or is there any configuration which i am missing here?
Code Snippet
Please note I have commented the language switcher via which things are working fine
public override object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
if (item == null || item.Paths.Path.Contains(SearchConstant.StandardValues)) return null;
GroupedDroplinkField contentTypeField = null;
if (item.IsDerived(new ID(SearchConstant.BaseArticleTemplateID)) )
{
contentTypeField = item.Fields[SearchConstant.ContentTypeField];
}
if (contentTypeField == null || contentTypeField.TargetItem == null) return null;
if (contentTypeField != null && contentTypeField.TargetItem != null)
//Please note if i dont put LanguageSwitcher then it always give me english content
// using (new Sitecore.Globalization.LanguageSwitcher(item.Language.Name))
// {
return contentTypeField.TargetItem.Fields[SearchConstant.Name].Value;
//}
return null;
}
Code snippet for Multilist field
public override object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
if (item == null)
{
return null;
}
List<string> featuredTagList = null;
if ((item.IsDerived(new ID(SearchConstant.BaseArticleTemplateID)))
{
MultilistField listField = item?.Fields["FeaturedTags"];
if (listField?.TargetIDs != null && listField.TargetIDs.Length > 0)
{
//using (new Sitecore.Globalization.LanguageSwitcher(item.Language.Name))
//{
var listItems = listField.GetItems();
featuredTagList = (from listItem in listItems where listItem?.Fields[SearchConstant.Name] != null let fieldValue = listItem.Fields[SearchConstant.Name].Value select !string.IsNullOrEmpty(fieldValue) ? fieldValue : listItem.Name).ToList();
//}
}
}
return featuredTagList;
}
}