lackac / app_lego

A collection of Rails templates I use for projects

app_lego / locale.rb
100644 36 lines (30 sloc) 1.138 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
locales = (ENV['LOCALES'] || ask("Which locales do you want to use (separate with commas if more)?")).split(/[,\s]+/)
 
locales.each do |locale|
  locale += '.yml' unless locale =~ /\.(yml|rb)$/
  file "config/locales/#{locale}",
    open("http://github.com/svenfuchs/rails-i18n/raw/master/rails/locale/#{locale}").read
end
 
def uri_exists?(uri)
  uri = URI.parse(uri)
  Net::HTTP.start(uri.host, uri.port) do |http|
    http.head(uri.request_uri).is_a?(Net::HTTPSuccess)
  end
end
 
if @base_path
  log '', 'Trying to download module specific localizations...'
  @used_legos.each do |lego|
    locales.each do |locale|
      fn = "#{locale.split(".").first}.#{lego}.yml"
      path = "#{@base_path}/locales/#{fn}"
      if @base_path !~ /^https?:\/\// or uri_exists?(path)
        content = open(path).read rescue nil
        file "config/locales/#{fn}", content if content
      end
    end
  end
end
 
gsub_file "config/environment.rb",
/(#\s*)?config.i18n.default_locale.*$/,
  "config.i18n.default_locale = '#{locales.first.gsub(/\.(yml|rb)$/, '')}'"
 
git :add => "."
git :commit => "-a -m 'Added #{locales.join(",")} localizations'"