Class: ActionController::Base

Inherits:
Object
  • Object
show all
Includes:
GetText::Rails
Defined in:
lib/gettext/rails.rb

Constant Summary collapse

@@gettext_domainnames =
[]
@@gettext_content_type =
nil
@@before_init_gettext =

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

(e.g.)

class ApplicationController < ActionController::Base
  before_init_gettext{|controller|
    cookies = controller.cookies
    if (cookies["lang"].nil? or cookies["lang"].empty?)
      GetText.locale = "zh_CN"
    else
      GetText.locale = cookies["lang"]
    end
  }
  init_gettext "myapp"
  # ...
end
[]
@@after_init_gettext =

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.)

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

Constants included from GetText::Rails

GetText::Rails::Rails

Constants included from GetText

GetText::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText::Rails

#_bindtextdomain, bindtextdomain, included

Methods included from GetText

N_, #Nn_, _, #add_default_locale_path, bindtextdomain, #bindtextdomain_to, bound_target, bound_targets, cached=, cached?, cgi, cgi=, charset=, clear_cache, create_mofiles, current_textdomain_info, each_textdomain, gettext, included, locale, locale=, msgmerge, msgmerge_all, n_, ngettext, ns_, nsgettext, output_charset, output_charset=, rgettext, rmsgfmt, rmsgmerge, s_, set_cgi, set_charset, set_locale, set_locale_all, set_output_charset, setlocale, sgettext, textdomain, #textdomain_to, update_pofiles

Class Method Details

.after_init_gettext(*methods, &block) ⇒ Object



159
160
161
162
# File 'lib/gettext/rails.rb', line 159

def self.after_init_gettext(*methods, &block)
  @@after_init_gettext += methods
  @@after_init_gettext << block if block_given? 
end

.before_init_gettext(*methods, &block) ⇒ Object



140
141
142
143
# File 'lib/gettext/rails.rb', line 140

def self.before_init_gettext(*methods, &block)
  @@before_init_gettext += methods
  @@before_init_gettext << block if block_given? 
end

.init_gettext(domainname, options = {}, content_type = "text/html") ⇒ 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”

    • :content_type - the content type. Default is “text/html”

    • :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).

Note: Don’t use content_type argument(not in options). They are remained for backward compatibility.

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

app/controller/blog_controller.rb:

require 'gettext/rails'

class BlogController < ApplicationController
  init_gettext "blog"
    :
    :
  end


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/gettext/rails.rb', line 188

def self.init_gettext(domainname, options = {}, content_type = "text/html")
  opt = {:charset => "UTF-8", :content_type => content_type}
  if options.kind_of? String
	# For backward compatibility
	opt.merge!(:charset => options, :content_type => content_type)
  else
	opt.merge!(options)
  end
  GetText.output_charset = opt[:charset]
  @@gettext_content_type = opt[:content_type]
  locale_path = opt[:locale_path]
  unless locale_path
	cal = caller[0]
	if cal =~ /app.controllers/
	  locale_path = File.join(cal.split(/app.controllers/)[0] + "locale")
	else
	  locale_path = File.join(RAILS_ROOT, "locale")
	end
  end

  unless @@gettext_domainnames.find{|i| i[0] == domainname}
	@@gettext_domainnames << [domainname, locale_path] 
  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
end

.textdomainnameObject

Gets the textdomain name of this controller. This returns the first textdomain which is bound in app/controller/*.rb.

Notice Deprecated since 1.8.



226
227
228
229
# File 'lib/gettext/rails.rb', line 226

def self.textdomainname
  textdomain = @@gettext_domainnames[0]
  textdomain ? textdomain[0] : nil
end

.textdomainsObject

Gets the textdomain name and path of this controller which is set with init_gettext. *(Since 1.8)*

  • Returns: [[textdomainname1, path1], [textdomainname2, path2], …]



235
236
237
# File 'lib/gettext/rails.rb', line 235

def self.textdomains
  @@gettext_domainnames
end

Instance Method Details

#call_methods_around_init_gettext(ary) ⇒ Object

:nodoc:



99
100
101
102
103
104
105
106
107
# File 'lib/gettext/rails.rb', line 99

def call_methods_around_init_gettext(ary)  #:nodoc:
  ary.each do |block|
	if block.kind_of? Symbol
	  send(block)
	else
	  block.call(self)
	end
  end
end

#init_content_typeObject

:nodoc:



91
92
93
94
95
96
97
# File 'lib/gettext/rails.rb', line 91

def init_content_type #:nodoc:
  if headers["Content-Type"] and /javascript/ =~ headers["Content-Type"]
	headers["Content-Type"] = "text/javascript; charset=#{GetText.output_charset}"
  elsif ! headers["Content-Type"]
	headers["Content-Type"] = "#{@@gettext_content_type}; charset=#{GetText.output_charset}"
  end
end

#init_gettextObject

:nodoc:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gettext/rails.rb', line 109

def init_gettext # :nodoc:
  cgi = nil
  if defined? request.cgi
    cgi = request.cgi
  end
  call_methods_around_init_gettext(@@before_init_gettext)
  init_gettext_main(cgi) if @@gettext_domainnames.size > 0
  call_methods_around_init_gettext(@@after_init_gettext)

  if ::RAILS_ENV == "development"
	@@before_init_gettext = []
	@@after_init_gettext = []
  end
end

#init_gettext_main(cgi) ⇒ Object

:nodoc:



85
86
87
88
89
# File 'lib/gettext/rails.rb', line 85

def init_gettext_main(cgi) #:nodoc:
  cgi.params["lang"] = [params["lang"]] if params["lang"]
  set_cgi(cgi)
  set_locale_all(nil)
end