Skip to content

Instantly share code, notes, and snippets.

@alec-c4
Created July 1, 2009 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alec-c4/138659 to your computer and use it in GitHub Desktop.
Save alec-c4/138659 to your computer and use it in GitHub Desktop.
locale filter
###### 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