i18n_sync 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "x@nofxx.com"
11
11
  gem.homepage = "http://github.com/nofxx/i18n_sync"
12
12
  gem.authors = ["Marcos Piccinini"]
13
+ gem.add_dependency "ya2yaml"
13
14
  gem.add_development_dependency "rspec", ">= 1.2.9"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
data/bin/i18s CHANGED
@@ -3,7 +3,7 @@
3
3
  # I18S
4
4
  #
5
5
  # Created on 2010-5-17.
6
- # Copyright (c) 2008. All rights reserved.
6
+ # Copyleft nofxx (c) 2011.
7
7
  #
8
8
  begin
9
9
  require 'rubygems'
@@ -43,5 +43,6 @@ if ARGV.empty?
43
43
  puts parser.banner
44
44
  exit
45
45
  else
46
- I18S.new ARGV, Options # ARGF
46
+ syncr = I18S.new ARGV, Options # ARGF
47
+ syncr.work
47
48
  end
data/lib/i18n_sync.rb CHANGED
@@ -1,96 +1,101 @@
1
1
  #
2
2
  # Based on translation rake task on spree (http://spreecommerce.com/)
3
3
  #
4
+ require 'ya2yaml'
5
+
4
6
  class I18S
5
7
 
6
- def initialize(argv, opts, argf=[])
8
+ def initialize(argv, opts = {}, argf=[])
7
9
  # argf.each { |file| p file }
8
- @fullpath, *new_ones = argv
10
+ @fullpath, *@new_ones = argv
9
11
  @file, *path = @fullpath.split("/").reverse # hm.. hack,,, in 1.9
10
- @path = path.reverse.join("/") # can splat the first
11
- @path ||= "."
12
- s = @file.split(".")
13
- @namespace = s.length > 2 ? s[0] : false
14
- @lang = s[-2]
12
+ @path = path.reverse.join("/") || "." # can splat the first
13
+ _, @lang, @namespace = @file.split(".").reverse
15
14
  @debug = opts[:trace]
16
15
  @order = opts[:order]
16
+ @comments, @words = read_file(@fullpath, @lang)
17
+ end
18
+
19
+ def work
17
20
  puts "Start work on #{@file} (#{@lang})"
18
- @words = get_translation_keys
19
- if new_ones.empty?
20
- sync
21
- else
22
- new_ones.each do |file|
23
- puts "Creating new file #{file}"
24
- create(file)
25
- end
21
+ @new_ones.empty? ? sync : create_new_files
22
+ end
23
+
24
+ def create_new_files
25
+ @new_ones.each do |file|
26
+ puts "Creating new file #{file}"
27
+ create(file)
26
28
  end
27
29
  end
28
30
 
29
31
  def sync
30
32
  Dir["#{@path}/*.{yml,rb}"].each do |filename|
31
33
  #next if filename.match('_rails')
32
- basename = File.basename(filename, '.yml')
33
- if basename =~ /\./
34
- n, basename = basename.split(".")
35
- next unless @namespace && @namespace == n
34
+ next if filename =~ /#{@file}/
35
+ lang, namespace = File.basename(filename, '.yml').split(".").reverse #filename.split("/").last.split(".").reverse
36
+ if namespace
37
+ next unless @namespace && @namespace == namespace
38
+ else
39
+ next if @namespace
36
40
  end
37
- puts "Writing #{filename} -> #{basename}"
38
- (comments, other) = read_file(filename, basename)
39
- @words.each { |k,v| other[k] ||= @words[k] } #Initializing hash variable as empty if it does not exist
40
- other.delete_if { |k,v| !@words[k] } #Remove if not defined in en-US.yml
41
- write_file(filename, basename, comments, other)
41
+
42
+ puts "Writing #{filename}"
43
+ (_comments, other) = read_file(filename, lang)
44
+ # Initializing hash variable as empty if it does not exist
45
+ @words.each { |k,v| other[k] ||= @words[k] }
46
+ # Remove if not defined in master
47
+ other.delete_if { |k,v| !@words[k] }
48
+ write_file(filename, lang, @comments, other)
42
49
  end
