Module: GetText::ActiveRecordParser

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

Class Method Summary collapse

Methods included from GetText

update_pofiles, update_pofiles_org

Class Method Details

.add_target(targets, file, msgid) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
# File 'lib/gettext_activerecord/parser.rb', line 153

def add_target(targets, file, msgid) # :nodoc:
  po = PoMessage.new(:normal)
  po.msgid = msgid
  po.sources << "#{file}:-"
  targets << po
  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_classes - an Array of the modules/class names which is ignored as the msgid.

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

    • :untranslate_table_name - a Boolean that avoids table name to be translated if it is true … Generally, we don’t have to translate table_name, do we? Maybe it is not true.…. but it is a test

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



82
83
84
85
86
87
88
89
90
# File 'lib/gettext_activerecord/parser.rb', line 82

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

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

:nodoc:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gettext_activerecord/parser.rb', line 108

def parse(file, targets = []) # :nodoc:
  puts "parse file #{file}" if $DEBUG
  
  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 = ActiveRecord::Base.active_record_classes_list
  ActiveRecord::Base.reset_active_record_classes_list
  loaded_constants.each do |classname|
    klass = eval(classname, TOPLEVEL_BINDING)
    if translatable_class?(klass)
      puts "processing class #{klass.name}" if $DEBUG 
      add_target(targets, file, ActiveSupport::Inflector.singularize(klass.table_name.gsub(/_/, " "))) unless @config[:untranslate_table_name]
      unless klass.class_name == classname
        add_target(targets, file, ActiveSupport::Inflector.singularize(klass.to_s_with_gettext.gsub(/_/, " ").downcase))
      end
      begin
        klass.columns.each do |column|
          if translatable_column?(klass, column.name)
            if @config[:use_classname]
              msgid = klass.to_s_with_gettext + "|" +  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
  if RubyParser.target?(file)
    targets += RubyParser.parse(file)
  end
  targets
end

.require_rails(file) ⇒ Object

:nodoc:



54
55
56
57
58
59
60
# File 'lib/gettext_activerecord/parser.rb', line 54

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)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gettext_activerecord/parser.rb', line 161

def target?(file) # :nodoc:
  init(nil) unless @ar_re
  data = IO.readlines(file)
  data.each do |v|
    if @ar_re =~ v
      unless ActiveRecord::Base.connected?
        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_rails 'activesupport'
          require_rails 'gettext_activerecord'
        rescue LoadError
          require_rails 'rubygems'
          gem 'activerecord'
          require_rails 'activesupport'
          require_rails 'active_record'
          require_rails 'gettext_activerecord'
        end
        begin
          yaml = YAML.load(IO.read(@config[:db_yml]))
          if yaml[@config[:db_mode]]
            ActiveRecord::Base.establish_connection(yaml[@config[:db_mode]])
          else
            ActiveRecord::Base.establish_connection(yaml)
          end
        rescue
          if @config[:adapter]
            ActiveRecord::Base.establish_connection(@config)
          else
            return false
          end
        end
      end
      return true
    end
  end
  false
end

.translatable_class?(klass) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gettext_activerecord/parser.rb', line 92

def translatable_class?(klass)
  if klass.is_a?(Class) && klass < ActiveRecord::Base
    if klass.untranslate_all? || klass.abstract_class? || @config[:untranslate_classes].include?(klass.name)
      false
    else
      true
    end
  else
    true
  end
end

.translatable_column?(klass, columnname) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/gettext_activerecord/parser.rb', line 104

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