class BeautifulLocaleGenerator

encoding : utf-8

Public Instance Methods

install_locale() click to toggle source
# File lib/generators/beautiful_locale_generator.rb, line 19
def install_locale
  list_locales.each{ |temp_locale|

    ["beautiful_scaffold.#{temp_locale}.yml"].each do |filename|
      gem_localepath = "app/locales/#{filename}"
      app_localepath = "config/locales/#{filename}"
      begin
        copy_file gem_localepath, app_localepath
      rescue
        say_status("Error", "This beautiful_locale #{temp_locale} doesn't exist !", :red)
      end
    end

    rails_locale_file = "#{temp_locale}.yml"
    download_path = "https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/#{rails_locale_file}"
    begin
      get download_path, "config/locales/#{rails_locale_file}"
    rescue
      say_status("Error", "Error to download locale, verify if locale exist at : #{download_path}", :red)
    end

    willpaginate_locale_file = "will_paginate.#{temp_locale}.yml"
    download_path = "https://raw.githubusercontent.com/tigrish/will-paginate-i18n/master/config/locales/#{temp_locale}.yml"
    begin
      get download_path, "config/locales/#{willpaginate_locale_file}"
      say_status("Warning", "You must modify Will_paginate locale at : Rails.root/config/locale/#{willpaginate_locale_file}", :red)
    rescue
      say_status("Error", "Error to download locale, verify if locale exist at : #{download_path}", :red)
    end
  }

  say_status("Warning", "/!\\ Remember to update your application.rb file !", :yellow)
end
list_locales() click to toggle source
# File lib/generators/beautiful_locale_generator.rb, line 12
def list_locales
  availablelocale = ["fr", "en", "ja"]

  localestr = name.downcase
  (localestr == 'all' ? availablelocale : [localestr])
end
regenerate_app_locale() click to toggle source
# File lib/generators/beautiful_locale_generator.rb, line 53
def regenerate_app_locale
  require 'net/http'

  app_path = (Rails.root || engine_opt)
  app_name = Rails.application.class.name.split('::').first.downcase
  prefix = engine_opt.blank? ? '' : "#{engine_opt.camelize}::"

  already_processed = {}
  hi18n = {}

  list_locales.each do |locale_str|
    locale = locale_str.downcase

    already_processed[locale] ||= {}

    filepath = File.join(app_path, 'config', 'locales', "#{app_name}.#{locale}.yml")
    begin
      if File.exist?(filepath)
        hi18n = YAML.load_file(filepath)
      end
    rescue
      puts "Error loading locale file (YAML invalid?) : #{filepath}"
    end

    hi18n[locale]                  ||= { 'app' => {} }
    hi18n[locale]['app']           ||= { 'models' => {} }
    hi18n[locale]['app']['models'] ||= {}

    # Feed data already translated
    hi18n[locale]['app']['models'].each do |modelname, hshtranslations|
      hshtranslations['bs_attributes'].each do |attr, translated_attr|
        already_processed[locale][attr] = translated_attr
      end
    end

    Dir.glob("app/models/**/*").each do |model_file|
      puts model_file

      next if File.directory?(model_file) or
        File.basename(model_file).first == '.' or
        model_file.include?('/concerns/') or
        model_file.include?('pdf_report.rb') or
        model_file.include?('application_record.rb')

      model = File.basename(model_file, File.extname(model_file))

      klass = "#{prefix}#{model}".camelize.constantize
      sorted_attr = klass.attribute_names.sort

      newmodel = !hi18n[locale]['app']['models'].has_key?(model)

      hi18n[locale]['app']['models'][model] ||= {
        'bs_caption'            => model,
        'bs_caption_plural'     => model.pluralize,
        'bs_attributes'         => {},
      }

      if newmodel then
        bs_caption = ""
        begin
          bs_caption = translate_string(locale, model)
        rescue Exception => e
          puts "Erreur traduction #{e.backtrace}"
          bs_caption = model
        end
        bs_caption_plural = ""
        begin
          bs_caption_plural = translate_string(locale, model.pluralize)
        rescue Exception => e
          puts "Erreur traduction #{e.backtrace}"
          bs_caption_plural = model.pluralize
        end

        hi18n[locale]['app']['models'][model]['bs_caption'] = bs_caption
        hi18n[locale]['app']['models'][model]['bs_caption_plural'] = bs_caption_plural
      end

      hi18n[locale]['app']['models'][model]['bs_attributes'] ||= {}

      sorted_attr.each do |k|
        # Si pas déjà renseigné
        if hi18n[locale]['app']['models'][model]['bs_attributes'][k].blank?
          # Si pas déjà traduit
          if already_processed[locale][k].blank?
            begin
              attr_translate = translate_string(locale, k)
              already_processed[locale][k] = attr_translate
            rescue
              puts "Plantage translate API"
              attr_translate = k
            end
          else
            attr_translate = already_processed[locale][k]
          end
        else
          # Récupère l'attribut traduit
          attr_translate = hi18n[locale]['app']['models'][model]['bs_attributes'][k]
        end

        hi18n[locale]['app']['models'][model]['bs_attributes'][k] = attr_translate
      end
    end

    File.unlink(filepath) if File.exist?(filepath)

    file = File.open(filepath, "w")
    file.write(hi18n[locale].to_yaml)
    file.close
  end
end

Private Instance Methods

translate_string(locale, str) click to toggle source
# File lib/generators/beautiful_locale_generator.rb, line 166
def translate_string(locale, str)
  # See http://www.microsofttranslator.com/dev/
  #
  if locale == "en"
    attr_translate = "#{str.gsub(/_/, " ")}"
  else
    url_domain = "api.mymemory.translated.net"
    url_query = "/get?q=#{str.gsub(/_/, "%20")}&langpair=en%7C#{locale}"

    json = JSON.parse(Net::HTTP.get(url_domain, url_query))
    attr_translate = json["responseData"]["translatedText"].strip.downcase
  end
  raise 'Free Limit' if attr_translate =~ /mymemory/

  return attr_translate
end