i18nliner 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -331,16 +331,10 @@ modification.
331
331
 
332
332
  I18nliner requires at least Ruby 1.9.3 and Rails 3.
333
333
 
334
- ## What about JavaScript/Handlebars?
335
-
336
- [Coming](https://github.com/jenseng/i18nliner-js) [soon](https://github.com/fivetanley/i18ninliner-handlebars).
337
- I18nliner was inspired by some [I18n extensions](https://github.com/instructure/canvas-lms/tree/master/lib/i18n_extraction)
338
- I did in [Canvas-LMS](https://github.com/instructure/canvas-lms). While
339
- it also has the JavaScript/Handlebars equivalents, they are tightly
340
- coupled to Canvas-LMS and are written in Ruby. So basically we're talking
341
- a full reimplementation in JavaScript using
342
- [esprima](http://esprima.org/) instead of the [shameful, brittle, regexy hack](http://cdn.memegenerator.net/instances/400x/24091937.jpg)
343
- that is js_extractor.
334
+ ## Related Projects
335
+
336
+ * [i18nliner-js](https://github.com/jenseng/i18nliner-js)
337
+ * [i18nliner-handlebars](https://github.com/fivetanley/i18nliner-handlebars)
344
338
 
345
339
  ## License
346
340
 
@@ -7,8 +7,8 @@ module I18nliner
7
7
  ALLOWED_PLURALIZATION_KEYS = [:zero, :one, :few, :many, :other]
8
8
  REQUIRED_PLURALIZATION_KEYS = [:one, :other]
9
9
 
10
- def normalize_key(key, scope = Scope.root)
11
- scope.normalize_key(key.to_s)
10
+ def normalize_key(key, scope, inferred)
11
+ scope.normalize_key(key.to_s, inferred)
12
12
  end
13
13
 
14
14
  def normalize_default(default, translate_options = {}, options = {})
@@ -78,6 +78,7 @@ module I18nliner
78
78
  def key_provided?(key_or_default = nil, default_or_options = nil, maybe_options = nil, *others)
79
79
  return false if key_or_default.is_a?(Hash)
80
80
  return true if key_or_default.is_a?(Symbol)
81
+ raise ArgumentError.new("invalid key_or_default argument. expected String, Symbol or Hash, got #{key_or_default.class}") unless key_or_default.is_a?(String)
81
82
  return true if default_or_options.is_a?(String)
82
83
  return true if maybe_options
83
84
  return true if key_or_default =~ /\A\.?(\w+\.)+\w+\z/
@@ -90,21 +91,26 @@ module I18nliner
90
91
  (hash.keys - ALLOWED_PLURALIZATION_KEYS).size == 0
91
92
  end
92
93
 
93
- def infer_arguments(args, meta = {})
94
+ def infer_arguments(args)
95
+ raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)") if args.empty? || args.size > 3
94
96
  if args.size == 2 && args[1].is_a?(Hash) && args[1][:default]
95
97
  return args
96
98
  end
97
99
 
98
100
  has_key = key_provided?(*args)
99
- meta[:inferred_key] = !has_key
100
101
  args.unshift infer_key(args[0]) unless has_key
101
102
 
103
+ default = nil
102
104
  default_or_options = args[1]
103
105
  if args[2] || default_or_options.is_a?(String) || pluralization_hash?(default_or_options)
104
- options = args[2] ||= {}
105
- options[:default] = args.delete_at(1) if options.is_a?(Hash)
106
+ default = args.delete_at(1)
106
107
  end
107
108
  args << {} if args.size == 1
109
+ options = args[1]
110
+ raise ArgumentError.new("invalid default translation. expected Hash or String, got #{default.class}") unless default.nil? || default.is_a?(String) || default.is_a?(Hash)
111
+ raise ArgumentError.new("invalid options argument. expected Hash, got #{options.class}") unless options.is_a?(Hash)
112
+ options[:default] = default if default
113
+ options[:i18n_inferred_key] = true unless has_key
108
114
  args
109
115
  end
110
116
 
@@ -9,7 +9,8 @@ module I18nliner
9
9
  key, options = *CallHelpers.infer_arguments(args)
10
10
 
11
11
  scope = options.delete(:i18n_scope) || Scope.root
12
- key = CallHelpers.normalize_key(key, scope)
12
+ inferred_key = options.delete(:i18n_inferred_key)
13
+ key = CallHelpers.normalize_key(key, scope, inferred_key)
13
14
 
14
15
  if default = options[:default]
15
16
  options[:default] = CallHelpers.normalize_default(default, options)
@@ -22,6 +23,8 @@ module I18nliner
22
23
  end
23
24
 
24
25
  result
26
+ rescue ArgumentError
27
+ raise
25
28
  end
26
29
  alias :t :translate
27
30
  alias :t! :translate
@@ -14,11 +14,12 @@ module I18nliner
14
14
  # 1. the user did something weird (e.g. <%= t{ "haha" } %>)
15
15
  # 2. the erb pre processor missed it somehow (bug)
16
16
  raise InvalidBlockUsageError.new("block translate calls need to be output (i.e. `<%=`) and the block body must be of the form `%>your string<%`") if block_given?
17
- raise ArgumentError.new("wrong number of arguments (0 for 1..3)") if args.empty? || args.size > 3
18
17
  key, options = CallHelpers.infer_arguments(args)
19
18
  options = inferpolate(options) if I18nliner.infer_interpolation_values
20
19
  options[:i18n_scope] = i18n_scope
21
20
  super(key, options)
21
+ rescue ArgumentError
22
+ raise
22
23
  end
23
24
  alias :t :translate
24
25
  alias :t! :translate
@@ -29,7 +29,7 @@ module I18nliner
29
29
  end
30
30
 
31
31
  def normalize
32
- @key = normalize_key(@key, @scope) unless @meta[:inferred_key]
32
+ @key = normalize_key(@key, @scope, @options[:i18n_inferred_key])
33
33
  @default = normalize_default(@default, @options || {}, {:remove_whitespace => @scope.remove_whitespace?})
34
34
  end
35
35
 
@@ -77,15 +77,11 @@ module I18nliner
77
77
  # default_string [, options]
78
78
  # default_hash, options
79
79
  def normalize_arguments(args)
80
- raise InvalidSignatureError.new(@line, args) if args.empty?
81
-
82
- @key, @options, *others = infer_arguments(args, @meta)
83
-
84
- raise InvalidSignatureError.new(@line, args) if !others.empty?
85
- raise InvalidSignatureError.new(@line, args) unless @key.is_a?(Symbol) || @key.is_a?(String)
86
- raise InvalidSignatureError.new(@line, args) unless @options.nil? || @options.is_a?(Hash)
87
- @default = @options.delete(:default) if @options
80
+ @key, @options = infer_arguments(args)
81
+ @default = @options.delete(:default)
88
82
  raise InvalidSignatureError.new(@line, args) unless @default.nil? || @default.is_a?(String) || @default.is_a?(Hash)
83
+ rescue ArgumentError
84
+ raise InvalidSignatureError.new(@line, args)
89
85
  end
90
86
 
91
87
  def validate_interpolation_values(key, default)
@@ -237,7 +237,7 @@ module I18nliner
237
237
  end
238
238
 
239
239
  def options_to_ruby(options)
240
- return if options.size == 0
240
+ options["i18n_inferred_key"] = "true"
241
241
  options.map do |key, value|
242
242
  ":" << key << " => (" << value << ")"
243
243
  end.join(", ")
@@ -14,7 +14,7 @@ module I18nliner
14
14
  @context = options.fetch(:context, nil)
15
15
  end
16
16
 
17
- def normalize_key(key)
17
+ def normalize_key(key, inferred)
18
18
  if allow_relative? && (key = key.dup) && key.sub!(/\A\./, '')
19
19
  scope + key
20
20
  else
data/lib/i18nliner.rb CHANGED
@@ -3,6 +3,6 @@ require 'i18nliner/base'
3
3
 
4
4
  require 'i18nliner/extensions/core'
5
5
  I18n.send :extend, I18nliner::Extensions::Core
6
- I18n::RESERVED_KEYS << :i18n_scope
6
+ I18n::RESERVED_KEYS << :i18n_scope << :i18n_inferred_key
7
7
 
8
8
  require 'i18nliner/railtie' if defined?(Rails)
@@ -6,8 +6,9 @@ describe I18nliner::Extensions::Controller do
6
6
  let(:i18n) do
7
7
  Module.new do
8
8
  extend(Module.new do
9
- def translate(*args)
10
- I18n.translate(*args)
9
+ def translate(key, options)
10
+ options.delete(:i18n_inferred_key)
11
+ I18n.translate(key, options)
11
12
  end
12
13
 
13
14
  def controller_name
@@ -5,11 +5,6 @@ require 'i18nliner/call_helpers'
5
5
  describe I18nliner::Extensions::Model do
6
6
  let(:i18n) do
7
7
  Module.new do
8
- extend(Module.new do
9
- def translate(*args)
10
- I18n.translate(*args)
11
- end
12
- end)
13
8
  extend I18nliner::Extensions::Model
14
9
  end
15
10
  end
@@ -20,7 +15,7 @@ describe I18nliner::Extensions::Model do
20
15
  i18n.stub(:foo).and_return("FOO")
21
16
  I18nliner::CallHelpers.stub(:infer_key).and_return(:key)
22
17
 
23
- expect(I18n).to receive(:translate).with(:key, :default => "hello %{foo}", :foo => "FOO", :i18n_scope => i18n.i18n_scope)
18
+ expect(I18n).to receive(:translate).with(:key, :default => "hello %{foo}", :foo => "FOO", :i18n_scope => i18n.i18n_scope, :i18n_inferred_key => true)
24
19
  i18n.translate("hello %{foo}")
25
20
  end
26
21
 
@@ -6,8 +6,9 @@ describe I18nliner::Extensions::View do
6
6
  let(:i18n) do
7
7
  Module.new do
8
8
  extend(Module.new do
9
- def translate(*args)
10
- I18n.translate(*args)
9
+ def translate(key, options)
10
+ options.delete(:i18n_inferred_key)
11
+ I18n.translate(key, options)
11
12
  end
12
13
  end)
13
14
  extend I18nliner::Extensions::View
@@ -17,12 +17,12 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
17
17
 
18
18
  it "should transform t block expressions" do
19
19
  process("<%= t do %>hello world!<% end %>").should ==
20
- '<%= t :key, "hello world!" %>'
20
+ '<%= t :key, "hello world!", :i18n_inferred_key => (true) %>'
21
21
  end
22
22
 
23
23
  it "should remove extraneous whitespace from the default" do
24
24
  process("<%= t do %> ohai! lulz\t <% end %>").should ==
25
- '<%= t :key, "ohai! lulz" %>'
25
+ '<%= t :key, "ohai! lulz", :i18n_inferred_key => (true) %>'
26
26
  end
27
27
 
28
28
  # so that line numbers are close-ish when you get an error in a
@@ -39,7 +39,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
39
39
  <% end %>
40
40
  SOURCE
41
41
  should == <<-EXPECTED.strip_heredoc
42
- <%= t :key, "ohai! %{test} lulz", :test => (test)
42
+ <%= t :key, "ohai! %{test} lulz", :test => (test), :i18n_inferred_key => (true)
43
43
 
44
44
 
45
45
 
@@ -57,7 +57,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
57
57
  SOURCE
58
58
  should == <<-EXPECTED
59
59
  <%= form_for do %>
60
- <%= t :key, "Your Name" %>
60
+ <%= t :key, "Your Name", :i18n_inferred_key => (true) %>
61
61
  <input>
62
62
  <% end %>
63
63
  EXPECTED
@@ -87,7 +87,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
87
87
  <% end %>
88
88
  SOURCE
89
89
  should == <<-EXPECTED
90
- <%= t :key, "*bold*, or even **combos** get wrapper'd", :wrappers => ["<b>\\\\1</b>", "<a href=\\\"#\\\"><i><img>\\\\1</i></a>"] %>
90
+ <%= t :key, "*bold*, or even **combos** get wrapper'd", :i18n_inferred_key => (true), :wrappers => ["<b>\\\\1</b>", "<a href=\\\"#\\\"><i><img>\\\\1</i></a>"] %>
91
91
  EXPECTED
92
92
  end
93
93
 
@@ -104,7 +104,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
104
104
  <% end %>
105
105
  SOURCE
106
106
  should == <<-EXPECTED
107
- <%= t :key, "You should *create a profile*. idk why **this link** has concatention", :wrappers => [link_to("\\\\1", "/profile"), link_to("\\\\1", "/zomg")] %>
107
+ <%= t :key, "You should *create a profile*. idk why **this link** has concatention", :i18n_inferred_key => (true), :wrappers => [link_to("\\\\1", "/profile"), link_to("\\\\1", "/zomg")] %>
108
108
  EXPECTED
109
109
  end
110
110
 
@@ -115,7 +115,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
115
115
  <% end %>
116
116
  SOURCE
117
117
  should == <<-EXPECTED
118
- <%= t :key, "Your account rep is *%{user_name}*", :user_name => (@user.name), :wrappers => [link_to("\\\\1", "/user/\#{@user.id}")] %>
118
+ <%= t :key, "Your account rep is *%{user_name}*", :user_name => (@user.name), :i18n_inferred_key => (true), :wrappers => [link_to("\\\\1", "/user/\#{@user.id}")] %>
119
119
  EXPECTED
120
120
  end
121
121
 
@@ -129,7 +129,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
129
129
  <% end %>
130
130
  SOURCE
131
131
  should == <<-EXPECTED
132
- <%= t :key, "the wrappers for **these** **links** are the same, as are the ones for *these* *tags*", :wrappers => ["<b>\\\\1</b>", link_to("\\\\1", url)] %>
132
+ <%= t :key, "the wrappers for **these** **links** are the same, as are the ones for *these* *tags*", :i18n_inferred_key => (true), :wrappers => ["<b>\\\\1</b>", link_to("\\\\1", url)] %>
133
133
  EXPECTED
134
134
  end
135
135
 
@@ -140,7 +140,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
140
140
  <% end %>
141
141
  SOURCE
142
142
  should == <<-EXPECTED
143
- <%= t :key, "Hello, %{name}", :name => (name) %>
143
+ <%= t :key, "Hello, %{name}", :name => (name), :i18n_inferred_key => (true) %>
144
144
  EXPECTED
145
145
  end
146
146
 
@@ -151,7 +151,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
151
151
  <% end %>
152
152
  SOURCE
153
153
  should == <<-EXPECTED
154
- <%= t :key, "Go to *your account*", :wrappers => ["<a href=\\"/asdf\\" title=\\"\#{name}\\">\\\\1</a>"] %>
154
+ <%= t :key, "Go to *your account*", :i18n_inferred_key => (true), :wrappers => ["<a href=\\"/asdf\\" title=\\"\#{name}\\">\\\\1</a>"] %>
155
155
  EXPECTED
156
156
  end
157
157
 
@@ -165,7 +165,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
165
165
  <% end %>
166
166
  SOURCE
167
167
  should == <<-EXPECTED
168
- <%= t :key, "Go to *your account*", :wrappers => ["<a href=\\"/asdf\\" title=\\"\#{t :key, \"manage account stuffs, %{name}\", :name => (name)}\\">\\\\1</a>"] %>
168
+ <%= t :key, "Go to *your account*", :i18n_inferred_key => (true), :wrappers => ["<a href=\\"/asdf\\" title=\\"\#{t :key, \"manage account stuffs, %{name}\", :name => (name), :i18n_inferred_key => (true)}\\">\\\\1</a>"] %>
169
169
  EXPECTED
170
170
  end
171
171
 
@@ -176,7 +176,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
176
176
  <% end %>
177
177
  SOURCE
178
178
  should == <<-EXPECTED
179
- <%= t :key, "Create %{input_name_count} groups", :input_name_count => ("<input name=\\"count\\">".html_safe) %>
179
+ <%= t :key, "Create %{input_name_count} groups", :input_name_count => ("<input name=\\"count\\">".html_safe), :i18n_inferred_key => (true) %>
180
180
  EXPECTED
181
181
  end
182
182
 
@@ -187,7 +187,7 @@ describe I18nliner::PreProcessors::ErbPreProcessor do
187
187
  <% end %>
188
188
  SOURCE
189
189
  should == <<-EXPECTED
190
- <%= t :key, "© %{year} ACME Corp. All Rights Reserved. Our lawyers > your lawyers", :year => (year) %>
190
+ <%= t :key, "© %{year} ACME Corp. All Rights Reserved. Our lawyers > your lawyers", :year => (year), :i18n_inferred_key => (true) %>
191
191
  EXPECTED
192
192
  end
193
193
  end
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.6
4
+ version: 0.0.7
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-10-15 00:00:00.000000000 Z
12
+ date: 2014-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport