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
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.
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?
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)
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;
}
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.
}]);
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
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
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
Yup, sure, it should be as simple as
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
Dirk,
Sorted!!! In my controller i have
And it works. Cheers mate.
Have some solution karma lol.
Regards
Ismail
Didn't even know .IsAjaxRequest() exists!! Good to know, #h5yr
Haha, I'm trying to join the 10k karma club this year ;)
Try add <globalization> with your default language into web.config
Old post, but this is the best solution as far as I'm concerned.
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:
}
In your Web API config you need to activate this:
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:
Cheers!
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
is working on a reply...