43
50
  end
44
51
 
45
52
  def create(file)
46
53
  fullpath = file =~ /\// ? file : "#{@path}/#{file}.yml"
47
54
  return puts("File exists.") if File.exists?(fullpath)
48
- write_file(fullpath, file, "", @words)
49
- end
50
-
51
- #Retrieve US word set
52
- def get_translation_keys
53
- (dummy_comments, words) = read_file(@fullpath, @lang)
54
- words
55
+ write_file(fullpath, file, @comments, @words)
55
56
  end
56
57
 
57
58
  #Retrieve comments, translation data in hash form
58
59
  def read_file(filename, basename)
59
- (comments, data) = IO.read(filename).split(/\n*#{basename}:\s*\n/) #Add error checking for failed file read?
60
- return comments, create_hash(data, basename)
60
+ comments = File.read(filename).each_line.select { |l| l =~ /^\w*#/}.join("\n")
61
+ [comments, YAML.load(File.open(filename, "r:utf-8"))[basename]]
61
62
  end
62
63
 
63
64
  #Creates hash of translation data
64
- def create_hash(data, basename)
65
- words = Hash.new
66
- return words if !data
67
- parent = Array.new
68
- previous_key = 'base'
69
- data.split("\n").each do |w|
70
- next if w.strip.empty?
71
- (key, value) = w.split(':')
72
- value ||= ''
73
- shift = (key =~ /\w/)/2 - parent.size #Determine level of current key in comparison to parent array
74
- key = key.sub(/^\s+/,'')
75
- parent << previous_key if shift > 0 #If key is child of previous key, add previous key as parent
76
- (shift*-1).times { parent.pop } if shift < 0 #If key is not related to previous key, remove parent keys
77
- previous_key = key #Track key in case next key is child of this key
78
- words[parent.join(':')+':'+key] = value
79
- end
80
- words
81
- end
65
+ #def create_hash(data, filename)
66
+ # return {} unless data
67
+ # words = Hash.new
68
+ # return words if !data
69
+ # parent = Array.new
70
+ # previous_key = 'base'
71
+ # data.split("\n").each do |w|
72
+ # next if w.strip.empty?
73
+ # w.sub!(":", "") if w =~ /^(\s*)\:/
74
+ # (key, *value) = w.split(':')
75
+ # value = value.join# ||= ''
76
+ # shift = (key =~ /\w/)/2 - parent.size #Determine level of current key in comparison to parent array
77
+ # key = key.sub(/^\s+/,'')
78
+ # parent << previous_key if shift > 0 #If key is child of previous key, add previous key as parent
79
+ # (shift*-1).times { parent.pop } if shift < 0 #If key is not related to previous key, remove parent keys
80
+ # previous_key = key #Track key in case next key is child of this key
81
+ # words[parent.join(':')+':'+key] = value
82
+ # end
83
+ # words
84
+ #end
82
85
 
83
86
  #Writes to file from translation data hash structure
84
- def write_file(filename,basename,comments,words)
85
- File.open(filename, "w") do |log|
86
- log.puts(comments+"\n"+basename+": \n")
87
- words.sort.each do |k,v|
88
- keys = k.split(':')
89
- (keys.size-1).times { keys[keys.size-1] = ' ' + keys[keys.size-1] } #Add indentation for children keys
90
- log.puts(keys[keys.size-1]+':'+v+"\n")
91
- end
87
+ def write_file(filename, basename, comments, data)
88
+ File.delete filename if File.exists? filename
89
+ File.open(filename, "w:utf-8") do |y|
90
+ y.puts(comments) if comments
91
+ #y.puts(basename + ":\n")
92
+ y.puts({ basename => data }.ya2yaml )# (:sick_compatible => true))
93
+ # words.sort.each do |k,v|
94
+ # keys = k.split(':')
95
+ # (keys.size-1).times { keys[keys.size-1] = ' ' + keys[keys.size-1] } #Add indentation for children keys
96
+ # y.puts(keys[keys.size-1]+':'+v+"\n")
97
+ # end
92
98
  end
93
-
94
99
  end
95
100
 
96
101
  def out(txt)
data/spec/fixtures/en.yml CHANGED
@@ -1,3 +1,4 @@
1
+ # Comment cool
1
2
 
2
3
  en:
3
4
  sync: To Sync It!
@@ -0,0 +1,4 @@
1
+ en:
2
+ yup: Yes
3
+ with_colon: "Value: Rock"
4
+ normal: Normal
@@ -0,0 +1,3 @@
1
+ pt:
2
+ yup: Sim
3
+ nope: Não
@@ -8,36 +8,89 @@ def run(comm)
8
8
  end
9
9
 
10
10
  describe "I18nSync" do
11
- before do
11
+ before(:each) do
12
12
  `rm -rf spec/work`
13
13
  `cp -rf spec/fixtures spec/work`
14
14
  end
15
15
 
16
- it "should create a new one" do
17
- newfile = "spec/work/fo.yml"
18
- `rm #{newfile}` if File.exists?(newfile)
19
- run("#{EN} fo")
20
- File.read(newfile).should eql("\nfo: \n sync: To Sync It!\n test: Test\n")
21
- end
16
+ describe "Unit" do
22
17
 
23
- it "should sync files nicely" do
24
- run("spec/work/pt.yml")
25
- File.read(EN).should eql("\nen: \n new: \"Uau, isso é novo\"\n sync: To Sync It!\n test: Test\n")
26
- end
18
+ let(:i) { I18S.new("spec/work/en.yml") }
19
+
20
+ it "should parse master file" do
21
+ i.instance_variable_get("@path").should eql("spec/work")
22
+ i.instance_variable_get("@lang").should eql("en")
23
+ i.instance_variable_get("@file").should eql("en.yml")
24
+ end
25
+
26
+ it "should read comments" do
27
+ i.instance_variable_get("@comments").should eql("# Comment cool\n")
28
+ end
29
+
30
+ it "should read the hash" do
31
+ i.instance_variable_get("@words").should eql({"sync"=>"To Sync It!", "test"=>"Test"})
32
+ end
33
+
34
+ it "should work fine with other files" do
35
+ i2 = I18S.new("spec/work/pt.yml")
36
+ i2.instance_variable_get("@lang").should eql("pt")
37
+ i2.instance_variable_get("@comments").should be_empty
38
+ end
27
39
 
28
- it "should respect namespaces" do
29
- run("spec/work/pt.yml")
30
- File.read("spec/work/named.pt.yml").should eql("\npt:\n something: Algo\n")
31
40
  end
32
41
 
33
- it "should respect namespaces" do
34
- run("spec/work/named.en.yml")
35
- File.read("spec/work/named.pt.yml").should eql("\npt: \n another: Another\n something: Algo\n")
42
+ describe "Namespaced" do
43
+ let(:i) { I18S.new("spec/work/named.en.yml") }
44
+
45
+ it "should parse master file" do
46
+ i.instance_variable_get("@path").should eql("spec/work")
47
+ i.instance_variable_get("@lang").should eql("en")
48
+ i.instance_variable_get("@namespace").should eql("named")
49
+ i.instance_variable_get("@file").should eql("named.en.yml")
50
+ end
51
+
52
+ it "should read the hash" do
53
+ i.instance_variable_get("@words").should eql({"something"=>"Something", "another"=>"Another"})
54
+ end
55
+
36
56
  end
37
57
 
38
- it "should order alphabeticaly namespaces" do
39
- run("spec/work/order.pt.yml")
40
- File.read("spec/work/order.en.yml").should eql("\nen: \n alpha: Alpha\n blue: Blue\n zebra: Zebra\n")
41
- File.read("spec/work/named.pt.yml").should eql("\npt:\n something: Algo\n")
58
+ describe "Acceptance" do
59
+
60
+ it "should create a new one" do
61
+ newfile = "spec/work/fo.yml"
62
+ `rm #{newfile}` if File.exists?(newfile)
63
+ run("#{EN} fo")
64
+ File.read(newfile).should eql("# Comment cool\n--- \nfo: \n sync: \"To Sync It!\"\n test: Test\n")
65
+ end
66
+
67
+ it "should sync files nicely" do
68
+ run("spec/work/pt.yml")
69
+ File.read(EN).should eql("\n--- \nen: \n new: \"Uau, isso é novo\"\n sync: \"To Sync It!\"\n test: Test\n")
70
+ end
71
+
72
+ it "should respect namespaces" do
73
+ run("spec/work/pt.yml")
74
+ File.read("spec/work/named.pt.yml").should eql("\npt:\n something: Algo\n")
75
+ end
76
+
77
+ it "should work with namespaces" do
78
+ run("spec/work/named.en.yml")
79
+ File.read("spec/work/named.pt.yml").should eql("\n--- \npt: \n another: Another\n something: Algo\n")
80
+ File.read("spec/work/pt.yml").should eql("\npt:\n sync: To Sync It!\n test: Teste\n new: \"Uau, isso é novo\"\n")
81
+ end
82
+
83
+ it "should order alphabeticaly namespaces" do
84
+ run("spec/work/order.pt.yml")
85
+ File.read("spec/work/order.en.yml").should eql("\n--- \nen: \n alpha: Alpha\n blue: Blue\n zebra: Zebra\n")
86
+ File.read("spec/work/named.pt.yml").should eql("\npt:\n something: Algo\n")
87
+ end
88
+
89
+ it "should work with extra chars" do
90
+ run("spec/work/extra.en.yml")
91
+ File.read("spec/work/extra.pt.yml").should eql("\n--- \npt: \n normal: Normal\n with_colon: \"Value: Rock\"\n yup: Sim\n")
92
+ end
93
+
42
94
  end
95
+
43
96
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 5
9
- version: 0.0.5
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marcos Piccinini
@@ -14,13 +14,26 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-16 00:00:00 -03:00
17
+ date: 2011-04-04 00:00:00 -03:00
18
18
  default_executable: i18s
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: ya2yaml
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
24
37
  none: false
25
38
  requirements:
26
39
  - - ">="
@@ -31,7 +44,7 @@ dependencies:
31
44
  - 9
32
45
  version: 1.2.9
33
46
  type: :development
34
- version_requirements: *id001
47
+ version_requirements: *id002
35
48
  description: Gem to sync all locale yml/rb files based on a "master" one.
36
49
  email: x@nofxx.com
37
50
  executables:
@@ -43,7 +56,6 @@ extra_rdoc_files:
43
56
  - README.rdoc
44
57
  files:
45
58
  - .document
46
- - .gitignore
47
59
  - LICENSE
48
60
  - README.rdoc
49
61
  - Rakefile
@@ -52,6 +64,8 @@ files:
52
64
  - i18n_sync.gemspec
53
65
  - lib/i18n_sync.rb
54
66
  - spec/fixtures/en.yml
67
+ - spec/fixtures/extra.en.yml
68
+ - spec/fixtures/extra.pt.yml
55
69
  - spec/fixtures/named.en.yml
56
70
  - spec/fixtures/named.pt.yml
57
71
  - spec/fixtures/order.en.yml
@@ -64,8 +78,8 @@ homepage: http://github.com/nofxx/i18n_sync
64
78
  licenses: []
65
79
 
66
80
  post_install_message:
67
- rdoc_options:
68
- - --charset=UTF-8
81
+ rdoc_options: []
82
+
69
83
  require_paths:
70
84
  - lib
71
85
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -92,5 +106,5 @@ signing_key:
92
106
  specification_version: 3
93
107
  summary: Syncs all locale yml/rb files based on a "master" one.
94
108
  test_files:
95
- - spec/spec_helper.rb
96
109
  - spec/i18n_sync_spec.rb
110
+ - spec/spec_helper.rb
data/.gitignore DELETED
@@ -1,22 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
22
- spec/work/*