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, available_locales, bindtextdomain, included, normalized_locale

Methods included from GetText

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

Class Method Details

.after_init_gettext(*methods, &block) ⇒ Object



170
171
172
173
# File 'lib/gettext/rails.rb', line 170

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



151
152
153
154
# File 'lib/gettext/rails.rb', line 151

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


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/gettext/rails.rb', line 199

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

.textdomainsObject

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

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



237
238
239
# File 'lib/gettext/rails.rb', line 237

def self.textdomains
  @@gettext_domainnames
end

Instance Method Details

#call_methods_around_init_gettext(ary) ⇒ Object

:nodoc:



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

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:



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

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:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gettext/rails.rb', line 120

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:



96
97
98
99
100
# File 'lib/gettext/rails.rb', line 96

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