i18n_sync 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
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
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Marcos Piccinini
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = i18n_sync
2
+
3
+ Syncs all locale yml/rb files based on a "master" one.
4
+
5
+
6
+ == Install
7
+
8
+ gem install i18n_sync
9
+
10
+
11
+ = Use
12
+
13
+ i18s path/to/master/file.yml
14
+
15
+ All other yml should be located on the same folder as the "master".
16
+
17
+
18
+ == Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
26
+ * Send me a pull request. Bonus points for topic branches.
27
+
28
+ == Copyright
29
+
30
+ Copyright (c) 2010 Marcos Piccinini. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "i18n_sync"
8
+ gem.summary = %Q{Syncs all locale yml/rb files based on a "master" one.}
9
+ gem.description = %Q{Gem to sync all locale yml/rb files based on a "master" one.}
10
+ gem.email = "x@nofxx.com"
11
+ gem.homepage = "http://github.com/nofxx/i18n_sync"
12
+ gem.authors = ["Marcos Piccinini"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "i18n_sync #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/bin/i18s ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # I18S
4
+ #
5
+ # Created on 2010-5-17.
6
+ # Copyright (c) 2008. All rights reserved.
7
+ #
8
+ begin
9
+ require 'rubygems'
10
+ rescue LoadError
11
+ # no rubygems to load, so we fail silently
12
+ end
13
+ require 'optparse'
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ require 'i18n_sync'
16
+
17
+ Options = {}
18
+
19
+ parser = OptionParser.new do |opts|
20
+ opts.banner = <<BANNER
21
+ I18N Sync
22
+
23
+ Usage: #{File.basename($0)} <path/to/master.yml> [args]
24
+
25
+ Options are:
26
+
27
+ -O --no-order - Order by name
28
+ -t --trace - Be more verbose
29
+
30
+ Common usage:
31
+
32
+ i18s ./en-US.yml
33
+
34
+ BANNER
35
+ opts.on("-O", "--no-order", "Table order" ) { Options[:order] = false }
36
+ opts.on("-t", "--trace") { Options[:trace] = true }
37
+ opts.parse!(ARGV)
38
+ end
39
+
40
+ if ARGV.empty?
41
+ puts parser.banner
42
+ exit
43
+ else
44
+ I18S.new ARGV, Options # ARGF
45
+ end
data/i18n_sync.gemspec ADDED
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{i18n_sync}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marcos Piccinini"]
12
+ s.date = %q{2010-05-17}
13
+ s.default_executable = %q{i18s}
14
+ s.description = %q{Gem to sync all locale yml/rb files based on a "master" one.}
15
+ s.email = %q{x@nofxx.com}
16
+ s.executables = ["i18s"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/i18s",
29
+ "i18n_sync.gemspec",
30
+ "lib/i18n_sync.rb",
31
+ "spec/fixtures/en.yml",
32
+ "spec/fixtures/pt.yml",
33
+ "spec/i18n_sync_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/nofxx/i18n_sync}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.6}
40
+ s.summary = %q{Syncs all locale yml/rb files based on a "master" one.}
41
+ s.test_files = [
42
+ "spec/spec_helper.rb",
43
+ "spec/i18n_sync_spec.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ end
58
+ end
59
+
data/lib/i18n_sync.rb ADDED
@@ -0,0 +1,93 @@
1
+ #
2
+ # Based on translation rake task on spree (http://spreecommerce.com/)
3
+ #
4
+ class I18S
5
+
6
+ def initialize(argv, opts, argf=[])
7
+ # argf.each { |file| p file }
8
+ @fullpath, *new_ones = argv
9
+ @file, *path = @fullpath.split("/").reverse # hm.. hack,,, in 1.9
10
+ @path = path.reverse.join("/") # can splat the first
11
+ @lang = @file.split(".")[0]
12
+ @debug = opts[:trace]
13
+ @order = opts[:order]
14
+ puts "Start work on #{@file} (#{@lang})"
15
+ @words = get_translation_keys
16
+ if new_ones.empty?
17
+ sync
18
+ else
19
+ new_ones.each do |file|
20
+ puts "Creating new file #{file}"
21
+ create(file)
22
+ end
23
+ end
24
+ end
25
+
26
+ def sync
27
+ Dir["#{@path}/*.{yml,rb}"].each do |filename|
28
+ #next if filename.match('_rails')
29
+ basename = File.basename(filename, '.yml')
30
+ puts "Writing #{filename} -> #{basename}"
31
+ (comments, other) = read_file(filename, basename)
32
+ @words.each { |k,v| other[k] ||= @words[k] } #Initializing hash variable as empty if it does not exist
33
+ other.delete_if { |k,v| !@words[k] } #Remove if not defined in en-US.yml
34
+ write_file(filename, basename, comments, other)
35
+ end
36
+ end
37
+
38
+ def create(file)
39
+ fullpath = "#{@path}/#{file}.yml"
40
+ return puts("File exists.") if File.exists?(fullpath)
41
+ write_file(fullpath, file, "", @words)
42
+ end
43
+
44
+ #Retrieve US word set
45
+ def get_translation_keys
46
+ (dummy_comments, words) = read_file(@fullpath, @lang)
47
+ words
48
+ end
49
+
50
+ #Retrieve comments, translation data in hash form
51
+ def read_file(filename, basename)
52
+ (comments, data) = IO.read(filename).split(/\n*#{basename}:\s*\n/) #Add error checking for failed file read?
53
+ return comments, create_hash(data, basename)
54
+ end
55
+
56
+ #Creates hash of translation data
57
+ def create_hash(data, basename)
58
+ words = Hash.new
59
+ return words if !data
60
+ parent = Array.new
61
+ previous_key = 'base'
62
+ data.split("\n").each do |w|
63
+ next if w.strip.empty?
64
+ (key, value) = w.split(':')
65
+ value ||= ''
66
+ shift = (key =~ /\w/)/2 - parent.size #Determine level of current key in comparison to parent array
67
+ key = key.sub(/^\s+/,'')
68
+ parent << previous_key if shift > 0 #If key is child of previous key, add previous key as parent
69
+ (shift*-1).times { parent.pop } if shift < 0 #If key is not related to previous key, remove parent keys
70
+ previous_key = key #Track key in case next key is child of this key
71
+ words[parent.join(':')+':'+key] = value
72
+ end
73
+ words
74
+ end
75
+
76
+ #Writes to file from translation data hash structure
77
+ def write_file(filename,basename,comments,words)
78
+ File.open(filename, "w") do |log|
79
+ log.puts(comments+"\n"+basename+": \n")
80
+ words.sort.each do |k,v|
81
+ keys = k.split(':')
82
+ (keys.size-1).times { keys[keys.size-1] = ' ' + keys[keys.size-1] } #Add indentation for children keys
83
+ log.puts(keys[keys.size-1]+':'+v+"\n")
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ def out(txt)
90
+ puts txt if @debug
91
+ end
92
+
93
+ end
@@ -0,0 +1,4 @@
1
+
2
+ en:
3
+ sync: To Sync It!
4
+ test: Test
@@ -0,0 +1,4 @@
1
+
2
+ pt:
3
+ sync: To Sync It!
4
+ test: Teste
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "I18nSync" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'i18n_sync'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_sync
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Marcos Piccinini
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-17 00:00:00 -03:00
18
+ default_executable: i18s
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Gem to sync all locale yml/rb files based on a "master" one.
35
+ email: x@nofxx.com
36
+ executables:
37
+ - i18s
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - bin/i18s
51
+ - i18n_sync.gemspec
52
+ - lib/i18n_sync.rb
53
+ - spec/fixtures/en.yml
54
+ - spec/fixtures/pt.yml
55
+ - spec/i18n_sync_spec.rb
56
+ - spec/spec_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/nofxx/i18n_sync
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.6
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Syncs all locale yml/rb files based on a "master" one.
87
+ test_files:
88
+ - spec/spec_helper.rb
89
+ - spec/i18n_sync_spec.rb