Class: I18S
- Inherits:
-
Object
- Object
- I18S
- Defined in:
- lib/i18n_sync.rb
Instance Method Summary collapse
- #create(file) ⇒ Object
- #create_new_files ⇒ Object
-
#initialize(argv, opts = {}, argf = []) ⇒ I18S
constructor
A new instance of I18S.
- #out(txt) ⇒ Object
-
#read_file(filename, basename) ⇒ Object
Retrieve comments, translation data in hash form.
- #sync ⇒ Object
- #work ⇒ Object
-
#write_file(filename, basename, comments, data) ⇒ Object
Writes to file from translation data hash structure.
Constructor Details
#initialize(argv, opts = {}, argf = []) ⇒ I18S
Returns a new instance of I18S.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/i18n_sync.rb', line 8 def initialize(argv, opts = {}, argf=[]) # argf.each { |file| p file } @fullpath, *@new_ones = argv @file, *path = @fullpath.split("/").reverse # hm.. hack,,, in 1.9 @path = path.reverse.join("/") || "." # can splat the first _, @lang, @namespace = @file.split(".").reverse @debug = opts[:trace] @order = opts[:order] @comments, @words = read_file(@fullpath, @lang) end |
Instance Method Details
#create(file) ⇒ Object
52 53 54 55 56 |
# File 'lib/i18n_sync.rb', line 52 def create(file) fullpath = file =~ /\// ? file : "#{@path}/#{file}.yml" return puts("File exists.") if File.exists?(fullpath) write_file(fullpath, file, @comments, @words) end |
#create_new_files ⇒ Object
24 25 26 27 28 29 |
# File 'lib/i18n_sync.rb', line 24 def create_new_files @new_ones.each do |file| puts "Creating new file #{file}" create(file) end end |
#out(txt) ⇒ Object
101 102 103 |
# File 'lib/i18n_sync.rb', line 101 def out(txt) puts txt if @debug end |
#read_file(filename, basename) ⇒ Object
Retrieve comments, translation data in hash form
59 60 61 62 |
# File 'lib/i18n_sync.rb', line 59 def read_file(filename, basename) comments = File.read(filename).each_line.select { |l| l =~ /^\w*#/}.join("\n") [comments, YAML.load(File.open(filename, "r:utf-8"))[basename]] end |
#sync ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/i18n_sync.rb', line 31 def sync Dir["#{@path}/*.{yml,rb}"].each do |filename| #next if filename.match('_rails') next if filename =~ /#{@file}/ lang, namespace = File.basename(filename, '.yml').split(".").reverse #filename.split("/").last.split(".").reverse if namespace next unless @namespace && @namespace == namespace else next if @namespace end puts "Writing #{filename}" (_comments, other) = read_file(filename, lang) # Initializing hash variable as empty if it does not exist @words.each { |k,v| other[k] ||= @words[k] } # Remove if not defined in master other.delete_if { |k,v| !@words[k] } write_file(filename, lang, @comments, other) end end |
#work ⇒ Object
19 20 21 22 |
# File 'lib/i18n_sync.rb', line 19 def work puts "Start work on #{@file} (#{@lang})" @new_ones.empty? ? sync : create_new_files end |
#write_file(filename, basename, comments, data) ⇒ Object
Writes to file from translation data hash structure
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/i18n_sync.rb', line 87 def write_file(filename, basename, comments, data) File.delete filename if File.exists? filename File.open(filename, "w:utf-8") do |y| y.puts(comments) if comments #y.puts(basename + ":\n") y.puts({ basename => data }.ya2yaml )# (:sick_compatible => true)) # words.sort.each do |k,v| # keys = k.split(':') # (keys.size-1).times { keys[keys.size-1] = ' ' + keys[keys.size-1] } #Add indentation for children keys # y.puts(keys[keys.size-1]+':'+v+"\n") # end end end |