Globalize tuning

Hi,
I am using Globalize plug-in extensively and I made some “tuning” to it.

  1. First I have added a method to search for strings that should be
    translated (not visiting every page from the site).
  2. Then I added a new method to be able to pass an array of arguments to
    a string like: “%s likes %s and %s gave him %d bottles of %s”.t /John
    /milk /Doe /5 /beer This result into many strings added to the DB
    because .t method is called for each replacement.
    I added .t_p method that works like:
    “%s likes %s and %s gave him %d bottles of %s”.t_p(“John”, “milk”,
    “Doe”, 5, “beer”)
  3. Then a simple class with one method to auto translates strings using
    bublefish powered by Systran.
    All are very simple codes, but I think that it might help on a starting
    project with globalize.

Codes:
2. In core_ext.rb add this:
def translate_p(arg = nil, default= nil)
Locale.translate(self, default, arg)
enD
alias :t_p :translate_p
Then in db_view_translator.rb change the fetch methid with this one:
def fetch(key, language, default = nil, arg = nil) # :nodoc:
# hack for “%d %d”.t / [1,2]
if arg.kind_of?(Array)

    real_default = default || key
    result = fetch_from_cache(key, language, real_default, 1)
    arg.each do |x|
      if x.kind_of?(Numeric)
        result.sub!('%d',x.to_s)
      else
        result.sub!('%s',x.to_s)
      end
    end
    return result
 # end of hack
  else

    # use argument as pluralization number, if number
    num = arg.kind_of?(Numeric) ? arg : nil

    # if there's no translation, use default or original key
    real_default = default || key

    result = fetch_from_cache(key, language, real_default, num)

    if num
      return result.sub('%d', num.to_s)
    else
      return arg.nil? ? result : result.sub('%s', arg.to_s)
    end

  end
end

1.add this to any controller and call it
def get_translateable_strings
Locale.clear_cache
Locale.translator.cache_reset
@strs = []
#getting untraslated strings from views (.rhtml files)
Dir.glob("#{RAILS_ROOT}/app/views//*.rhtml").collect do |f|
ll = File.read(f).scan(/"["].[t|t_p]/)
@strs << ll
ll = File.read(f).scan(/[’].[t|t_p]/)
@strs << ll
end
#getting untraslated strings from helpers
Dir.glob("#{RAILS_ROOT}/app/helpers/
/.rb").collect do |f|
ll = File.read(f).scan(/"["].[t|t_p]/)
@strs << ll
ll = File.read(f).scan(/[’].[t|t_p]/)
@strs << ll
end
#getting untraslated strings from controllers
Dir.glob("#{RAILS_ROOT}/app/controllers/**/
.rb").collect do |f|
ll = File.read(f).scan(/"["].[t|t_p]/)
@strs << ll
ll = File.read(f).scan(/[’].[t|t_p]/)
@strs << ll
end
#getting untraslated strings from environment.rb
Dir.glob("#{RAILS_ROOT}/config/environment.rb").collect do |f|
ll = File.read(f).scan(/"["].[t|t_p]/)
@strs << ll
end
@new_strs=Array.new
@[email protected]
LOCALES.values.each do |loc|
Locale.set “#{loc}”
@new_strs.each do |str|
str.to_s.translate
end
#translate error messages
ActiveRecord::Errors.default_error_messages.each_value do
|error_msg|
error_msg.translate
end
end

require ‘net/http’
require ‘uri’
class Systran

TRANSLATOR_ENGINE = “http://www.babelfish.altavista.com/tr
#Available options for trans_key:
#zh_en=>Chinese-simp to English
#zt_en=>Chinese-trad to English
#en_zh=>English to Chinese-simp
#en_zt=>English to Chinese-trad
#en_nl=>English to Dutch
#en_fr=>English to French
#en_de=>English to German
#en_el=>English to Greek
#en_it=>English to Italian
#en_ja=>English to Japanese
#en_ko=>English to Korean
#en_pt=>English to Portuguese
#en_ru=>English to Russian
#en_es=>English to Spanish
#nl_en=>Dutch to English
#nl_fr=>Dutch to French
#fr_nl=>French to Dutch
#fr_en=>French to English
#fr_de=>French to German
#fr_el=>French to Greek
#fr_it=>French to Italian
#fr_pt=>French to Portuguese
#fr_es=>French to Spanish
#de_en=>German to English
#de_fr=>German to French
#el_en=>Greek to English
#el_fr=>Greek to French
#it_en=>Italian to English
#it_fr=>Italian to French
#ja_en=>Japanese to English
#ko_en=>Korean to English
#pt_en=>Portuguese to English
#pt_fr=>Portuguese to French
#ru_en=>Russian to English
#es_en=>Spanish to English
#es_fr=>Spanish to French
def self.translate_string(string, trans_key)
url = URI.parse(TRANSLATOR_ENGINE)
http = Net::HTTP.post_form(url,
{“doit”=>“done”,
“intl”=>“1”,
“tt”=>“urltext”,
“trtext”=>"#{string}",
“lp”=>"#{trans_key}",
“btnTrTxt”=>“Translate”})
begin
doc = http.body.split(’<input type=’)
rescue
doc = http.body.split(’

’)
doc = doc[1].split(’
’)
end
@translated_text=doc[0].to_s
return @translated_text
end

end

I know you wrote this a long time ago, but I wanted to thank you. The
Babelfish translator is exactly what I’ve been looking for and it
still works like a charm.

Thank you,
Anthony

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs