Created
September 21, 2016 23:35
-
-
Save frank184/5c0833d25b427633f89b50de0d65b4a3 to your computer and use it in GitHub Desktop.
Ruby on Rails - I18n implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
include Devise::Redirects | |
# Prevent CSRF attacks by raising an exception. | |
# For APIs, you may want to use :null_session instead. | |
protect_from_forgery with: :exception | |
include Locale | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/concerns/locale.rb | |
module Locale | |
extend ActiveSupport::Concern | |
included { before_action :set_locale, :set_content_language } | |
private | |
def set_locale | |
I18n.locale = session[:locale] = params[:locale] || session[:locale] || I18n.default_locale | |
end | |
def set_content_language | |
response.headers["Content-Language"] = I18n.locale.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment