Skip to content

Instantly share code, notes, and snippets.

@frank184
Created September 21, 2016 23:35
Show Gist options
  • Save frank184/5c0833d25b427633f89b50de0d65b4a3 to your computer and use it in GitHub Desktop.
Save frank184/5c0833d25b427633f89b50de0d65b4a3 to your computer and use it in GitHub Desktop.
Ruby on Rails - I18n implementation
# 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
# 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