8

Is it possible to determine the browser language setting of a guest user on an external site from Apex or VF?

I'm leveraging custom labels and they seem to respond to the browser language setting of a guest user, however I also have some dynamic picklist values that I need to display in the correct language as well. Has anyone had experience with this or know of a way to get the browser language setting value?

Thanks!

1 Answer 1

4

The normal way to get that value is by Accept-Language request header.

I think this is not possible to get by Apex.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm#RequestHeadersSectionTitle

//this is returning null
ApexPages.currentPage().getHeaders().get('Accept-Language');

A possible workaround could be to get it on VF using this technique in javascript

var language = window.navigator.userLanguage || window.navigator.language; //works on IE/CH/FF
if(language){
    nowDoSomethingWithIt(language);
}else{
  //other browsers gonna be a little tricky
  $.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        language = headers['Accept-Language'];
        nowDoSomethingWithIt(language);
    }
  });
}

Once you have the language you could rerender the picklist using a

@remoteAction 
4
  • 1
    ApexPages.currentPage().getHeaders().get('Accept-Language'); worked fine for me, thanks!
    – Phil B
    Jun 27, 2013 at 19:35
  • Great @PhilB, which API version are you using? Jun 27, 2013 at 22:19
  • That class is using API version 25
    – Phil B
    Jul 10, 2013 at 21:03
  • Just implemented ApexPages.currentPage().getHeaders().get('Accept-Language'); in a multilingual site and it's working for Chrome, Firefox, Safari and Edge. Mar 21, 2018 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.