Class | ActionController::Base |
In: |
lib/gettext_rails/action_controller.rb
|
Parent: | 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
# File lib/gettext_rails/action_controller.rb, line 68 68: def self.after_init_gettext(*filters, &block) 69: after_init_locale(*filters, &block) 70: end
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
# File lib/gettext_rails/action_controller.rb, line 41 41: def self.before_init_gettext(*filters, &block) 42: before_init_locale(*filters, &block) 43: end
Bind a ‘textdomain’ to all of the controllers/views/models. Call this instead of GetText.bindtextdomain.
locale is searched the order by params["lang"] > "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
# File lib/gettext_rails/action_controller.rb, line 90 90: def self.init_gettext(domainname, options = {}) 91: opt = {:charset => "UTF-8"}.merge(options) 92: 93: set_output_charset(opt[:charset]) 94: locale_path = opt[:locale_path] 95: unless locale_path 96: cal = caller[0] 97: if cal =~ /app.controllers/ 98: # Don't use RAILS_ROOT first, for RailsEngines. 99: locale_path = File.join(cal.split(/app.controllers/)[0] + "locale") 100: else 101: locale_path = File.join(RAILS_ROOT, "locale") 102: end 103: end 104: 105: bindtextdomain(domainname, {:path => locale_path}) 106: 107: if defined? ActiveRecord::Base 108: textdomain_to(ActiveRecord::Base, domainname) 109: textdomain_to(ActiveRecord::Validations, domainname) 110: end 111: textdomain_to(ActionView::Base, domainname) if defined? ActionView::Base 112: textdomain_to(ApplicationHelper, domainname) if defined? ApplicationHelper 113: textdomain_to(ActionMailer::Base, domainname) if defined? ActionMailer::Base 114: textdomain_to(ActionView::Helpers, domainname) if defined? ActionView::Helpers 115: end