Created
November 20, 2009 09:35
-
-
Save silvioq/239381 to your computer and use it in GitHub Desktop.
Selección de idioma
This file contains hidden or 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
class ApplicationController < ActionController::Base | |
before_filter :set_charset, :set_locale | |
# Setea el charset con el que vamos a hablar con el | |
# cliente | |
def set_charset | |
content_type = headers["Content-Type"] || "text/html" | |
if /^text\//.match(content_type) | |
headers["Content-Type"] = "#{content_type}; charset=utf-8" | |
end | |
end | |
# Establece el idioma. | |
def set_locale | |
default_locale = 'en-US' | |
@locale = params[:locale] || session[:locale] || | |
get_request_language || default_locale | |
session[:locale] = @locale | |
begin | |
Locale.set @locale | |
rescue | |
Locale.set default_locale | |
end | |
end | |
private | |
def get_request_language | |
request_language = request.env['HTTP_ACCEPT_LANGUAGE'] | |
request_language.nil? ? nil : request_language[/[^,;]+/] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment