make_language.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:2k
源码类别:

Ajax

开发平台:

Others

  1. class LangaugeFile
  2.   
  3.   def executeStage(message) 
  4.     puts message
  5.     yield
  6.   end
  7.   
  8.   def generate_language_file(language,duplicate)
  9.     filename = File.join("lang", "#{language}.rb");
  10.     
  11.     
  12.     stringMap = {}
  13.     executeStage "Loading last language file #{filename}" do
  14.       File.read(filename).scan(/["](.*?)["],(.*)/u).each do |pp|
  15.         stringMap[pp[0]] = pp[1]
  16.       end
  17.     end
  18.     
  19.     stringAll = []
  20.     executeStage "Generating #{filename}" do
  21.       rc  = ""
  22.       rc += "Localization.define("#{language}") do |l|"
  23.       Dir.glob("**/*.{erb,rhtml,rb}").collect do |ff|
  24.         strings   = File.read(ff).scan(/_([ ]*["](.*?)["]/)
  25.         strings  += File.read(ff).scan(/_([ ]*['](.*?)[']/)
  26.         if strings.length > 0
  27.           strings.uniq!
  28.           stringsRemove = strings          
  29.           strings -= stringAll           
  30.           stringAll += stringsRemove
  31.           stringAll.uniq!
  32.           if strings.length > 0
  33.             rc += "nn  # #{ff}"
  34.             strings.each do |ss|
  35.               key = ss[0]
  36.               if ( duplicate )
  37.                 rc += "n  l.store "#{key}", "#{key}""
  38.               else
  39.                 if stringMap.has_key?(key)
  40.                   rc += "n  l.store "#{key}",#{stringMap[key]}"
  41.                 else
  42.                   rc += "n  l.store "#{key}", """
  43.                 end
  44.               end
  45.             end
  46.           end
  47.         end
  48.       end
  49.       rc += "nend"
  50.       ff = File.new(filename,"w")
  51.       ff.write(rc)
  52.       ff.close()
  53.     end
  54.   end
  55. end
  56. class Tool
  57.   def initialize
  58.     @language  = 'en_US'
  59.     @duplicate = true  
  60.     
  61.     if ( ARGV[0] )
  62.       @language  = ARGV[0]
  63.       @duplicate = false
  64.     end    
  65.   end
  66.  
  67.   def run 
  68.     lang = LangaugeFile.new 
  69.     lang.generate_language_file(@language,@duplicate);  
  70.   end
  71. end
  72. # should be run on the Rails directory
  73. tool = Tool.new
  74. tool.run