Class: I18S
- Inherits:
-
Object
- Object
- I18S
- Defined in:
- lib/i18n_sync.rb
Constant Summary collapse
- VERSION =
babel shark
'0.5.1'
Class Method Summary collapse
- .add_key(key, val, file) ⇒ Object
- .recursive_hash ⇒ Object
-
.work_on(argv, opts = {}, argf = []) ⇒ Object
Just here cuz I’m lazy.…TBF really ugly ! ! ! !.
Instance Method Summary collapse
- #create(newlang) ⇒ Object
- #create_new_files ⇒ Object
-
#initialize(argv, opts = {}, keys = {}, 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 = {}, keys = {}, argf = []) ⇒ I18S
Returns a new instance of I18S.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/i18n_sync.rb', line 50 def initialize(argv, opts = {}, keys = {}, 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[:debug] @order = opts[:order] @comments, @words = read_file(@fullpath, @lang) @words.merge! keys unless keys.empty? work end |
Class Method Details
.add_key(key, val, file) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/i18n_sync.rb', line 41 def self.add_key(key, val, file) hsh = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }#recursive_hash keys = key.split(".") keys.reduce(hsh) do |a, k| k == keys[-1] ? a[k] = val : a = a[k] end new(file, {}, hsh) end |
.recursive_hash ⇒ Object
37 38 39 |
# File 'lib/i18n_sync.rb', line 37 def self.recursive_hash Hash.new { |h,k| h[k] = recursive_hash } end |
.work_on(argv, opts = {}, argf = []) ⇒ Object
Just here cuz I’m lazy.…TBF really ugly ! ! ! !
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/i18n_sync.rb', line 26 def self.work_on(argv, opts = {}, argf = []) if File.directory? path = argv[0] Dir["#{path}/**"].map do |file| next unless file =~ /(^|\.)#{opts[:lang]}\./ new([file], opts, argf) end.reject(&:nil?) else [new(argv, opts, argf)] end end |
Instance Method Details
#create(newlang) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/i18n_sync.rb', line 92 def create newlang # New name "app.en.yml" -> "app.pt.yml", "en.yml" -> "pt.yml" newname = @file.gsub(/(^|\.)#{@lang}\./, "\\1#{newlang}.") fullpath = "#{@path}/#{newname}" return puts("File exists.") if File.exists?(fullpath) write_file(fullpath, newlang, @comments, @words) end |
#create_new_files ⇒ Object
68 69 70 71 72 73 |
# File 'lib/i18n_sync.rb', line 68 def create_new_files @new_ones.each do |name| puts "Creating new file #{name}" create name end end |
#out(txt) ⇒ Object
115 116 117 |
# File 'lib/i18n_sync.rb', line 115 def out(txt) puts txt if @debug end |
#read_file(filename, basename) ⇒ Object
Retrieve comments, translation data in hash form
101 102 103 104 |
# File 'lib/i18n_sync.rb', line 101 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/i18n_sync.rb', line 75 def sync Dir["#{@path}/*.{yml,rb}"].each do |filename| lang, namespace = File.basename(filename, '.yml').split(".").reverse if namespace next unless @namespace && @namespace == namespace else next if @namespace end out "Writing #{filename}" (_comments, old) = read_file(filename, lang) # Initializing hash variable as empty if it does not exist other = @words.dup.deep_merge! old write_file(filename, lang, @comments, other) end end |
#work ⇒ Object
63 64 65 66 |
# File 'lib/i18n_sync.rb', line 63 def work out "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
107 108 109 110 111 112 113 |
# File 'lib/i18n_sync.rb', line 107 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 => data }.ya2yaml ) end end |