Detect Browser Language in Rails

Detect browser language in Rails

Here's the solution:

2.6 Setting the Locale from the Client Supplied Information


In specific cases, it would make sense to set the locale from client-supplied information, i.e. not from the URL. This information may come for example from the users’ preferred language (set in their browser), can be based on the users’ geographical location inferred from their IP, or users can provide it simply by choosing the locale in your application interface and saving it to their profile. This approach is more suitable for web-based applications or services, not for websites — see the box about sessions, cookies and RESTful architecture above...

One source of client supplied information would be an Accept-Language HTTP header. People may set this in their browser or other clients (such as curl)...

Another way of choosing the locale from client information would be to use a database for mapping the client IP to the region, such as GeoIP Lite Country...

You can also provide users of your application with means to set (and possibly over-ride) the locale in your application interface, as well...

extract browser language rails 3

Your regular expression is only looking for two letters try this:

browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].try(:scan, /^[a-z-]{2,5}/).try(:first).try(:to_sym)

This will work with two to five character codes with lower case letters or dashes.

This is just a start you may need to refine this regular expression more.

Rails SpreeCommerce - Does not detect browser language settings

You need to add some logic to your application to set the Rails.locale to one of the locales which you support based upon the request Accept-Language passed in.

This functionality isn't supported directly by spree-i18n, but designed to be implemented by your application.

set locale automatically in ruby on rails

try using gem geocoder and i18n_data gem and have a before_filter to a method which does

def checklocale
I18n.locale = I18nData.country_code(request.location.country)
end

How do I set the browser language in Cucumber / Capybara?

Got it!

I had the right line, the problem was elsewhere.

page.driver.header 'Accept-Language', language

This works fine, the problem was that my Scenario was as follows.

Scenario:
Given I am an anonymous user
And the browser language is fr
Then I should see "Bonjour"

And it should have been

Scenario:
Given the browser language is fr
And I am an anonymous user
Then I should see "Bonjour"

The 'I am an anonymous user' step was just doing a sign out first. It would seem that the browser language must be set before any other steps are taken which use the browser.

How to set language based on user profile?

You are currently just using the cookie to keep track of the selected locale, a different device or browser will have its own cookies so they fall back to the default.

You have to persist the selected locale for a user on the server side (user profile) and then check in the ApplicationController if you have a logged in user (devise provides helpers as far as I recall) and if so set the I18.locale to this stored value otherwise you can just fall back to your current cookie based solution (for not logged in users).

Another (fallback) approach is to guess the locale by the browser specific headers which will be sent with the request. Say if the browser locale is set to french the user might also want to see the content in french.



Related Topics



Leave a reply



Submit