Module: GetText::ActiveRecordParser

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

Constant Summary

Constants included from GetText

VERSION

Class Method Summary collapse

Methods included from GetText

N_, Nn_, _, add_default_locale_path, bindtextdomain, callersrc, 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

.add_target(targets, file, msgid) ⇒ Object

:nodoc:



97
98
99
100
101
102
103
104
105
# File 'lib/gettext/parser/activerecord.rb', line 97

def add_target(targets, file, msgid) # :nodoc:
  key_existed = targets.assoc(msgid)
  if key_existed 
	targets[targets.index(key_existed)] = key_existed << "#{file}:-"
  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”]

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



52
53
54
55
56
57
58
59
# File 'lib/gettext/parser/activerecord.rb', line 52

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:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gettext/parser/activerecord.rb', line 61

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

.require_rails(file) ⇒ Object

:nodoc:



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

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)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gettext/parser/activerecord.rb', line 107

def target?(file) # :nodoc:
  init(nil) unless @ar_re
  data = IO.readlines(file)
  data.each do |v|
	if @ar_re =~ v
	  unless @db
 require 'rubygems'
 require 'active_record'
 begin
   yml = YAML.load(ERB.new(IO.read(@config[:db_yml])).result)
 rescue
   return false
 end
 ENV["RAILS_ENV"] = @config[:db_mode]
 require_rails 'config/boot.rb'
 require_rails 'config/environment.rb'
 require_rails 'app/controllers/application.rb'
	  end
	  return true
	end
  end
  false
end