Skip to content

Instantly share code, notes, and snippets.

@tristanm
Created September 4, 2015 05:46
Show Gist options
  • Save tristanm/e85b635499b720863ff6 to your computer and use it in GitHub Desktop.
Save tristanm/e85b635499b720863ff6 to your computer and use it in GitHub Desktop.
Concerns::Localizable
module Concerns::Localizable
extend ActiveSupport::Concern
included do
around_action :localize
end
def localize
return false if set_locale === false
yield
set_language_header
end
def default_url_options(options = {})
super.merge({ locale: I18n.locale })
end
private
def set_locale
if request.get? && params[:locale].blank?
redirect_to request.query_parameters.merge(locale: I18n.default_locale)
return false
else
I18n.locale = params[:locale] || I18n.default_locale
end
end
def set_language_header
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