Preparing Rails to use gettext


Short post, as a note for future. To use gettext with RoR you have to do


  1. install gem

    sudo gem install gettext


  2. change controllers/application.rb to

    require 'gettext/rails'

    class ApplicationController < ActionController::Base
    init_gettext 'myapp'
    helper :all # include all helpers, all the time

    # See ActionController::RequestForgeryProtection for details
    # Uncomment the :secret if you're not using the cookie session store
    protect_from_forgery # :secret => 'fda6e0e8ddfd10693079c4bcdb4b6874'

    # See ActionController::Base for details
    # Uncomment this to filter the contents of submitted sensitive data parameters
    # from your application log (in this case, all fields with names like "password").
    # filter_parameter_logging :password
    end


  3. add add the end of config/environment.rb this code

    require 'gettext/rails'


  4. add to Rackfile some tasks

    require 'gettext/utils'
    desc "Create mo-files for L10n"
    task :makemo do
    GetText.create_mofiles(true, "po", "locale")
    end

    desc "Update pot/po files to match new version."
    task :updatepo do
    MY_APP_TEXT_DOMAIN = "myapp"
    MY_APP_VERSION = "myapp 1.1.0"
    GetText.update_pofiles(MY_APP_TEXT_DOMAIN,
    Dir.glob("{app,lib}/**/*.{rb,rhtml}"),
    MY_APP_VERSION)
    end


  5. I am not sure, but perhaps you also need libintl-gettext-ruby package in your system (I am spiking about Ubuntu Linux)




Very important thing, you have to restart web server to commit this changes. Also if you get 500 Internal Server Error you have to create file gettext.rb in config/initializers
config/initializers/gettext.rb


module ActionView
class Base
delegate :file_exists?, :to => :finder unless respond_to?(:file_exists?)
end
end

This bug was reported here but in last version of rails (2.1.0) it is also available. Solution was presented on
zargony.com.



More information:


No comments: