Get the user locale from http headers

less than 1 minute read

If you want to provide default localization support for guest user in your website yo can locate their browser locale by the following code:


def extract_locale_from_header
unless request.env['HTTP_ACCEPT_LANGUAGE'].nil?
return request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
'en'
end

This way you can determine on server side the locale of the user currently entered your site.

Comments