Ruby Forum Rails I18n > Globalize: Collection Messages

Posted by Thomas Baustert (Guest)
on 15.08.2006 14:19
(Received via mailing list)
Hi,

as far as I understand is Globalize adding every not translated
message to the database
during *runtime* of the application? (if method translate() is called)

That means one need run the application and visit every page in any
constellation
to get every message to the database to then afterwards translating
them. And
this for each language to be supported except the default one?

Is there any tool or way right now to collect every message without
running the
application? Like with Gettext?

Or do I misunderstand the function of Globalize in this case?

Thanx in advance.
Thomas


---
Thomas Baustert - Freiberuflicher Softwarecoach
fon: +49(40)411 622 35 mobil: +49(173)23 911 43
fax: +49(40)411 622 36
thomas.baustert(at)b-simple.de
Posted by Eduard (Guest)
on 23.08.2006 17:02
THere is no such function.
Anyway I have writen one to sarch in all views and controllers and also 
translate the errr messages.
Look here:
 #this one will parse all views and contrller from disk to search 
strings that need to be translated. It also will add to the DB the error 
message as needing translation
  def get_translatable_strings
    @strs = []
    Dir.glob("#{RAILS_ROOT}/app/views/**/*.rhtml").collect do |f|
      @strs << File.read(f).scan(/[\"\']([A-Za-z0-9% ]*)[\"\']\.t/)
    end.uniq.flatten.uniq
    @new_strs=Array.new
    @strs=@strs.uniq.sort
    0.upto @strs.size-1 do |i|
      @strs[i].each do |str|
        @new_strs << str
      end
    end
    @strs = []
    Dir.glob("#{RAILS_ROOT}/app/controllers/**/*.rb").collect do |f|
      @strs << File.read(f).scan(/[\"\']([A-Za-z0-9% ]*)[\"\']\.t/)
    end.uniq.flatten.uniq
    @strs=@strs.uniq.sort
    0.upto @strs.size-1 do |i|
      @strs[i].each do |str|
        @new_strs << str
      end
    end
    @new_strs=@new_strs.uniq.sort
    Locale::LOCALES.values.each do |loc|
      Locale.set "#{loc}"
      @new_strs.each do |str|
        str.to_s.translate
      end
      ActiveRecord::Errors.default_error_messages.each_value do 
|error_msg|
        error_msg.translate
      end
    end
  end
Posted by Stim (Guest)
on 04.07.2007 17:32
Thank you for your contribution Eduard.
I am saving myself pretty some work using it.

Still, I have (tried to have) improve your code a little, mainly the
regexp:
  def harvest
    @strs = []
    allowed = ' \w0-9%:;@&#<>\/\\\?\!\+\)\(-=\*'
    regexp =
Regexp.new('[\"](['+allowed+'\']*)[\"]\.t|[\'](['+allowed+'\"]*)[\']\.t')
    Dir.glob("#{RAILS_ROOT}/app/views/**/*.rhtml").collect do |f|
      @strs << File.read(f).scan(regexp)
    end
    @new_strs=Array.new
    @strs=@strs.flatten.uniq
    0.upto @strs.size-1 do |i|
      if @strs[i] then
        @strs[i].each do |str|
          @new_strs << str
        end
      end
    end
    @strs = []
    Dir.glob("#{RAILS_ROOT}/app/controllers/**/*.rb").collect do |f|
      @strs << File.read(f).scan(regexp)
    end
    @strs=@strs.flatten.uniq
    0.upto @strs.size-1 do |i|
      if @strs[i] then
        @strs[i].each do |str|
          @new_strs << str
        end
      end
    end
    @new_strs=@new_strs.uniq.sort
    LOCALES.each do |key,loc|
      Locale.set loc
      @new_strs.each do |str|
        str.to_s.translate
      end
      ActiveRecord::Errors.default_error_messages.each_value do
|error_msg|
        error_msg.translate
      end
    end
  end
Posted by Athu Athu (athira)
on 22.11.2007 12:34
I'm getting the following error , I have added the above method in
method in application helper file and called from one of the view but
got the follwoing error

uninitialized constant ApplicationHelper::LOCALES


Could u give me more information about it (i.e how to use it)?