1

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.
Moamen Naanou
  • 1,683
  • 1
  • 21
  • 45
  • Are you trying to switch the language for the user or just get a specific item in a different language? – jammykam Aug 18 '16 at 13:48
  • 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 Aug 18 '16 at 13:52
  • so your issue is detecting the current language? [I had similar problems](http://stackoverflow.com/questions/33522061/why-is-my-sitecore-context-language-resetting-in-controller) – Liam Aug 18 '16 at 13:57
  • not detecting current language, just need to get item in different language which already I'm able to see it in DB – Moamen Naanou Aug 18 '16 at 14:03
  • 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 Aug 18 '16 at 14:05
  • I have items with two languages, I can't get the Arabic version through the code in question and just to be clear, I'm confirming that the items are saved in DB properly. I'm not getting error messages but I'm getting empty fields for 'ar' language. – Moamen Naanou Aug 18 '16 at 14:09
  • I updated the question – Moamen Naanou Aug 18 '16 at 14:20
  • So this does seem a repition of my issue (linked question). I couldn't set the language, it simply didn't work (bug?). I tried to intercept and change the language in several places but it always ended up just being english when it got to my controller. In the end I bypassed sitecores language altogether and stored it in my own context variable... Not sure that's that helpful but I spent quite some time trying to fix this issue. – Liam Aug 18 '16 at 14:23
  • I'm guessing `Sitecore.Context.Language` is `En` or something when you execute your call to `GetItem`? – Liam Aug 18 '16 at 14:25
  • Test `HttpContext.Current.Items["sc_Language"]` at both ends of your code. This is what sitecore actually stores the language in. What do you get? – Liam Aug 18 '16 at 14:26
  • I checked it, it is 'ar-AE' !! – Moamen Naanou Aug 18 '16 at 14:34
  • So when you call `GetItem` the `Sitecore.Context.Language` and `HttpContext.Current.Items["sc_Language"]` is ar? You do have an ar version in sitecore right? – Liam Aug 18 '16 at 14:39
  • yeah exactly it is same – Moamen Naanou Aug 18 '16 at 14:40
  • That should be working. It sounds like you don't have an ar language version of the item or your languages are not configured correctly in sitecore. Do you see a 1 next to Arabic in the language drop down? See the last image in [my question](http://stackoverflow.com/q/33522061/542251) – Liam Aug 18 '16 at 14:41
  • yeah u mean version 1? all items I have are versioned 1 – Moamen Naanou Aug 18 '16 at 14:42
  • and every field is populated in this version? The version is effectively a totally different item. If this doens't work. I give in, this sounds correct – Liam Aug 18 '16 at 14:46
  • I couldn't get you exactly, but when I change the language, I'm getting the item (same ID and same url) but other fields (name, value ..) comes empty – Moamen Naanou Aug 18 '16 at 14:48
  • Maybe a silly idea, but is your item published in Arabic? Can you switch to your web database and verify? – Gatogordo Aug 18 '16 at 14:53
  • It's published properly – Moamen Naanou Aug 18 '16 at 15:31
  • This sounds similar to this where the ar language was returning empty fields- could be related? http://stackoverflow.com/questions/30913668/sitecore-empty-field-in-multi-culture – Ian Graham Aug 18 '16 at 21:23
  • There are already so many comments on this one, but noone speaks about `using (new SC.Globalization.LanguageSwitcher(SC.Globalization.Language))`. You can get your item in any Language using LanguageSwitcher. Is the question solved? – Pavel Jounda Aug 19 '16 at 10:35

2 Answers2

0

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

Pavel Jounda
  • 219
  • 5
  • 14
-1

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

grg
  • 584
  • 4
  • 17
  • 1
    This answer has been flagged as low quality. If it answers the question, consider adding a bit of text to explain how it works. – lmo Aug 24 '16 at 22:26