Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails persisted locale

I'm about to start adding I18n support for a Rails app I'm currently working on. In the past I have set the locale value from the URL. I just was wondering how bad practice it'd be to persist the locale?

So instead of something like this:

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

Do something like

before_filter :set_locale

def set_locale
  I18n.locale = current_user.try(:locale) || I18n.default_locale
end

It looks like FB persists the locale, what are the trade-offs of this schema? how might this affect SEO stuff?

Thanks!

like image 592
jpemberthy Avatar asked May 14 '12 16:05

jpemberthy


1 Answers

I've faced these issues and ended up using this strategy:

  • locale in URL (allows page caching and direct linking in specific language)
  • store locale in user cookie

If locale is not present in URL, try (in order):

  • retrieve from cookie
  • guess from geolocation
  • guess from browser header accept language
  • fallback to app default

Hope that helps; let me know if you'd like any points elaborated on.

like image 56
Zubin Avatar answered Oct 11 '22 17:10

Zubin