fesplugas (owner)

Revisions

  • ac02e9 fesplugas Thu Oct 15 06:23:40 -0700 2009
  • b1b58a fesplugas Sat Oct 04 10:20:16 -0700 2008
  • b9d17b fesplugas Sat Oct 04 10:17:29 -0700 2008
  • dc0d3a fesplugas Sat Oct 04 08:40:15 -0700 2008
  • 1d7bbd fesplugas Sat Oct 04 08:39:13 -0700 2008
gist: 14768 Download_button fork
public
Description:
Experiment to extract translation strings from files and merge with the current translations.
Public Clone URL: git://gist.github.com/14768.git
Embed All Files: show embed
application.html.erb #
1
<%= t("Today is {{value}}.", :value => Date.today) %>
application.rb #
1
2
3
4
5
6
7
8
9
class ApplicationController < ActionController::Base
 
  before_filter :set_locale
 
  def set_locale
    I18n.locale = params[:locale] || 'es-ES'
  end
 
end
i18n.rake #
1
2
3
4
5
6
7
8
namespace :i18n do
 
  desc "Update translations"
  task :update => :environment do
    I18n.update_translations
  end
 
end
i18n.rb #
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
I18n.load_path += Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ]
 
module I18n
 
  def self.extract(folders = Dir["{app,lib}/**/*.{rb,rhtml,erb,rjs}"])
    content = []
    folders.map do |f|
      content << File.read(f).scan(/.*[^\w]t\s*[\"\'](.*?)[\"\']/)
      content << File.read(f).scan(/.*[^\w]translate\s*[\"\'](.*?)[\"\']/)
    end
    return content.uniq.flatten
  end
 
  def self.update_translations(locale_folder = "#{RAILS_ROOT}/lib/locale" )
    content = {}
    extract.each { |c| content[c] = nil }
    Dir[ File.join(locale_folder, '*.yml') ].each do |file|
      locale = File.basename(file, '.yml')
      hsh = YAML.load_file(file) || {}
      hsh = { locale => {} } if hsh[locale].nil?
      hsh_to_merge = { locale => content }
      hsh_to_merge[locale].merge!(hsh[locale])
      File.open(file, 'w+') { |f| f.write(hsh_to_merge.to_yaml) }
    end
  end
 
end
 
class Object
 
  def _(*args)
    translate(*args)
  end
 
end
welcome_controller.rb #
1
2
3
4
5
6
7
class WelcomeController < ApplicationController
 
  def index
    @text = translate "Today is {{value}}.", :value => Date.today
  end
 
end