Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Setup Globalize plugin in Rails (See related posts)

Don't forget to edit config/database.yml first.

Terminal:
script/plugin install \
    http://svn.globalize-rails.org/svn/globalize/globalize/trunk 
rake globalize:setup 


…in config/environment.rb:
include Globalize
Locale.set_base_language 'en-US'


…in the app controller, before any other filter:
  before_filter :set_locale
  # FIXME, NOT TESTED YET, PROBABLY DOESN'T WORK.
  def set_locale
    if params[:locale]
      # The user has explicitly requested a locale.
      session[:locale] = params[:locale]
      Locale.set params[:locale]
    elsif session[:locale].empty?
      # The user has a current session.
      Locale.set session[:locale]
    elsif ! request.env["HTTP_ACCEPT_LANGUAGE"].empty?
      # We use the browser's locale settings.
      Locale.set request.env["HTTP_ACCEPT_LANGUAGE"][/[a-z]{2}-[a-z]{2}/i]
    else
      # the default locale defined in config/environment.rb is used.
    end
  end


…in the model:
class Blog < ActiveRecord::Base
  translates :title, :post
end

Comments on this post

thiagoarrais posts on Jul 03, 2006 at 16:59
There is a minor bug on the HTTP_ACCEPT_LANGUAGE clause, it doesn't accept general unlocalized languages specs like 'en' or 'pt'. The regexp won't match and Locale.set will receive nil

This slightly modified version does the job:

      # We use the browser's locale settings.
      Locale.set request.env["HTTP_ACCEPT_LANGUAGE"][/[^,;]+/]


comma and semicolon are the separators used on the HTTP Accept-Language Header Field, according to the RFC2616
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

You need to create an account or log in to post comments to this site.


Related Posts