i18n-verify 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,13 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n-verify (0.0.4)
5
+
6
+ GEM
7
+ specs:
8
+
9
+ PLATFORMS
10
+ x86-mingw32
11
+
12
+ DEPENDENCIES
13
+ i18n-verify!
File without changes
@@ -6,13 +6,15 @@ This is a set of tools that make your life easier if you have lots of translatio
6
6
  Installation
7
7
  ============
8
8
 
9
- You normally would want to install this gem as stand-alons (i.e. not throu environment.rb / Gemfile) because it is not to be called from the application but as a command line utility.
9
+ To use the utils in Rails 2.x, add to your `environment.rb`:
10
10
 
11
- Install it by:
11
+ config.gem "i18n-verify"
12
12
 
13
- gem install i18n-verify
14
-
15
- If you do want to include it in your environment.rb or Gemfile just insert the standard line (perhaps in the :development group in Gemfile; and don't forget to run `bundle install`.
13
+ If using Rails 3.x, add to your Gemfile (perhaps in the :development group)
14
+
15
+ gem "i18n-verify"
16
+
17
+ and run 'bundle install'.
16
18
 
17
19
  Usage
18
20
  =====
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ begin
2
+ require "bundler"
3
+ Bundler.setup
4
+ rescue LoadError
5
+ $stderr.puts "You need to have Bundler installed to be able build this gem."
6
+ end
7
+
8
+ gemspec = eval(File.read(Dir["*.gemspec"].first))
9
+
10
+
11
+ desc "Validate the gemspec"
12
+ task :gemspec do
13
+ gemspec.validate
14
+ end
15
+
16
+ desc "Build gem locally"
17
+ task :build => :gemspec do
18
+ system "gem build #{gemspec.name}.gemspec"
19
+ FileUtils.mkdir_p "pkg"
20
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
21
+ end
22
+
23
+ desc "Install gem locally"
24
+ task :install => :build do
25
+ system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
26
+ end
27
+
28
+ desc "Clean automatically generated files"
29
+ task :clean do
30
+ FileUtils.rm_rf "pkg"
31
+ end
@@ -0,0 +1,31 @@
1
+ # coding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "i18n-verify"
5
+ s.homepage = "http://github.com/fastcatch/i18n-verify"
6
+ s.version = "0.0.4"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = %Q{Tools to verify your Ruby on Rails localizations}
9
+ s.description = %Q{It helps you find keys, undefined translations, duplicate keys, and more}
10
+ s.email = "axz10@cwru.edu"
11
+ s.authors = ["fastcatch"]
12
+ # s.rubyforge_project = s.name
13
+
14
+ # s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ # If you have runtime dependencies, add them here
17
+ # s.add_runtime_dependency "other", "~> 1.2"
18
+
19
+ # If you have development dependencies, add them here
20
+ # s.add_development_dependency "another", "= 0.9"
21
+
22
+ # The list of files to be contained in the gem
23
+ s.files = `git ls-files`.split("\n")
24
+ # s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
25
+ # s.extensions = `git ls-files ext/extconf.rb`.split("\n")
26
+
27
+ s.require_path = 'lib'
28
+
29
+ # For C extensions
30
+ # s.extensions = "ext/extconf.rb"
31
+ end
@@ -0,0 +1,7 @@
1
+ module I18nVerify
2
+ class I18nVerifyRailtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "tasks/verify.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,109 @@
1
+ module I18nVerify
2
+ class Checker
3
+ def initialize(filenames)
4
+ @translations = load_files(filenames)
5
+ end
6
+
7
+ def find_key(regexp=Regexp.new(''), group_by_filename=false)
8
+ # select translations with matching keys
9
+ matching_translations = @translations.select {|t| [t[:locale],t[:key]].join('.') =~ regexp}
10
+
11
+ # print matching translations
12
+ if group_by_filename
13
+ matching_translations.group_by {|t| t[:filename]}.each_pair do |filename, translations|
14
+ puts "#{filename}:"
15
+ translations.each do |t|
16
+ puts " #{[t[:locale],t[:key]].join('.')}: #{t[:translation]}\n"
17
+ end
18
+ end
19
+ else
20
+ matching_translations.each do |t|
21
+ puts "#{t[:filename]} # #{[t[:locale],t[:key]].join('.')}: #{t[:translation]}\n"
22
+ end
23
+ end
24
+ end
25
+
26
+ def is_complete?(locales_requested = [])
27
+ # check each pair of locales
28
+ # if the first one has keys the second one doesn't => these are the incomplete translations
29
+ locales = @translations.collect{|tr| tr[:locale]}.uniq
30
+ locales_to_check = locales_requested.empty? ? locales : (locales & locales_requested)
31
+
32
+ if locales_to_check.size <= 1
33
+ puts "Need at least two locales; found #{locales_to_check.size}: #{locales_to_check.join(',')}"
34
+ else
35
+ puts "Checking locales #{locales_to_check.inspect} out of #{locales.inspect} for completeness"
36
+ locales_to_check.permutation.each do |first, second|
37
+ first_translations = @translations.select {|translation| translation[:locale] == first}
38
+ second_translations = @translations.select {|translation| translation[:locale] == second}
39
+
40
+ differences = first_translations.select {|f| f if second_translations.none? {|s| f[:key]==s[:key]} }.compact
41
+ if differences.empty?
42
+ puts "#{first} => #{second}: complete\n"
43
+ else
44
+ puts "Missing from #{second} vs. #{first}:\n"
45
+ differences.each do |difference|
46
+ puts " " + [difference[:locale], difference[:key]].join('.') + " defined for #{first} in #{difference[:filename]}\n"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ def duplicates(locales_requested = [])
54
+ locales = @translations.collect{|tr| tr[:locale]}.uniq
55
+ locales_to_check = locales_requested.empty? ? locales : (locales & locales_requested)
56
+
57
+ puts "Checking locales #{locales_to_check.inspect} out of #{locales.inspect} for redundancy"
58
+
59
+ # collect and print duplicate translations
60
+ locales_to_check.each do |locale|
61
+ puts "#{locale}:"
62
+ translations_by_key = @translations.select {|t| t[:locale] == locale}.uniq.group_by {|t| t[:key]}
63
+ translations_by_key.reject {|t| t[1].count == 1}.each_pair do |key, translations|
64
+ puts " #{key}: #{translations.collect{|t| t[:filename]}.join(", ")}"
65
+ end
66
+ end
67
+ end
68
+
69
+ protected
70
+ # convert translations hash to flat keys
71
+ # i.e. from { :de => {:new => neue, :old => alt} } to [ ['de.new', 'neue'], ['de.old', 'alte'] ]
72
+ # and yields a flat key and the value to the block
73
+ def flatten_keys(hash, prev_key=nil, &block)
74
+ hash.each_pair do |key, value|
75
+ curr_key = [prev_key, key].compact.join('.')
76
+ if value.is_a?(Hash)
77
+ flatten_keys(value, curr_key, &block)
78
+ else
79
+ yield curr_key, value
80
+ end
81
+ end
82
+ end
83
+
84
+ # read files and store translations (similar to i18n's internals but include filenames)
85
+ def load_files(filenames)
86
+ translations = []
87
+ filenames.each do |filename|
88
+ # puts "Loading: #{filename}"
89
+ type = File.extname(filename).tr('.', '').downcase.to_sym
90
+ case type
91
+ when :rb
92
+ data = eval IO.read(filename) # , binding, filename)
93
+ when :yml
94
+ data = YAML.load_file(filename)
95
+ else
96
+ raise I18n::UnknownFileType.new(type, filename)
97
+ end
98
+ raise I18n::InvalidLocaleData.new(filename) unless data.is_a?(Hash)
99
+ data.each_pair do |locale, d|
100
+ flatten_keys(d || {}) do |flat_key, translation|
101
+ translations.push({ :filename => filename, :locale => locale.to_s, :key => flat_key, :translation => translation })
102
+ end
103
+ end
104
+ end
105
+ translations
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,2 @@
1
+ require 'i18n-verify/railtie' # if defined?(Rails)
2
+ require 'i18n-verify/support'
@@ -16,7 +16,7 @@ namespace :i18n do
16
16
  require "#{::Rails.root.to_s}/config/environment.rb"
17
17
  regexp = Regexp.new(args[:regexp] || "")
18
18
  group_by_filename = args[:group_by_filename]
19
- checker = I18nVerify:Checker.new(I18n.config.load_path)
19
+ checker = I18nVerify::Checker.new(I18n.config.load_path)
20
20
  checker.find_key(regexp,group_by_filename)
21
21
  end
22
22
 
@@ -34,9 +34,8 @@ namespace :i18n do
34
34
  desc "Checks if translations are complete or there are missing ones"
35
35
  task :is_complete do |t, args|
36
36
  require "#{::Rails.root.to_s}/config/environment.rb"
37
- translations = load_files( I18n.config.load_path )
38
- locales_requested = ENV['locales'].downcase.split(',')
39
- checker = I18nVerify:Checker.new(I18n.config.load_path)
37
+ locales_requested = (ENV['locales'] || "").downcase.split(',')
38
+ checker = I18nVerify::Checker.new(I18n.config.load_path)
40
39
  checker.is_complete?(locales_requested)
41
40
  end
42
41
 
@@ -53,10 +52,9 @@ namespace :i18n do
53
52
  desc "Checks if any keys are translated multiple times"
54
53
  task :duplicates do |t, args|
55
54
  require "#{::Rails.root.to_s}/config/environment.rb"
56
- translations = load_files( I18n.config.load_path )
57
- locales_requested = ENV['locales'].downcase.split(',')
58
- checker = I18nVerify:Checker.new(I18n.config.load_path)
59
- checker.duplicates?(locales_requested)
55
+ locales_requested = (ENV['locales'] || "").downcase.split(',')
56
+ checker = I18nVerify::Checker.new(I18n.config.load_path)
57
+ checker.duplicates(locales_requested)
60
58
  end
61
59
 
62
60
  =begin
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-verify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - fastcatch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-11 00:00:00 +02:00
18
+ date: 2011-07-19 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -28,15 +28,21 @@ extensions: []
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Gemfile.lock
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - i18n-verify.gemspec
38
+ - lib/i18n-verify.rb
39
+ - lib/i18n-verify/railtie.rb
40
+ - lib/i18n-verify/support.rb
31
41
  - lib/tasks/verify.rake
32
- - lib/verify/railtie.rb
33
- - lib/verify.rb
34
- - LICENSE.txt
35
- - README.markdown
36
42
  has_rdoc: true
37
43
  homepage: http://github.com/fastcatch/i18n-verify
38
- licenses:
39
- - MIT
44
+ licenses: []
45
+
40
46
  post_install_message:
41
47
  rdoc_options: []
42
48
 
@@ -1,9 +0,0 @@
1
- require 'verify'
2
-
3
- module Verify
4
- class VerifyRailtie < Rails::Railtie
5
- rake_tasks do
6
- load "tasks/verify.rake"
7
- end
8
- end
9
- end
data/lib/verify.rb DELETED
@@ -1,2 +0,0 @@
1
- puts "Loading verify..."
2
- require 'verify/railtie' if defined?(Rails)