Class: BeautifulLocaleGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
BeautifulScaffoldCommonMethods
Defined in:
lib/generators/beautiful_locale_generator.rb

Instance Method Summary collapse

Instance Method Details

#install_localeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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_localesObject



12
13
14
15
16
17
# 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_localeObject



53
54
55
56
57
58
59
60
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
96
97
98
99
100
101
102
103
104
105
106
107
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
152
153
154
155
156
157
158
159
160
161
162
# 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