Class: ActionController::Base

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/gettext_rails/action_controller.rb

Class Method Summary collapse

Methods included from GetText

#create_mofiles, #create_mofiles_org

Class Method Details

.after_init_gettext(*filters, &block) ⇒ Object

Append a block which is called after initializing gettext on the each WWW request.

The GetText.locale is set the locale which bound to the textdomains when gettext is initialized.

(e.g.1)

class ApplicationController < ActionController::Base
  after_init_gettext {|controller|
    L10nClass.new(GetText.locale)
  }
  init_gettext "foo"
  # ...
end

(e.g.2)

class ApplicationController < ActionController::Base
  def sample_foo
    L10nClass.new(GetText.locale)
  end
  after_init_gettext :sample_foo
  init_gettext "foo"
  # ...
end


68
69
70
# File 'lib/gettext_rails/action_controller.rb', line 68

def self.after_init_gettext(*filters, &block)
  after_init_locale(*filters, &block)
end

.before_init_gettext(*filters, &block) ⇒ Object

Append a block which is called before initializing gettext on the each WWW request.

(e.g.1)

class ApplicationController < ActionController::Base
  before_init_gettext{|controller|
    p "before_init_gettext is called."
  }
  init_gettext "myapp"
  # ...
end

(e.g.2)

class ApplicationController < ActionController::Base
  def sample_foo
    p "sample_foo is called."
  end
  before_init_gettext :sample_foo
  init_gettext "myapp"
  # ...
end


41
42
43
# File 'lib/gettext_rails/action_controller.rb', line 41

def self.before_init_gettext(*filters, &block)
  before_init_locale(*filters, &block)
end

.init_gettext(domainname, options = {}) ⇒ Object

Bind a ‘textdomain’ to all of the controllers/views/models. Call this instead of GetText.bindtextdomain.

  • textdomain: the textdomain

  • options: options as a Hash.

    • :charset - the output charset. Default is “UTF-8”

    • :locale_path - the path to locale directory. Default is RAILS_ROOT/locale or root directory/locale.

locale is searched the order by params > “lang” value of QUERY_STRING > “lang” value of Cookie > HTTP_ACCEPT_LANGUAGE value > Default locale(en). And the charset is set order by “the argument of bindtextdomain” > HTTP_ACCEPT_CHARSET > Default charset(UTF-8). Refer Ruby-Locale for more details.

If you want to separate the textdomain each controllers, you need to call this function in the each controllers.

app/controller/blog_controller.rb:

class BlogController < ApplicationController
  init_gettext "blog"
    :
  end


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gettext_rails/action_controller.rb', line 90

def self.init_gettext(domainname, options = {})
  opt = {:charset => "UTF-8"}.merge(options)

  set_output_charset(opt[:charset])
  locale_path = opt[:locale_path]
  unless locale_path
    cal = caller[0]
    if cal =~ /app.controllers/
      # Don't use RAILS_ROOT first, for RailsEngines. 
      locale_path = File.join(cal.split(/app.controllers/)[0] + "locale")
    else
      locale_path = File.join(RAILS_ROOT, "locale")
    end
  end

  bindtextdomain(domainname, {:path => locale_path})

  if defined? ActiveRecord::Base
    textdomain_to(ActiveRecord::Base, domainname) 
    textdomain_to(ActiveRecord::Validations, domainname)
  end
  textdomain_to(ActionView::Base, domainname) if defined? ActionView::Base
  textdomain_to(ApplicationHelper, domainname) if defined? ApplicationHelper
  textdomain_to(ActionMailer::Base, domainname) if defined? ActionMailer::Base
  textdomain_to(ActionView::Helpers, domainname) if defined? ActionView::Helpers
end