Module: GetText::Rails

Overview

GetText for Ruby on Rails

GetText::Rails supports Ruby on Rails. You add only 2 lines in your controller, all of the controller/view/models are targeted the textdomain.

See <Ruby-GetText-Package HOWTO for Ruby on Rails (www.yotabanana.com/hiki/ruby-gettext-howto-rails.html>.

Constant Summary collapse

Rails =

:nodoc:

::Rails

Constants included from GetText

VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

N_, Nn_, _, add_default_locale_path, cgi, cgi=, charset=, create_mofiles, gettext, locale, locale=, msgmerge, msgmerge_all, n_, ngettext, output_charset, output_charset=, rgettext, rmsgfmt, rmsgmerge, s_, set_cgi, set_charset, set_locale, set_output_charset, setlocale, sgettext, textdomain, update_pofiles

Class Method Details

.bindtextdomain(domainname, _cgi = nil, locale = nil, charset = nil, with_model = true) ⇒ Object

Bind a textdomain(#path/#GetText.locale/LC_MESSAGES/#domainname.mo) to your program. Notes the textdomain scope becomes all of the controllers/views/models in your app. This is different from normal GetText.bindtextomain.

Usually, you don’t call this directly in your rails application. Call init_gettext in ActionController::Base instead.

On the other hand, you need to call this in helpers/plugins.

  • domainname: the textdomain name.

  • locale: the locale value such as “ja-JP”. When the value is nil,

  • charset: the charset. Generally UTF-8 is recommanded.

  • with_model: false if you want to ignore model support.

locale is searched the order by this value > “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).



50
51
52
53
54
55
56
57
58
# File 'lib/gettext/rails.rb', line 50

def bindtextdomain(domainname, _cgi = nil, locale = nil, charset = nil, with_model = true)
  Locale.set_current(nil)  # IMPORTANT! current locale should be nil once(default is used).
  set_cgi(_cgi) if _cgi
  @gettext_container_domainname = domainname
  path = File.join(RAILS_ROOT, "locale")
  _bindtextdomain(domainname, path, locale, charset)

  bindtextdomain_to(ActiveRecord::Base, domainname) if with_model
end

.bindtextdomain_to(klass, domainname) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gettext/rails.rb', line 60

def bindtextdomain_to(klass, domainname) #:nodoc:
  klass.class_eval {
	textdomain(domainname)
	def self.human_attribute_name(attribute_key_name) #:nodoc:
	  s_("#{self}|#{attribute_key_name.humanize}")
	end
	def self.human_attribute_table_name_for_error(table_name) #:nodoc:
	  _(table_name.gsub(/_/, " "))
	end
  }
end

.callersrcObject

:nodoc:



72
73
74
75
# File 'lib/gettext/rails.rb', line 72

def callersrc  #:nodoc:
  @gettext_container_domainname = nil unless defined? @gettext_container_domainname
  @gettext_container_domainname
end

.use_localized_templates(setting) ⇒ Object

true to use localized templates such as list.rhtml, list_ja.rhtml, list_ja_JP.rhtml.

  • setting: true if localized, otherwise false. Default is true.

  • Returns: setting



80
81
82
# File 'lib/gettext/rails.rb', line 80

def use_localized_templates(setting)
  @@gettext_use_localized_templates = setting
end

.use_localized_templates?Boolean

true if it use localized templates. See GetText::Rails.use_localized_templates for more details.

  • Returns: true or false.

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/gettext/rails.rb', line 87

def use_localized_templates?
  if defined? @@gettext_use_localized_templates
	@@gettext_use_localized_templates 
  else
	true
  end
end

Instance Method Details

#_bindtextdomainObject

:nodoc:



29
# File 'lib/gettext/rails.rb', line 29

alias :_bindtextdomain :bindtextdomain