Module: GetText::ActiveRecordParser

Extended by:
GetText
Includes:
GetText
Defined in:
lib/gettext/parser/active_record.rb

Constant Summary collapse

@@db_loaded =
nil

Constants included from GetText

VERSION

Class Method Summary collapse

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

.add_target(targets, file, msgid) ⇒ Object

:nodoc:



117
118
119
120
121
122
123
124
125
126
# File 'lib/gettext/parser/active_record.rb', line 117

def add_target(targets, file, msgid) # :nodoc:
  file_lineno = "#{file}:-"
  key_existed = targets.assoc(msgid)
  if key_existed and ! targets[targets.index(key_existed)].include?(file_lineno)
	targets[targets.index(key_existed)] = key_existed << file_lineno
  else
	targets << [msgid, "#{file}:-"]
  end
  targets
end

.init(config) ⇒ Object

Sets some preferences to parse ActiveRecord files.

  • config: a Hash of the config. It can takes some values below:

    • :use_classname - If true, the msgids of ActiveRecord become “ClassName|FieldName” (e.g. “Article|Title”). Otherwise the ClassName is not used (e.g. “Title”). Default is true.

    • :db_yml - the path of database.yml. Default is “config/database.yml”.

    • :db_mode - the mode of the database. Default is “development”

    • :activerecord_classes - an Array of the superclass of the models. The classes should be String value. Default is [“ActiveRecord::Base”]

    • :untranslate_columns - an Array of the column names which is ignored as the msgid.

    • :adapter - the options for ActiveRecord::Base.establish_connection. If this value is set, :db_yml option is ignored.

    • :host - ditto

    • :username - ditto

    • :password - ditto

    • :database - ditto

    • :socket - ditto

    • :encoding - ditto

“ClassName|FieldName” uses GetText.sgettext. So you don’t need to translate the left-side of “|”. See <Documents for Translators for more details(www.yotabanana.com/hiki/ruby-gettext-translate.html)>.



61
62
63
64
65
66
67
68
# File 'lib/gettext/parser/active_record.rb', line 61

def init(config)
  if config
	config.each{|k, v|
	  @config[k] = v
	}
  end
  @ar_re = /class.*(#{@config[:activerecord_classes].join("|")})/
end

.parse(file, targets = []) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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/parser/active_record.rb', line 74

def parse(file, targets = []) # :nodoc:
  GetText.locale = "en"
  old_constants = Object.constants
  begin
    eval(open(file).read, TOPLEVEL_BINDING)
  rescue
    $stderr.puts _("Ignored '%{file}'. Solve dependencies first.") % {:file => file}
    $stderr.puts $! 
  end
  loaded_constants = Object.constants - old_constants
  loaded_constants.each do |classname|
	klass = eval(classname, TOPLEVEL_BINDING)
	if klass.is_a?(Class) && klass < ActiveRecord::Base
	  unless klass.untranslate_all?
 add_target(targets, file, ::Inflector.singularize(klass.table_name.gsub(/_/, " ")))
 unless klass.class_name == classname
   add_target(targets, file, ::Inflector.singularize(classname.gsub(/_/, " ").downcase))
 end
 begin
   klass.columns.each do |column|
		unless untranslate_column?(klass, column.name)
if @config[:use_classname]
  msgid = classname + "|" +  klass.human_attribute_name(column.name)
else
  msgid = klass.human_attribute_name(column.name)
end
add_target(targets, file, msgid)
		end
   end
 rescue
   $stderr.puts _("No database is available.")
   $stderr.puts $!
 end
	  end
	end
  end
  if RubyParser.target?(file)
	targets = RubyParser.parse(file, targets)
  end
  targets.uniq!
  targets
end

.require_rails(file) ⇒ Object

:nodoc:



35
36
37
38
39
40
41
# File 'lib/gettext/parser/active_record.rb', line 35

def require_rails(file) # :nodoc:
  begin
	require file
  rescue MissingSourceFile
	$stderr.puts _("'%{file}' is not found.") % {:file => file}
  end
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/gettext/parser/active_record.rb', line 129

def target?(file) # :nodoc:
  init(nil) unless @ar_re
  data = IO.readlines(file)
  data.each do |v|
	if @ar_re =~ v
	  unless @@db_loaded
 begin
   require 'rubygems'
 rescue LoadError
   $stderr.puts _("rubygems are not found.") if $DEBUG
 end
 begin
   ENV["RAILS_ENV"] = @config[:db_mode]
   require 'config/boot.rb'
   require 'config/environment.rb'
#	      require 'app/controllers/application.rb'
 rescue LoadError
   require_rails 'rubygems'
          if Kernel.respond_to? :gem
            gem 'activerecord'
          else
            require_gem 'activerecord'
          end
   require_rails 'active_record'
   require_rails 'gettext/active_record'
 end
 if @config[:adapter]
   ActiveRecord::Base.establish_connection(@config)
 else
   begin
		yml = YAML.load(ERB.new(IO.read(@config[:db_yml])).result)
   rescue
		return false
   end
 end
	  end
	  @@db_loaded = true
	  return true
	end
  end
  false
end

.untranslate_column?(klass, columnname) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/gettext/parser/active_record.rb', line 70

def untranslate_column?(klass, columnname)
  klass.untranslate?(columnname) || @config[:untranslate_columns].include?(columnname)
end