Created
July 1, 2009 07:53
-
-
Save alec-c4/138659 to your computer and use it in GitHub Desktop.
locale filter
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
###### application_controller.rb | |
before_filter :set_locale | |
def set_locale | |
locale = params[:locale] || cookies[:locale] || TLD_LOCALES[request.host.split('.').last] || request.headers['HTTP_ACCEPT_LANGUAGE'].split(',').map { |l| l.split(';').first } | |
locale = DEFAULT_LOCALE unless (SUPPORTED_LOCALES).include?(locale.to_s) | |
I18n.locale = locale.to_s | |
cookies[:locale] = locale unless (cookies[:locale] && cookies[:locale] == locale) | |
end | |
###### environment.rb | |
DEFAULT_LOCALE = "en" | |
LOCALES_DIRECTORY = "#{RAILS_ROOT}/config/locales/" | |
locale_files = Dir["#{LOCALES_DIRECTORY}/*.{rb,yml}"] | |
SUPPORTED_LOCALES = (locale_files.collect do |locale_file| | |
File.basename(File.basename(locale_file, ".rb"), ".yml") | |
end + [DEFAULT_LOCALE]).uniq | |
TLD_LOCALES = { | |
'.ru' => :ru, | |
'.eu' => :en, | |
'.net' => :en | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment