Skip to content

Instantly share code, notes, and snippets.

@Bahanix
Last active February 11, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bahanix/90aed8f20ac8441271324f710bc8ed79 to your computer and use it in GitHub Desktop.
Save Bahanix/90aed8f20ac8441271324f710bc8ed79 to your computer and use it in GitHub Desktop.
Ruby on Rails i18n: using user locale
# db/migrates/*_add_locale_to_users.rb
class AddLocaleToUsers < ActiveRecord::Migration
def change
add_column :users, :locale, :string, default: "fr"
end
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_current_user
before_action :set_locale
private
def set_current_user
if session[:user_id]
@current_user = User.find(session[:user_id])
end
end
def set_locale
if @current_user.try(:locale)
I18n.locale = current_user.locale
end
end
end
@Bahanix
Copy link
Author

Bahanix commented Apr 29, 2016

À vous de créer le formulaire pour que l'utilisateur puisse mettre à jour sa locale en base de données :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment