i18nliner 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require 'zlib'
2
2
  require 'i18n'
3
+ require 'active_support/core_ext/string/inflections'
3
4
  require 'i18nliner/base'
4
5
 
5
6
  module I18nliner
@@ -13,18 +14,20 @@ module I18nliner
13
14
 
14
15
  def normalize_default(default, translate_options = {}, options = {})
15
16
  default = infer_pluralization_hash(default, translate_options)
16
- normalize_whitespace!(default, options)
17
+ default = normalize_whitespace(default, options)
17
18
  default
18
19
  end
19
20
 
20
- def normalize_whitespace!(default, options)
21
+ def normalize_whitespace(default, options)
22
+ return default unless default.is_a?(String) || default.is_a?(Hash)
23
+
24
+ default = default.dup
25
+
21
26
  if default.is_a?(Hash)
22
- default.each { |key, value| normalize_whitespace!(value, options) }
23
- return
27
+ default.each { |key, value| default[key] = normalize_whitespace(value, options) }
28
+ return default
24
29
  end
25
30
 
26
- return unless default.is_a?(String)
27
-
28
31
  if options[:remove_whitespace]
29
32
  default.gsub!(/\s+/, ' ')
30
33
  default.strip!
@@ -32,6 +35,7 @@ module I18nliner
32
35
  default.sub!(/\s*\n\z/, '')
33
36
  default.lstrip!
34
37
  end
38
+ default
35
39
  end
36
40
 
37
41
  def infer_pluralization_hash(default, translate_options)
@@ -43,8 +47,9 @@ module I18nliner
43
47
 
44
48
  def infer_key(default, translate_options = {})
45
49
  return unless default && (default.is_a?(String) || default.is_a?(Hash))
50
+ default = normalize_default(default, translate_options, :remove_whitespace => true)
46
51
  default = default[:other].to_s if default.is_a?(Hash)
47
- keyify(normalize_default(default, translate_options, :remove_whitespace => true))
52
+ keyify(default)
48
53
  end
49
54
 
50
55
  def keyify_underscored(string)
@@ -98,7 +103,7 @@ module I18nliner
98
103
  end
99
104
 
100
105
  has_key = key_provided?(*args)
101
- args.unshift infer_key(args[0]) unless has_key
106
+ args.unshift nil unless has_key
102
107
 
103
108
  default = nil
104
109
  default_or_options = args[1]
@@ -111,6 +116,9 @@ module I18nliner
111
116
  raise ArgumentError.new("invalid options argument. expected Hash, got #{options.class}") unless options.is_a?(Hash)
112
117
  options[:default] = default if default
113
118
  options[:i18nliner_inferred_key] = true unless has_key
119
+ unless has_key
120
+ args[0] = infer_key(default, options)
121
+ end
114
122
  args
115
123
  end
116
124
 
@@ -229,7 +229,7 @@ module I18nliner
229
229
  default.strip!
230
230
  default.gsub!(/\s+/, ' ')
231
231
 
232
- key = infer_key(default)
232
+ key = infer_key(default, options)
233
233
  default = default.inspect
234
234
  options = options_to_ruby(options)
235
235
  wrappers = wrappers_to_ruby(wrappers)
@@ -28,8 +28,17 @@ describe I18nliner::Extensions::Core do
28
28
  i18n.translate("Hello %{name}", :name => "bob")
29
29
  end
30
30
 
31
+ it "should not mutate the arguments passed in" do
32
+ expect(i18n).to receive(:simple_translate).with("my.key", :default => "ohai whitespace ")
33
+ key = "my.key".freeze
34
+ default = " ohai whitespace ".freeze
35
+ expect {
36
+ i18n.translate(key, default)
37
+ }.to_not raise_error
38
+ end
39
+
31
40
  it "should infer pluralization hashes" do
32
- expect(i18n).to receive(:simple_translate).with("light_6feedaaa", :default => {:one => "1 light", :other => "%{count} lights"}, count: 1)
41
+ expect(i18n).to receive(:simple_translate).with("count_lights_58339e29", :default => {:one => "1 light", :other => "%{count} lights"}, count: 1)
33
42
  i18n.translate("light", :count => 1)
34
43
  end
35
44
 
@@ -10,6 +10,7 @@ describe I18nliner::Extractors::TranslateCall do
10
10
 
11
11
  let(:no_scope) { I18nliner::Scope.new(nil) }
12
12
  let(:scope) { I18nliner::Scope.new("foo", :auto => true, :allow_relative => true) }
13
+ let(:erb_scope) { I18nliner::Scope.new(nil, :remove_whitespace => true) }
13
14
 
14
15
  describe "signature" do
15
16
  it "should reject extra arguments" do
@@ -116,12 +117,28 @@ describe I18nliner::Extractors::TranslateCall do
116
117
  call(scope, ['.key1', '.key2'], "value").translations.map(&:first).should == ['foo.key1', 'foo.key2']
117
118
  end
118
119
 
119
- it "should strip whitespace from defaults" do
120
- call(no_scope, "\t whitespace \n\t ").translations[0][1].should == "whitespace"
120
+ it "should strip leading whitespace from defaults" do
121
+ call(no_scope, "\t white space \n\t ").translations[0][1].should == "white space \n\t "
122
+ end
123
+
124
+ it "should strip all whitespace from defaults if the scope requests it" do
125
+ call(erb_scope, "\t white space \n\t ").translations[0][1].should == "white space"
121
126
  end
122
127
  end
123
128
 
124
129
  describe "pluralization" do
130
+ describe "keys" do
131
+ it "should be inferred from a word" do
132
+ translations = call(no_scope, "person", {:count => Object.new}).translations
133
+ translations.map(&:first).sort.should == ["count_people_489946e7.one", "count_people_489946e7.other"]
134
+ end
135
+
136
+ it "should be inferred from a hash" do
137
+ translations = call(no_scope, {:one => "just you", :other => "lotsa peeps"}, {:count => Object.new}).translations
138
+ translations.map(&:first).sort.should == ["lotsa_peeps_41499c40.one", "lotsa_peeps_41499c40.other"]
139
+ end
140
+ end
141
+
125
142
  describe "defaults" do
126
143
  it "should be inferred" do
127
144
  translations = call(no_scope, "person", {:count => Object.new}).translations
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18nliner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
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: 2014-11-11 00:00:00.000000000 Z
12
+ date: 2014-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -139,22 +139,6 @@ dependencies:
139
139
  - - ! '>='
140
140
  - !ruby/object:Gem::Version
141
141
  version: 1.5.0
142
- - !ruby/object:Gem::Dependency
143
- name: debugger
144
- requirement: !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ! '>='
148
- - !ruby/object:Gem::Version
149
- version: '0'
150
- type: :development
151
- prerelease: false
152
- version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
- requirements:
155
- - - ! '>='
156
- - !ruby/object:Gem::Version
157
- version: '0'
158
142
  - !ruby/object:Gem::Dependency
159
143
  name: rspec
160
144
  requirement: !ruby/object:Gem::Requirement
@@ -262,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
262
246
  version: 1.3.5
263
247
  requirements: []
264
248
  rubyforge_project:
265
- rubygems_version: 1.8.23
249
+ rubygems_version: 1.8.23.2
266
250
  signing_key:
267
251
  specification_version: 3
268
252
  summary: I18n made simple