Skip to content

Instantly share code, notes, and snippets.

@jordifierro
Last active March 30, 2016 09:40
Show Gist options
  • Save jordifierro/bad03cc0e0f525542bd625d7fc3b79f9 to your computer and use it in GitHub Desktop.
Save jordifierro/bad03cc0e0f525542bd625d7fc3b79f9 to your computer and use it in GitHub Desktop.
module Api::V1::Concerns
module Internationalizator
extend ActiveSupport::Concern
included do
before_action :set_locale
end
def set_locale
I18n.locale = extract_locale_from_accept_language_header ||
I18n.default_locale
end
def extract_locale_from_accept_language_header
req_lang = request.env['HTTP_ACCEPT_LANGUAGE']
if req_lang
language = req_lang.scan(/^[a-z]{2}/).first
language if I18n.available_locales.include?(language.to_sym)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment