1
votes

I'm new to Sitecore and I have an issue while trying to retrieve item/s by specific language by the following steps:

  1. Added new language for an item through the content editor which (i.e "ar" and culture info is "ar-AE" )
  2. get the language by either of following (both fails):

    • SC.Globalization.Language lang = SC.Globalization.Language.Parse("ar-AE"); // and tried by replacing culture with ("ar")
    • SC.Globalization.Language lang = SC.Data.Managers.LanguageManager.GetLanguage("ar-AE"); // and also tried by replacing culture with ("ar")
  3. SC.Context.SetLanguage(lang, true);

The Response: I'm able to get only "en" items and when I change the language (by the above mentioned code), items are retrieved properly but fields are empty.

Note: I've checked Sitecore DB Master -> VersionedFields[Language] and found the other languages of the same item saved as "ar-AE" (only single item version is used)

Update:

I'm using filter to change the whole API language by the following code:

public class LanguageChecker : System.Web.Mvc.ActionFilterAttribute
{
    // Set Default Language
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        SC.Globalization.Language lang = SC.Data.Managers.LanguageManager.GetLanguage("en");

         if (filterContext.HttpContext.Request.Headers["Accept-Language"] != null || filterContext.HttpContext.Request.Headers["Accept-Language"].ToLower() == "ar")
        {
            // lang = SC.Globalization.Language.Parse("ar");
            lang = SC.Data.Managers.LanguageManager.GetLanguage("ar-AE");
        }

        SC.Context.SetLanguage(lang, true);
        base.OnActionExecuting(filterContext);
    }
}

and annotated this filter in the API Main Controller (just like any filter)

So, I'm calling the item by:

 SC.Data.Items.Item item = context.GetItem(SC.Data.ID.Parse(id));
  • in this case I don't have to change the language for each item.
2
Are you trying to switch the language for the user or just get a specific item in a different language? - jammykam
I need to get item by language and I think 'SC.Context.SetLanguage(lang, true);' on the parent API Controller's constructor will change it - Moamen Naanou
so your issue is detecting the current language? I had similar problems - Liam
not detecting current language, just need to get item in different language which already I'm able to see it in DB - Moamen Naanou
Ok, now I'm not clear what your problem is? Can you show a full code snippet and show exactly which part of your code isn't working - Liam

2 Answers

0
votes

Normally, to get the item in any language you use LanguageSwitcher

var item = Sitecore.Context.Item;
using (new Sitecore.Globalization.LanguageSwitcher("ar"))
{
    arItem = item.Database.GetItem(item.ID);
}

This usage will dispose the LanguageSwitcher and reset the original language

-1
votes

Try using static methods on the Database class. Something like this might work: Sitecore.Context.Database.GetItem(item.ID, Language.Parse("ar"));