vocab 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vocab/application.rb +9 -2
- data/lib/vocab/merger/android.rb +6 -1
- data/lib/vocab/merger/base.rb +2 -2
- data/lib/vocab/merger/rails.rb +33 -7
- data/lib/vocab/version.rb +1 -1
- data/spec/data/rails/locales/en.yml +1 -1
- data/spec/data/rails/locales/es.yml +1 -1
- data/spec/extractor/rails_spec.rb +1 -1
- data/spec/merger/base_spec.rb +1 -1
- data/spec/merger/rails_spec.rb +1 -1
- data/spec/translator/rails_spec.rb +3 -3
- metadata +2 -2
data/lib/vocab/application.rb
CHANGED
@@ -20,6 +20,7 @@ module Vocab
|
|
20
20
|
|
21
21
|
parser.banner = 'Usage: vocab [-h] command [platform] [type] [path]'
|
22
22
|
parser.on( '-h', '--help', 'Show this usage message' ) { options.help = true }
|
23
|
+
parser.on( '-s', '--strict', 'Require matching order of interpolations' ) { options.strict = true }
|
23
24
|
parser.separator ""
|
24
25
|
parser.separator " vocab init"
|
25
26
|
parser.separator " vocab extract rails"
|
@@ -28,10 +29,12 @@ module Vocab
|
|
28
29
|
parser.separator " vocab clean rails"
|
29
30
|
parser.separator " vocab convert rails xml2yml <infile>"
|
30
31
|
parser.separator " vocab convert rails yml2xml <infile>"
|
31
|
-
parser.separator " vocab merge rails"
|
32
|
+
parser.separator " vocab merge rails [-s]"
|
32
33
|
parser.separator " vocab merge android"
|
33
34
|
parser.separator " vocab validate android"
|
34
35
|
parser.separator " vocab validate rails"
|
36
|
+
parser.separator " vocab interpolation rails <file> [-s]"
|
37
|
+
parser.separator " vocab interpolation android <file>"
|
35
38
|
parser.separator ""
|
36
39
|
|
37
40
|
commands = parser.parse( ARGV )
|
@@ -63,13 +66,17 @@ module Vocab
|
|
63
66
|
elsif( options.command == 'extract' && options.platform == 'android' )
|
64
67
|
Extractor::Android.extract
|
65
68
|
elsif( options.command == 'merge' && options.platform == 'rails' )
|
66
|
-
Merger::Rails.new.merge
|
69
|
+
Merger::Rails.new.merge( options.strict )
|
67
70
|
elsif( options.command == 'merge' && options.platform == 'android' )
|
68
71
|
Merger::Android.new.merge
|
69
72
|
elsif( options.command == 'validate' && options.platform == 'android' )
|
70
73
|
success = Validator::Android.new.validate
|
71
74
|
elsif( options.command == 'validate' && options.platform == 'rails' )
|
72
75
|
success = Validator::Rails.new.validate
|
76
|
+
elsif( options.command == 'interpolation' && options.platform == 'rails' && !options.type.nil? )
|
77
|
+
Vocab::Merger::Rails.new.check_all_interpolations( options.type, options.strict )
|
78
|
+
elsif( options.command == 'interpolation' && options.platform == 'android' && !options.type.nil? )
|
79
|
+
Vocab::Merger::Android.new.check_all_format_strings( options.type )
|
73
80
|
else
|
74
81
|
puts parser.help
|
75
82
|
end
|
data/lib/vocab/merger/android.rb
CHANGED
@@ -14,7 +14,7 @@ module Vocab
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def merge_file( path )
|
17
|
+
def merge_file( path, strict = true )
|
18
18
|
strings = strings( path )
|
19
19
|
plurals = plurals( path )
|
20
20
|
Vocab::Translator::Android.write( strings, plurals, path )
|
@@ -63,6 +63,11 @@ module Vocab
|
|
63
63
|
send( format_checker, key, new_value, path)
|
64
64
|
end
|
65
65
|
|
66
|
+
def check_all_format_strings( file )
|
67
|
+
strings( file )
|
68
|
+
plurals( file )
|
69
|
+
end
|
70
|
+
|
66
71
|
def plural_format_changed?( key, new_value, path )
|
67
72
|
new_value.each do |inner_key,inner_value|
|
68
73
|
if ( @english_plurals[ key ][ inner_key ].to_s.scan( FORMAT_PATTERN ) != inner_value.to_s.scan( FORMAT_PATTERN ) ) ||
|
data/lib/vocab/merger/base.rb
CHANGED
data/lib/vocab/merger/rails.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
module Vocab
|
2
2
|
module Merger
|
3
3
|
class Rails < Base
|
4
|
-
INTERPOLATION_PATTERN = /%{(
|
4
|
+
INTERPOLATION_PATTERN = /%{(.*?)}/
|
5
5
|
|
6
6
|
def initialize( locales_dir = nil, updates_dir = nil )
|
7
7
|
@locales_dir = locales_dir || 'config/locales'
|
8
8
|
@updates_dir = updates_dir || 'tmp/translations'
|
9
9
|
end
|
10
10
|
|
11
|
-
def merge_file( locales_path )
|
11
|
+
def merge_file( locales_path, strict = false )
|
12
12
|
return unless translatable?( locales_path )
|
13
13
|
create_if_missing( locales_path )
|
14
14
|
|
@@ -16,7 +16,6 @@ module Vocab
|
|
16
16
|
keys = Vocab::Merger::Rails.keys_for_file( locales_path )
|
17
17
|
english = Vocab::Merger::Rails.load_english( locales_path )
|
18
18
|
|
19
|
-
|
20
19
|
# existing translations already in the file
|
21
20
|
locales_translator = translator( locales_path )
|
22
21
|
locales = locales_translator.flattened_translations
|
@@ -38,7 +37,7 @@ module Vocab
|
|
38
37
|
value = updates[ key ] || locales[ key ]
|
39
38
|
if value
|
40
39
|
locales_translator.store( key, value )
|
41
|
-
check_matching_interpolations( key, english[ key ], value, locales_path )
|
40
|
+
check_matching_interpolations( key, english[ key ], value, locales_path, strict )
|
42
41
|
else
|
43
42
|
Vocab.ui.warn( "No translation found for key #{key} while merging #{locales_path}" )
|
44
43
|
end
|
@@ -47,10 +46,37 @@ module Vocab
|
|
47
46
|
locales_translator.write_file( locales_path )
|
48
47
|
end
|
49
48
|
|
50
|
-
def check_matching_interpolations( key, old_value, new_value, locales_path )
|
51
|
-
|
52
|
-
|
49
|
+
def check_matching_interpolations( key, old_value, new_value, locales_path, strict = false )
|
50
|
+
old_interpolations = old_value.to_s.scan( INTERPOLATION_PATTERN )
|
51
|
+
new_interpolations = new_value.to_s.scan( INTERPOLATION_PATTERN )
|
52
|
+
unless strict
|
53
|
+
old_interpolations.sort!
|
54
|
+
new_interpolations.sort!
|
55
|
+
end
|
56
|
+
|
57
|
+
if old_interpolations != new_interpolations
|
58
|
+
Vocab.ui.warn( "Interpolation mismatch for key #{key} in #{locales_path}. \n English: #{old_value} Translation: #{new_value}" )
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def check_all_interpolations( file, strict = false )
|
63
|
+
# list of keys that need to be in the translated file
|
64
|
+
keys = Vocab::Merger::Rails.keys_for_file( file )
|
65
|
+
english = Vocab::Merger::Rails.load_english( file )
|
66
|
+
|
67
|
+
# existing translations already in the file
|
68
|
+
locales_translator = translator( file )
|
69
|
+
locales = locales_translator.flattened_translations
|
70
|
+
|
71
|
+
keys.each do |key|
|
72
|
+
next if Vocab::Translator::Base.ignore_key?( key )
|
73
|
+
|
74
|
+
value = locales[ key ]
|
75
|
+
if value
|
76
|
+
check_matching_interpolations( key, english[ key ], value, file, strict )
|
77
|
+
end
|
53
78
|
end
|
79
|
+
return nil
|
54
80
|
end
|
55
81
|
|
56
82
|
def self.keys_for_file( path )
|
data/lib/vocab/version.rb
CHANGED
@@ -91,7 +91,7 @@ describe "Vocab::Extractor::Rails" do
|
|
91
91
|
|
92
92
|
it "creates a hash of the english translation strings currently in the config" do
|
93
93
|
actual = Vocab::Extractor::Rails.current_strings( @locales_root )
|
94
|
-
expected = {:"en.marketing.banner"=>"This product is so good",
|
94
|
+
expected = {:"en.marketing.banner"=>"This product is so good %{user}",
|
95
95
|
:"en.dashboard.chart"=>"This value has changed",
|
96
96
|
:"en.dashboard.details"=>"This key/value has been added",
|
97
97
|
:"en.menu.first"=>"First menu item",
|
data/spec/merger/base_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe "Vocab::Extractor::Base" do
|
|
12
12
|
files = [ 'es.yml', 'zh.yml' ]
|
13
13
|
@merger.should_receive( :files_to_merge ).and_return( files )
|
14
14
|
files.each do |file|
|
15
|
-
@merger.should_receive( :merge_file ).with( file )
|
15
|
+
@merger.should_receive( :merge_file ).with( file, false )
|
16
16
|
Vocab.ui.should_receive( :say ).with( "Merging file: #{file}" )
|
17
17
|
end
|
18
18
|
@merger.merge
|
data/spec/merger/rails_spec.rb
CHANGED
@@ -149,7 +149,7 @@ describe "Vocab::Merger::Rails" do
|
|
149
149
|
describe "check_matching_interpolations" do
|
150
150
|
it 'warns about interpolation changes' do
|
151
151
|
@merger = Vocab::Merger::Rails.new
|
152
|
-
Vocab.ui.should_receive( :warn ).with( "Interpolation mismatch for key test_key
|
152
|
+
Vocab.ui.should_receive( :warn ).with( "Interpolation mismatch for key test_key in test.yml. \n English: %{test_name} Translation: %{best_name}" )
|
153
153
|
@merger.check_matching_interpolations( 'test_key', "%{test_name}", "%{best_name}", "test.yml")
|
154
154
|
end
|
155
155
|
end
|
@@ -12,7 +12,7 @@ describe 'Vocab::Translator::Rails' do
|
|
12
12
|
it 'loads translations from a directory of yml files' do
|
13
13
|
|
14
14
|
actual = @translator.translations
|
15
|
-
expected = {:marketing=>{:banner=>"This product is so good"},
|
15
|
+
expected = {:marketing=>{:banner=>"This product is so good %{user}"},
|
16
16
|
:dashboard=>{:chart=>"This value has changed",
|
17
17
|
:details=>"This key/value has been added"},
|
18
18
|
:menu=>{:first=>"First menu item",
|
@@ -97,7 +97,7 @@ describe 'Vocab::Translator::Rails' do
|
|
97
97
|
translator = Vocab::Translator::Rails.new
|
98
98
|
translator.load_dir( "#{vocab_root}/spec/data/rails/locales" )
|
99
99
|
actual = translator.flattened_translations
|
100
|
-
expected = {:"marketing.banner"=>"This product is so good",
|
100
|
+
expected = {:"marketing.banner"=>"This product is so good %{user}",
|
101
101
|
:"dashboard.chart"=>"This value has changed",
|
102
102
|
:"dashboard.details"=>"This key/value has been added",
|
103
103
|
:"menu.first"=>"First menu item",
|
@@ -119,7 +119,7 @@ describe 'Vocab::Translator::Rails' do
|
|
119
119
|
translator.load_dir( "#{vocab_root}/spec/data/rails/locales" )
|
120
120
|
actual = translator.flattened_translations
|
121
121
|
expected = { :"models.product.id_125.name" =>"Lazero",
|
122
|
-
:"marketing.banner" =>"hola",
|
122
|
+
:"marketing.banner" =>"hola %{user }",
|
123
123
|
:"models.product.id_55.name" =>"Muy bonita",
|
124
124
|
:"models.product.id_125.description"=>"Verde",
|
125
125
|
:"models.product.id_36.name" =>"No Lazero",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vocab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|