Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Jun 17, 2014 @ 13:37
    Ismail Mayat
    1

    GetDictionaryValue returns empty string

    I have controller and view its called on page load and everything loads fine. In the view i am doing 

    umbraco.GetDictionaryValue(key);

    again all works fine.  I also have a show more button this is ajaxlink and makes ajax call to the controller everyhting loads fine except for dictionary items i get empty string, its the same key and it has value, just seems to not like an ajax call. Any ideas??  I am using umbraco 6.1.6

    Regards


    Ismail

  • Dirk De Grave 4537 posts 6007 karma points MVP 3x admin c-trib
    Jun 17, 2014 @ 13:52
    Dirk De Grave
    0

    Yup, seen it before (having had the same issues) > Seem to loose "culture" info on ajax call. I'm always adding a extra parameter and send in either a node id of the current language I'm working on or a culture as string (eg nl-BE, ....) and set culture on the current thread before trying to fetch dictionary items.

    if there's a better method, i'd love to hear 'bout it.

    /Dirk

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Jun 17, 2014 @ 15:25
    Ismail Mayat
    0

    Dirk,

    you got the code snippet to set the culture on the thread?  Also I wonder if in the controller there is way to tell if its an ajax call so then i can set it there?

    Regards

     

    Ismail

  • Dirk De Grave 4537 posts 6007 karma points MVP 3x admin c-trib
    Jun 17, 2014 @ 15:31
    Dirk De Grave
    104

    Yup, sure, it should be as simple as 

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-BE");
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-BE);

    you may need to change the actual culture ;)

    Again, it may not be "best practise", but at least this is what works!

    One of the things I remember when looking into this is that even though you might be in a surface controller, the publishedContentRequest.Culture (not sure if it's called this way) is not set, which is why we decided to set it ourselves (and we couldn't set the culture on that request object, it's readonly)

     

    /Dirk

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Jun 17, 2014 @ 15:37
    Ismail Mayat
    2

    Dirk,

    Sorted!!! In my controller i have

                if (ControllerContext.HttpContext.Request.IsAjaxRequest())
                {
                    //we lose culture and dictionary wont get labels therefore set it on the thread
                    string code = umbracoService.GetCultureCode();
                    //Get the culture info of the language code
                    CultureInfo culture = CultureInfo.CreateSpecificCulture(code);
                    Thread.CurrentThread.CurrentCulture = culture;
                    Thread.CurrentThread.CurrentUICulture = culture;
    
                }

    And it works. Cheers mate.

    Have some solution karma lol.

    Regards

    Ismail

  • Dirk De Grave 4537 posts 6007 karma points MVP 3x admin c-trib
    Jun 17, 2014 @ 15:38
    Dirk De Grave
    0

    Didn't even know .IsAjaxRequest() exists!! Good to know, #h5yr

  • Dirk De Grave 4537 posts 6007 karma points MVP 3x admin c-trib
    Jun 17, 2014 @ 15:39
    Dirk De Grave
    0

    Haha, I'm trying to join the 10k karma club this year ;)

  • z4kk 24 posts 82 karma points
    Aug 05, 2014 @ 12:12
    z4kk
    2

    Try add <globalization> with your default language into web.config

    <configuration>
     <system.web>
    <globalization uiCulture="ru-RU" culture="ru-RU" /> 
  • David Peck 636 posts 1667 karma points c-trib
    Nov 23, 2016 @ 15:00
    David Peck
    0

    Old post, but this is the best solution as far as I'm concerned.

  • Cristhian Amaya 52 posts 423 karma points
    Jun 19, 2015 @ 13:58
    Cristhian Amaya
    0

    I know this is an old thread but I had the same issue and I found a solution that might be worth sharing :)

    Basically I created a custom header (umb-language) and a message handler which reads that header and overrides the current thread culture based on that.

    So, enough theory, this is the message handler:

    public class LanguageOverrideHandler : DelegatingHandler
    {
        private const string Languageoverrideheader = "umb-language";
    
        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.Headers.Contains(Languageoverrideheader))
            {
                var language = request.Headers
                    .GetValues(Languageoverrideheader).FirstOrDefault() ?? "en-US";
                var cultureInfo = CultureInfo.GetCultureInfo(language);
    
                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
            }
    
            return base.SendAsync(request, cancellationToken);
        }
    

    }

    In your Web API config you need to activate this:

    public static void Configure(HttpConfiguration config)
    {
        config.MessageHandlers.Add(new LanguageOverrideHandler());
    }
    

    After adding that you just need to send the umb-header on every request. For example if you're using Angular, you could use something like this at the start of the Angular app:

    angular.module('myApp')
    .run(['$http', function($http) {
        $http.defaults.headers.common['umb-language'] = 'ru-RU'; // or whatever you want.
    }]);
    

    Cheers!

  • Abdul Hadi 2 posts 22 karma points
    5 days ago
    Abdul Hadi
    0

    Hello guys,

    I solved that problem changing this information below:

    I changed the value it was 4.6.1 to 4.5

    Best Regards, Abdul Hadi

Please Sign in or register to post replies

Write your reply to:

Draft