i18n-inflector-rails 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/ChangeLog +42 -0
- data/Gemfile +1 -1
- data/README.rdoc +33 -26
- data/Rakefile +4 -4
- data/docs/HISTORY +17 -0
- data/docs/TODO +0 -1
- data/i18n-inflector-rails.gemspec +1 -1
- data/lib/i18n-inflector-rails.rb +0 -2
- data/lib/i18n-inflector-rails/errors.rb +4 -2
- data/lib/i18n-inflector-rails/inflector.rb +247 -92
- data/lib/i18n-inflector-rails/options.rb +90 -38
- data/lib/i18n-inflector-rails/version.rb +1 -1
- data/spec/inflector_spec.rb +162 -67
- metadata +6 -7
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/ChangeLog
CHANGED
@@ -1,3 +1,45 @@
|
|
1
|
+
commit 5ee55b3ea54cfa86e541a47f27cbe321932c3820
|
2
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
3
|
+
Date: Tue Feb 15 01:38:13 2011 +0100
|
4
|
+
|
5
|
+
Release 1.0.0
|
6
|
+
|
7
|
+
commit 46377c25557637e0f576a637b6abd203555989da
|
8
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
9
|
+
Date: Tue Feb 15 01:09:46 2011 +0100
|
10
|
+
|
11
|
+
Documentation updated
|
12
|
+
|
13
|
+
commit f3419fd70560927aec82813e25d6dd4d33e6e629
|
14
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
15
|
+
Date: Tue Feb 15 01:09:07 2011 +0100
|
16
|
+
|
17
|
+
Specification updated
|
18
|
+
|
19
|
+
commit 8c2907f62a9273a66bc5be0e90da92c0299d1e72
|
20
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
21
|
+
Date: Tue Feb 15 01:07:53 2011 +0100
|
22
|
+
|
23
|
+
ClassMethods and InflectedTranslate refactored
|
24
|
+
|
25
|
+
- There are no additional Procs support, but Inflector's inflection methods may take arguments.
|
26
|
+
- Translation method now makes use of lazy methods feature of I18n Inflector.
|
27
|
+
- The internal hash is now i18n_inflector_kinds and it's indexed by kinds.
|
28
|
+
- Rewritten argument parsing for inflection_method.
|
29
|
+
- Added no_inflection_method_for (aka no_inflection_kind).
|
30
|
+
|
31
|
+
commit aee2f6f484bee1812ecf8765b28d910adc8412e5
|
32
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
33
|
+
Date: Tue Feb 15 00:58:53 2011 +0100
|
34
|
+
|
35
|
+
New switches are now added by opening the InflectionOptions class, added new switch :lazy_methods
|
36
|
+
|
37
|
+
commit 938f7bf971d8c00710b2202dc15e37929f8391dc
|
38
|
+
Author: Paweł Wilk <siefca@gnu.org>
|
39
|
+
Date: Mon Jan 17 15:01:07 2011 +0100
|
40
|
+
|
41
|
+
Link in documentation fixed
|
42
|
+
|
1
43
|
commit 15e330e957f34579fa95aa2036f0c06fd168ab54
|
2
44
|
Author: Paweł Wilk <siefca@gnu.org>
|
3
45
|
Date: Sun Jan 16 07:06:57 2011 +0100
|
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Simple I18n Inflector for Rails
|
2
2
|
|
3
|
-
<b>i18n-inflector-rails version <tt>0
|
3
|
+
<b>i18n-inflector-rails version <tt>1.0</tt></b> (<b><tt>Rain</tt></b>)
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -11,7 +11,7 @@
|
|
11
11
|
== Summary
|
12
12
|
|
13
13
|
This Rails plug-in contains bindings for the I18n Inflector module for Ruby's I18n.
|
14
|
-
It overwrites the translate() method from Rails in a way that it will
|
14
|
+
It overwrites the <tt>translate()</tt> method from Rails in a way that it will
|
15
15
|
interpolate additional inflection tokens present in translations.
|
16
16
|
These tokens may appear in *patterns* which are contained within
|
17
17
|
<tt>@{</tt> and <tt>}</tt> symbols.
|
@@ -19,15 +19,15 @@ These tokens may appear in *patterns* which are contained within
|
|
19
19
|
== Why?
|
20
20
|
|
21
21
|
It's intended to be used in a projects where tranlations are performed
|
22
|
-
by many people, yet there is a need to
|
23
|
-
|
22
|
+
by many people, yet there is a need to inflect sentences or words by
|
23
|
+
user's gender, person or other data.
|
24
24
|
|
25
25
|
To achieve similar functionality lambdas can be used but including
|
26
|
-
|
26
|
+
Proc objects in translations may be considered unsafe.
|
27
27
|
|
28
28
|
If you have a troop of happy translators that shouldn't have the
|
29
29
|
ability to execute any code yet you need some simple inflection
|
30
|
-
then
|
30
|
+
then this plug-in might help you.
|
31
31
|
|
32
32
|
== Synopsis
|
33
33
|
|
@@ -50,7 +50,7 @@ then you should use this module.
|
|
50
50
|
@neuter: @n
|
51
51
|
man: @male
|
52
52
|
woman: @female
|
53
|
-
default:
|
53
|
+
default: n
|
54
54
|
|
55
55
|
===== In a controller:
|
56
56
|
|
@@ -78,26 +78,26 @@ then you should use this module.
|
|
78
78
|
|
79
79
|
== Description
|
80
80
|
|
81
|
-
You can
|
82
|
-
of tokens to group them in a meaningful, semantical sets.
|
83
|
-
apply I18n Inflector to do simple inflection by a gender or a person, when some
|
84
|
-
language requires it.
|
81
|
+
You can create your own kinds (gender, title, person, time, author, etc.)
|
82
|
+
of tokens to group them in a meaningful, semantical sets.
|
85
83
|
|
86
|
-
This
|
87
|
-
|
88
|
-
|
84
|
+
This plug-in adds {I18n::Inflector::Rails::ClassMethods#inflection_method inflection_method},
|
85
|
+
{I18n::Inflector::Rails::ClassMethods#no_inflection_method no_inflection_method}
|
86
|
+
and {I18n::Inflector::Rails::ClassMethods#no_inflection_method_for no_inflection_method_for} clauses
|
87
|
+
that can be used in controllers and views. Using that clauses makes it easy to register
|
88
|
+
methods that will be called to obtain certain inflection options.
|
89
|
+
It is also possible to set up an inflection method for a so called
|
90
|
+
strict kind commonly used in a so called named and complex patterns.
|
89
91
|
|
90
|
-
This
|
92
|
+
This plug-in uses i18n-inflector[https://rubygems.org/gems/i18n-inflector]
|
91
93
|
module which allows passing inflection options to the translate method.
|
92
|
-
You may find
|
93
|
-
allow you to read inflection data in many different ways. You
|
94
|
-
example, use it to generate forms containing languages that are using
|
95
|
-
all known inflection kinds or tokens assigned to them.
|
96
|
-
bound to default I18n backend use:
|
97
|
-
I18n.inflector
|
94
|
+
You may find I18n Inflector's API and a default object very helpful since
|
95
|
+
they allow you to read inflection data in many different ways. You might,
|
96
|
+
for example, use it to generate forms containing lists of languages that are using
|
97
|
+
inflection, or to view all known inflection kinds or tokens assigned to them.
|
98
98
|
|
99
|
-
|
100
|
-
|
99
|
+
To access the Inflector object bound to default I18n backend use:
|
100
|
+
I18n.inflector
|
101
101
|
|
102
102
|
== Requirements
|
103
103
|
|
@@ -119,7 +119,7 @@ To understand how the I18n Inflector works please read {I18n Inflector documenta
|
|
119
119
|
|
120
120
|
== Installation
|
121
121
|
|
122
|
-
* <tt>
|
122
|
+
* <tt>gem install i18n-inflector-rails</tt>
|
123
123
|
|
124
124
|
== Specs
|
125
125
|
|
@@ -130,9 +130,16 @@ You can run RSpec examples both with
|
|
130
130
|
|
131
131
|
== More information
|
132
132
|
|
133
|
-
See {
|
133
|
+
See {I18n::Inflector::Rails::ClassMethods} to learn how to use +inflection_method+ and +no_inflection_method+.
|
134
|
+
|
135
|
+
See {I18n::Inflector::Rails::InflectedTranslate} to learn about using the translation wrapper.
|
136
|
+
|
137
|
+
See {I18n::Inflector::InflectionOptions} to know more about switches that can be used to control the engine.
|
138
|
+
|
139
|
+
See the {whole documentation}[http://rubydoc.info/gems/i18n-inflector-rails/] for more information.
|
134
140
|
|
135
|
-
|
141
|
+
To know how the basics and to learn more about Inflector that is used by this plug-in
|
142
|
+
see {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector].
|
136
143
|
|
137
144
|
== Credits
|
138
145
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# -*- ruby -*-
|
3
|
-
|
3
|
+
|
4
4
|
$:.unshift File.join(File.dirname(__FILE__), "lib")
|
5
5
|
|
6
6
|
require 'rubygems'
|
@@ -40,15 +40,15 @@ Hoe.spec 'i18n-inflector-rails' do
|
|
40
40
|
self.rsync_args << '--chmod=a+rX'
|
41
41
|
self.readme_file = 'README.rdoc'
|
42
42
|
self.history_file = 'docs/HISTORY'
|
43
|
-
|
44
|
-
extra_deps << ['i18n-inflector', '~> 2.
|
43
|
+
|
44
|
+
extra_deps << ['i18n-inflector', '~> 2.3'] <<
|
45
45
|
['railties', '~> 3.0'] <<
|
46
46
|
['actionpack', '~> 3.0']
|
47
47
|
extra_dev_deps << ['rspec', '>= 2.3.0'] <<
|
48
48
|
['yard', '>= 0.6.4'] <<
|
49
49
|
['bundler', '>= 1.0.7'] <<
|
50
50
|
['hoe-bundler', '>= 1.0.0']
|
51
|
-
|
51
|
+
|
52
52
|
unless extra_dev_deps.flatten.include?('hoe-yard')
|
53
53
|
extra_dev_deps << ['hoe-yard', '>= 0.1.2']
|
54
54
|
end
|
data/docs/HISTORY
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
=== 1.0.0 / 2011-02-14
|
2
|
+
|
3
|
+
* major enhancements
|
4
|
+
|
5
|
+
* Inflection methods evaluation is now lazy; passing Method objects to the Inflector
|
6
|
+
* Added strict kinds (named patterns) better support
|
7
|
+
* Added no_inflection_method_for (alias no_inflection_kind) clause
|
8
|
+
* Changed way that associations are stored from (method => kind) to (kind => method)
|
9
|
+
* Removed block passing support from inflection_method since I18n Inflector supports inflection methods with arguments
|
10
|
+
|
11
|
+
* minor enhancements
|
12
|
+
|
13
|
+
* Removed magic symbols from code
|
14
|
+
* Improved arguments parsing in inflection_method
|
15
|
+
* Added auto_helper global switch
|
16
|
+
* Added lazy_methods global switch
|
17
|
+
|
1
18
|
=== 0.2.0 / 2011-01-15
|
2
19
|
|
3
20
|
* 1 major enhancement
|
data/docs/TODO
CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.required_rubygems_version = '>= 1.3.5'
|
26
26
|
s.specification_version = 3
|
27
27
|
|
28
|
-
s.add_dependency 'i18n-inflector', '~> 2.
|
28
|
+
s.add_dependency 'i18n-inflector', '~> 2.3'
|
29
29
|
s.add_dependency 'railties', '~> 3.0'
|
30
30
|
s.add_dependency 'actionpack', '~> 3.0'
|
31
31
|
s.add_development_dependency 'rspec', '>= 2.3.0'
|
data/lib/i18n-inflector-rails.rb
CHANGED
@@ -6,8 +6,6 @@ require 'i18n-inflector-rails/errors'
|
|
6
6
|
require 'i18n-inflector-rails/options'
|
7
7
|
require 'i18n-inflector-rails/inflector'
|
8
8
|
|
9
|
-
I18n::Inflector::InflectionOptions.send(:include, I18n::Inflector::Rails::AdditionalOptions)
|
10
|
-
|
11
9
|
if defined? Rails::Engine
|
12
10
|
|
13
11
|
require 'i18n-inflector-rails/railtie'
|
@@ -9,7 +9,9 @@
|
|
9
9
|
module I18n
|
10
10
|
module Inflector
|
11
11
|
module Rails
|
12
|
-
|
12
|
+
|
13
|
+
# This class is for reporting bad inflection method
|
14
|
+
# in the given assignment.
|
13
15
|
class BadInflectionMethod < I18n::ArgumentError
|
14
16
|
attr_reader :assignment
|
15
17
|
def initialize(assignment)
|
@@ -17,7 +19,7 @@ module I18n
|
|
17
19
|
super "The given assignment is invalid: #{assignment}"
|
18
20
|
end
|
19
21
|
end
|
20
|
-
|
22
|
+
|
21
23
|
end
|
22
24
|
end
|
23
25
|
end
|
@@ -10,38 +10,40 @@
|
|
10
10
|
# inflection tokens defined in translation data.
|
11
11
|
|
12
12
|
module I18n
|
13
|
+
|
14
|
+
# @abstract It is defined in {I18n Inflector library}[http://rubydoc.info/gems/i18n-inflector].
|
13
15
|
module Inflector
|
14
16
|
module Rails
|
15
|
-
|
17
|
+
|
16
18
|
# This module contains instance methods for ActionController.
|
17
19
|
module InstanceMethods
|
18
|
-
|
19
|
-
# This method calls the class method {I18n::Inflector::Rails::ClassMethods#
|
20
|
-
def
|
21
|
-
self.class.
|
20
|
+
|
21
|
+
# This method calls the class method {I18n::Inflector::Rails::ClassMethods#i18n_inflector_kinds}
|
22
|
+
def i18n_inflector_kinds
|
23
|
+
self.class.i18n_inflector_kinds
|
22
24
|
end
|
23
25
|
|
24
26
|
# @private
|
25
27
|
def self.included(base)
|
26
|
-
base.helper_method(:
|
28
|
+
base.helper_method(:i18n_inflector_kinds)
|
27
29
|
end
|
28
|
-
|
30
|
+
|
29
31
|
end # instance methods
|
30
|
-
|
32
|
+
|
31
33
|
# This module contains class methods for ActionController.
|
32
34
|
module ClassMethods
|
33
|
-
|
34
|
-
# This method reads the internal Hash +
|
35
|
+
|
36
|
+
# This method reads the internal Hash +i18n_inflector_kinds+ containing registered
|
35
37
|
# inflection methods and the assigned kinds. It also reads any methods
|
36
38
|
# assignments that were defined earlier in the inheritance path and
|
37
39
|
# merges them with current results; the most current entries will
|
38
40
|
# override the entries defined before.
|
39
41
|
#
|
40
42
|
# @api public
|
41
|
-
# @return [Hash] the Hash containing assignments made by using {inflection_method}
|
42
|
-
def
|
43
|
-
prev = superclass.respond_to?(:
|
44
|
-
return @
|
43
|
+
# @return [Hash] the Hash containing assignments made by using {#inflection_method}
|
44
|
+
def i18n_inflector_kinds
|
45
|
+
prev = superclass.respond_to?(:i18n_inflector_kinds) ? superclass.i18n_inflector_kinds : {}
|
46
|
+
return @i18n_inflector_kinds.nil? ? prev : prev.merge(@i18n_inflector_kinds)
|
45
47
|
end
|
46
48
|
|
47
49
|
# This method allows to assign methods (typically attribute readers)
|
@@ -52,96 +54,248 @@ module I18n
|
|
52
54
|
# given then method assumes that the name of a kind is the same as the given
|
53
55
|
# name of a method.
|
54
56
|
#
|
57
|
+
# If the given kind begins with +@+ then strict kind is assumed. If there is
|
58
|
+
# no kind given but the given method name begins with +@+ character then
|
59
|
+
# also strict kind of the same name is assumed but method name is memorized
|
60
|
+
# without the leading symbol.
|
61
|
+
#
|
62
|
+
# Registering method for feeding an inflection option describing a strict
|
63
|
+
# kind might be good idea when using some regular kind of the same name,
|
64
|
+
# but note that regular kind inflection option is also tried by the
|
65
|
+
# translation method when strict kind is in use.
|
66
|
+
#
|
67
|
+
# In case of registering two methods of different
|
68
|
+
# names but assigned to kind and to a strict kind using the same base name,
|
69
|
+
# a named inflection pattern will first use an inflection option obtained
|
70
|
+
# from a method assigned to a strict kind. Note that it is impossible
|
71
|
+
# to use short formed +inflection_method+ calls to register a method for both
|
72
|
+
# strict and regular inflection kind, since the method names will be the
|
73
|
+
# same and the second call will overwrite the first one.
|
74
|
+
#
|
75
|
+
# @example Registering an inflection method for the kind gender visible in a whole application
|
76
|
+
# class ApplicationController < ActionController::Base
|
77
|
+
# inflection_method :gender
|
78
|
+
# […]
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# @example Registering an inflection method for the strict kind @gender visible in a whole application
|
82
|
+
# class ApplicationController < ActionController::Base
|
83
|
+
# inflection_method :@gender
|
84
|
+
# […]
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# @example Registering a custom-named inflection method for the kind gender
|
88
|
+
# class ApplicationController < ActionController::Base
|
89
|
+
# inflection_method :get_user_gender => :gender
|
90
|
+
# […]
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# @example Registering a custom-named inflection methods for the kinds gender and tense
|
94
|
+
# class ApplicationController < ActionController::Base
|
95
|
+
# inflection_method :get_user_gender => :gender, :get_tense => :tense
|
96
|
+
# […]
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# @example Registering inflection methods for the kinds gender and tense
|
100
|
+
# class ApplicationController < ActionController::Base
|
101
|
+
# inflection_method :gender, :tense
|
102
|
+
# […]
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# @example Registering inflection methods for the kind gender and the strict kind @tense
|
106
|
+
# class ApplicationController < ActionController::Base
|
107
|
+
# inflection_method :gender, :@tense
|
108
|
+
# […]
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# @example Registering a custom-named inflection methods for the kinds gender and @gender
|
112
|
+
# # in case of named patterns the method get_strict_gender and the
|
113
|
+
# # strict kind @gender will have priority; the regular kind gender
|
114
|
+
# # and its method would be used if there would be no strict variants
|
115
|
+
# class ApplicationController < ActionController::Base
|
116
|
+
# inflection_method :get_gender => :gender, :get_strict_gender => :@gender
|
117
|
+
# […]
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# @example Registering a method for the kind gender and the custom-named method for the kind @gender
|
121
|
+
# class ApplicationController < ActionController::Base
|
122
|
+
# inflection_method :gender, :get_strict_gender => :@gender
|
123
|
+
# […]
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# @example Registering a method for the kind gender visible in whole app and a variant for some controller
|
127
|
+
# class ApplicationController < ActionController::Base
|
128
|
+
# inflection_method :gender
|
129
|
+
# […]
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# # In this controller the method gender will be called
|
133
|
+
# # to obtain inflection option's value for the kind gender
|
134
|
+
# class UsersController < ApplicationController
|
135
|
+
# end
|
136
|
+
#
|
137
|
+
# # In this controller the method getit will be called
|
138
|
+
# # to obtain inflection option's value for the kind gender
|
139
|
+
# class OtherController < ApplicationController
|
140
|
+
# inflection_method :getit => :gender
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# # In this controller no method will be called
|
144
|
+
# # to obtain inflection option's value for the kind gender
|
145
|
+
# class FlowersController < ApplicationController
|
146
|
+
# no_inflection_method :getit
|
147
|
+
# end
|
148
|
+
#
|
55
149
|
# @api public
|
56
|
-
# @note Any added method will become helper
|
57
|
-
# @raise [I18n::Inflector::Rails::BadInflectionMethod] when name or value
|
58
|
-
# @param [Hash,Array
|
150
|
+
# @note Any added method will become a helper unless {I18n::Inflector::InflectionOptions#auto_helper} swtich is set to +false+!
|
151
|
+
# @raise [I18n::Inflector::Rails::BadInflectionMethod] when the given name or value are malformed
|
152
|
+
# @param [Hash{Symbol => Symbol},Array<Symbol>,Symbol,String] *args the methods and inflection kinds assigned to them
|
59
153
|
# @return [void]
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# @yieldparam [Symbol] kind the name of an inflection kind assigned to that +method+
|
65
|
-
# @yieldparam [Object] value the original result of calling the +method+ that will be assigned to a +kind+ as a token
|
66
|
-
# @yieldparam [Object] caller the object that made a call to {translate} method
|
67
|
-
# @yieldreturn [String] the new +value+ (token name) that will be assigned to a +kind+
|
68
|
-
def inflection_method(assignment, &block)
|
69
|
-
if assignment.is_a?(Array)
|
70
|
-
new_assignment = {}
|
71
|
-
assignment.flatten.each{|e| new_assignment[e]=new_assignment}
|
72
|
-
assignment = new_assignment
|
73
|
-
elsif (assignment.is_a?(String) || assignment.is_a?(Symbol))
|
74
|
-
assignment = { assignment => assignment }
|
154
|
+
def inflection_method(*args)
|
155
|
+
args = args.flatten
|
156
|
+
if args.empty?
|
157
|
+
raise I18n::Inflector::Rails::BadInflectionMethod.new(assignment)
|
75
158
|
end
|
76
159
|
|
77
|
-
|
78
|
-
|
160
|
+
assignment = {}
|
161
|
+
args.each do |e|
|
162
|
+
if (e.is_a?(Symbol) || e.is_a?(String))
|
163
|
+
assignment[e] = e
|
164
|
+
elsif e.is_a?(Hash)
|
165
|
+
raise I18n::Inflector::Rails::BadInflectionMethod.new(assignment) if e.empty?
|
166
|
+
assignment.merge!(e)
|
167
|
+
else
|
168
|
+
raise I18n::Inflector::Rails::BadInflectionMethod.new(assignment)
|
169
|
+
end
|
79
170
|
end
|
80
171
|
|
81
|
-
@
|
82
|
-
assignment.each_pair do |
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
raise I18n::Inflector::Rails::BadInflectionMethod.new("#{k.inspect} => #{v.inspect}")
|
172
|
+
@i18n_inflector_kinds ||= {}
|
173
|
+
assignment.each_pair do |method, kind|
|
174
|
+
method = method.to_s
|
175
|
+
if (method.empty? || I18n::Inflector::Config::Reserved::Kinds.invalid?(kind, :OPTION))
|
176
|
+
raise I18n::Inflector::Rails::BadInflectionMethod.new("#{method.inspect} => #{kind.inspect}")
|
87
177
|
end
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
178
|
+
kind = kind.to_s
|
179
|
+
method = method[1..-1] if (method[0..0] == I18n::Inflector::Config::Markers::PATTERN && method == kind)
|
180
|
+
kind = kind.to_sym
|
181
|
+
method = method.to_sym
|
182
|
+
helper_method(method) if I18n.backend.inflector.options.auto_helper
|
183
|
+
@i18n_inflector_kinds[kind] = method
|
93
184
|
end
|
94
185
|
end
|
95
186
|
alias_method :inflection_methods, :inflection_method
|
96
187
|
|
97
|
-
# This method
|
98
|
-
# created
|
188
|
+
# This method unregisters inflection kinds from assignments
|
189
|
+
# created by {inflection_method}. It is useful
|
99
190
|
# when there is a need to break inheritance in some controller,
|
100
|
-
# but a method
|
191
|
+
# but there was a method assigned to some inflection kind in
|
101
192
|
# a parrent class.
|
102
193
|
#
|
103
194
|
# @api public
|
104
|
-
# @raise [I18n::Inflector::Rails::BadInflectionMethod] when name or value is malformed
|
105
|
-
# @param [Array] names the method names
|
195
|
+
# @raise [I18n::Inflector::Rails::BadInflectionMethod] when name or value is bad or malformed
|
196
|
+
# @param [Array<Symbol>] names the method names for which the assigned kinds should be marked as not
|
197
|
+
# supported in a current controller and all derivative controllers
|
106
198
|
# @return [void]
|
107
199
|
def no_inflection_method(*names)
|
108
200
|
names = names.flatten
|
109
201
|
if (names.nil? || names.empty?)
|
110
202
|
raise I18n::Inflector::Rails::BadInflectionMethod.new(names)
|
111
203
|
end
|
112
|
-
@
|
204
|
+
@i18n_inflector_kinds ||= {}
|
113
205
|
names.each do |meth|
|
114
206
|
unless (meth.is_a?(Symbol) || meth.is_a?(String))
|
115
207
|
raise I18n::Inflector::Rails::BadInflectionMethod.new(meth)
|
116
208
|
end
|
117
209
|
meth = meth.to_s
|
210
|
+
meth = meth[1..-1] if meth[0..0] == I18n::Inflector::Config::Markers::PATTERN # for dummies
|
118
211
|
raise I18n::Inflector::Rails::BadInflectionMethod.new(meth) if meth.empty?
|
119
|
-
|
120
|
-
|
212
|
+
meth = meth.to_sym
|
213
|
+
i18n_inflector_kinds.each_pair do |kind, obj|
|
214
|
+
if obj == meth
|
215
|
+
@i18n_inflector_kinds[kind] = nil
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
121
219
|
end
|
122
220
|
alias_method :no_inflection_methods, :no_inflection_method
|
123
221
|
|
222
|
+
# This method unregisters the given inflection kinds from assignments
|
223
|
+
# created by {inflection_method}. It is useful
|
224
|
+
# when there is a need to break inheritance in some controller,
|
225
|
+
# but there was a method assigned to some inflection kind in
|
226
|
+
# a parrent class.
|
227
|
+
#
|
228
|
+
# @api public
|
229
|
+
# @raise [I18n::Inflector::Rails::BadInflectionMethod] when name or value is malformed
|
230
|
+
# @param [String,Symbol,Array<Symbol>] kinds the kind for which the method names should be marked
|
231
|
+
# as not supported in a current controller and all derivative controllers
|
232
|
+
# @return [void]
|
233
|
+
def no_inflection_method_for(*names)
|
234
|
+
names = names.flatten
|
235
|
+
if (names.nil? || names.empty?)
|
236
|
+
raise I18n::Inflector::Rails::BadInflectionMethod.new(names)
|
237
|
+
end
|
238
|
+
@i18n_inflector_kinds ||= {}
|
239
|
+
names.each do |kind|
|
240
|
+
unless (kind.is_a?(Symbol) || kind.is_a?(String))
|
241
|
+
raise I18n::Inflector::Rails::BadInflectionKind.new(kind)
|
242
|
+
end
|
243
|
+
if (I18n::Inflector::Config::Reserved::Kinds.invalid?(kind, :OPTION) ||
|
244
|
+
kind.to_s == I18n::Inflector::Config::Markers::PATTERN)
|
245
|
+
raise I18n::Inflector::Rails::BadInflectionKind.new(kind)
|
246
|
+
end
|
247
|
+
@i18n_inflector_kinds[kind.to_sym] = nil
|
248
|
+
end
|
249
|
+
end
|
250
|
+
alias_method :no_inflection_methods_for, :no_inflection_method_for
|
251
|
+
alias_method :no_inflection_kind, :no_inflection_method_for
|
252
|
+
|
124
253
|
end # class methods
|
125
|
-
|
126
|
-
# This module contains a
|
127
|
-
#
|
254
|
+
|
255
|
+
# This module contains a variant of the +translate+ method that
|
256
|
+
# uses {I18n::Inflector::Rails::ClassMethods#i18n_inflector_kinds i18n_inflector_kinds}
|
257
|
+
# available in the current context.
|
128
258
|
# The method from this module will wrap the
|
129
259
|
# {ActionView::Helpers::TranslationHelper#translate} method.
|
130
260
|
module InflectedTranslate
|
131
|
-
|
261
|
+
|
132
262
|
# This method tries to feed itself with the data coming
|
133
|
-
# from
|
134
|
-
#
|
135
|
-
#
|
136
|
-
# be passed to {I18n::
|
263
|
+
# from {I18n::Inflector::Rails::ClassMethods#i18n_inflector_kinds i18n_inflector_kinds}
|
264
|
+
# available in the current context.
|
265
|
+
# That data contains inflection pairs (<tt>kind => value</tt>) that will
|
266
|
+
# be passed to the interpolation method from {I18n::Inflector} through
|
137
267
|
# {ActionView::Helpers::TranslationHelper#translate}.
|
138
268
|
#
|
269
|
+
# You may also pass inflection options directly, along with other options,
|
270
|
+
# without registering methods responsible for delivering tokens.
|
271
|
+
# See {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector]
|
272
|
+
# for more info about inflection options.
|
273
|
+
#
|
139
274
|
# @api public
|
140
|
-
# @
|
141
|
-
# @
|
142
|
-
#
|
143
|
-
# @
|
144
|
-
#
|
275
|
+
# @raise {I18n::InvalidInflectionKind}
|
276
|
+
# @raise {I18n::InvalidInflectionOption}
|
277
|
+
# @raise {I18n::InvalidInflectionToken}
|
278
|
+
# @raise {I18n::MisplacedInflectionToken}
|
279
|
+
# @overload translate(key, options)
|
280
|
+
# @param [String] key translation key
|
281
|
+
# @param [Hash] options a set of options to pass to the
|
282
|
+
# translation routines
|
283
|
+
# @option options [Boolean] :inflector_verify_methods (false) local switch
|
284
|
+
# that overrides global setting (see {I18n::Inflector::InflectionOptions#verify_methods})
|
285
|
+
# @option options [Boolean] :inflector_lazy_methods (true) local switch
|
286
|
+
# that overrides global setting (see {I18n::Inflector::InflectionOptions#lazy_methods})
|
287
|
+
# @option options [Boolean] :inflector_excluded_defaults (false) local switch
|
288
|
+
# that overrides global setting (see {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector])
|
289
|
+
# @option options [Boolean] :inflector_unknown_defaults (true) local switch
|
290
|
+
# that overrides global setting (see {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector])
|
291
|
+
# @option options [Boolean] :inflector_raises (false) local switch
|
292
|
+
# that overrides global setting (see {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector])
|
293
|
+
# @option options [Boolean] :inflector_aliased_patterns (false) local switch
|
294
|
+
# that overrides global setting (see {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector])
|
295
|
+
# @option options [Boolean] :inflector_cache_aware (false) local switch
|
296
|
+
# that overrides global setting (see {I18n Inflector documentation}[http://rubydoc.info/gems/i18n-inflector])
|
297
|
+
# @return [String] the translated string with inflection patterns
|
298
|
+
# interpolated
|
145
299
|
def translate(*args)
|
146
300
|
opts_present = args.last.is_a?(Hash)
|
147
301
|
if opts_present
|
@@ -153,16 +307,12 @@ module I18n
|
|
153
307
|
test_locale ||= I18n.locale
|
154
308
|
inflector = I18n.backend.inflector
|
155
309
|
|
156
|
-
#
|
310
|
+
# return immediately if the locale is not supported
|
157
311
|
return super unless inflector.inflected_locale?(test_locale)
|
158
312
|
|
159
|
-
# read switch
|
160
|
-
verifies = options.delete(:inflector_verify_methods)
|
161
|
-
verifies = verifies.nil? ? inflector.options.verify_methods : verifies
|
162
|
-
|
163
313
|
# collect inflection variables that are present in this context
|
164
|
-
subopts = t_prepare_inflection_options(inflector,
|
165
|
-
|
314
|
+
subopts = t_prepare_inflection_options(inflector, locale, options)
|
315
|
+
|
166
316
|
# jump to original translate if no variables are present
|
167
317
|
return super if subopts.empty?
|
168
318
|
|
@@ -173,29 +323,34 @@ module I18n
|
|
173
323
|
end
|
174
324
|
|
175
325
|
alias_method :t, :translate
|
176
|
-
|
326
|
+
|
177
327
|
protected
|
178
|
-
|
179
|
-
# This method tries to read +
|
328
|
+
|
329
|
+
# This method tries to read +i18n_inflector_kinds+ available in the current context.
|
180
330
|
#
|
181
331
|
# @return [Hash] the inflection options (<tt>kind => value</tt>)
|
182
|
-
def t_prepare_inflection_options(inflector,
|
332
|
+
def t_prepare_inflection_options(inflector, locale, options)
|
183
333
|
subopts = {}
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
334
|
+
|
335
|
+
verifies = options[:inflector_verify_methods]
|
336
|
+
verifies = inflector.options.verify_methods if verifies.nil?
|
337
|
+
is_lazy = options[:inflector_lazy_methods]
|
338
|
+
is_lazy = inflector.options.lazy_methods if is_lazy.nil?
|
339
|
+
|
340
|
+
return subopts if (verifies && !respond_to?(:i18n_inflector_kinds))
|
341
|
+
|
342
|
+
i18n_inflector_kinds.each_pair do |kind, meth|
|
343
|
+
next if meth.nil? # kind is registered but disabled from usage
|
344
|
+
next if verifies && !respond_to?(meth)
|
345
|
+
obj = method(meth)
|
346
|
+
obj = obj.call { next kind, locale } unless is_lazy # lazy_methods is disabled
|
347
|
+
subopts[kind] = obj
|
193
348
|
end
|
194
349
|
return subopts
|
195
350
|
end
|
196
|
-
|
197
|
-
end #
|
198
|
-
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
351
|
+
|
352
|
+
end # module InflectedTranslate
|
353
|
+
|
354
|
+
end # module Rails
|
355
|
+
end # module Inflector
|
356
|
+
end # module I18n
|
@@ -10,43 +10,95 @@
|
|
10
10
|
|
11
11
|
module I18n
|
12
12
|
module Inflector
|
13
|
-
module Rails
|
14
|
-
|
15
|
-
# This module adds options to {I18n::Inflector::InflectionOptions}
|
16
|
-
module AdditionalOptions
|
17
|
-
|
18
|
-
attr_writer :verify_methods
|
19
|
-
|
20
|
-
# When this is set to +true+ then
|
21
|
-
# inflection works a bit slower but
|
22
|
-
# checks whether any method exists before
|
23
|
-
# calling it. This switch is by default set
|
24
|
-
# to +false+.
|
25
|
-
#
|
26
|
-
# By turning this switch on you're sure that
|
27
|
-
# there will be no +NameError+ (no method) exception
|
28
|
-
# raised during translation.
|
29
|
-
#
|
30
|
-
# Alternatively you can turn this locally,
|
31
|
-
# for the specified translate call, by setting
|
32
|
-
# <tt>:inflector_verify_methods</tt> option to +true+.
|
33
|
-
#
|
34
|
-
# @example Globally enabling methods verification
|
35
|
-
# I18n.inflector.options.verify_methods = true
|
36
|
-
# @example Locally enabling methods verification
|
37
|
-
# translate('welcome', :inflector_verify_methods => true)
|
38
|
-
def verify_methods
|
39
|
-
@verify_methods || false
|
40
|
-
end
|
41
|
-
|
42
|
-
# This method resets inflector's
|
43
|
-
# switches to default values.
|
44
|
-
def reset
|
45
|
-
@verify_methods = false
|
46
|
-
super
|
47
|
-
end
|
48
13
|
|
14
|
+
# This module adds options to {I18n::Inflector::InflectionOptions}
|
15
|
+
class InflectionOptions
|
16
|
+
|
17
|
+
# When this is set to +true+ then
|
18
|
+
# inflection works a bit slower but
|
19
|
+
# checks whether any method exists before
|
20
|
+
# calling it.
|
21
|
+
#
|
22
|
+
# This switch is by default set
|
23
|
+
# to +false+.
|
24
|
+
#
|
25
|
+
# By turning this switch on you're sure that
|
26
|
+
# there will be no +NameError+ (no method) exception
|
27
|
+
# raised during translation.
|
28
|
+
#
|
29
|
+
# Alternatively you can turn this on locally,
|
30
|
+
# for the specified translation call, by setting
|
31
|
+
# <tt>:inflector_verify_methods</tt> option to +true+.
|
32
|
+
#
|
33
|
+
# @example Globally enabling methods verification
|
34
|
+
# I18n.inflector.options.verify_methods = true
|
35
|
+
# @example Locally enabling methods verification
|
36
|
+
# translate('welcome', :inflector_verify_methods => true)
|
37
|
+
attr_accessor :verify_methods
|
38
|
+
|
39
|
+
# When this is set to +true+ then
|
40
|
+
# each time infleciton method is registered
|
41
|
+
# it becomes a helper method so it's available in
|
42
|
+
# a view.
|
43
|
+
#
|
44
|
+
# This switch is by default set
|
45
|
+
# to +true+.
|
46
|
+
#
|
47
|
+
# Setting this switch locally,
|
48
|
+
# for the specified translation call, using
|
49
|
+
# <tt>:inflector_auto_helper</tt> option will
|
50
|
+
# have no effect.
|
51
|
+
#
|
52
|
+
# @example Globally disabling automatic setting of helpers
|
53
|
+
# I18n.inflector.options.auto_helper = false
|
54
|
+
attr_accessor :auto_helper
|
55
|
+
|
56
|
+
# When this is set to +true+ then
|
57
|
+
# each time an infleciton method is used to obtain
|
58
|
+
# value for the associated kind it evaluates lazy.
|
59
|
+
# That means the method object is passed to the
|
60
|
+
# translation routines and it is evaluated when there
|
61
|
+
# is a need. If this is set to +false+ then
|
62
|
+
# evaluation takes place before calling Inflector's
|
63
|
+
# translation method and inflection options are passed
|
64
|
+
# as symbols, not as method objects.
|
65
|
+
#
|
66
|
+
# This switch is by default set
|
67
|
+
# to +true+. By disabling it you may experience
|
68
|
+
# some negative performance impact when many
|
69
|
+
# inflection methods are registered. That is because
|
70
|
+
# the lazy evaluation causes calling only those methods
|
71
|
+
# that are needed by internal interpolation routines
|
72
|
+
# of the Inflector. For instance, if in some pattern
|
73
|
+
# only the kind "gender" is used then there is no
|
74
|
+
# need to call inflection methods for other kinds.
|
75
|
+
# When lazy evaluation is disabled then all inflection
|
76
|
+
# methods have to be called before passing control
|
77
|
+
# to translation routines, since this plug-in does not
|
78
|
+
# analyze contents of inflection patterns or keys.
|
79
|
+
#
|
80
|
+
# Alternatively you can turn this off locally,
|
81
|
+
# for the specified translation call, by setting
|
82
|
+
# <tt>:inflector_lazy_methods</tt> option to +false+.
|
83
|
+
#
|
84
|
+
# @example Globally disabling lazy evaluation of kinds
|
85
|
+
# I18n.inflector.options.lazy_methods = false
|
86
|
+
# @example Locally disabling lazy evaluation of kinds
|
87
|
+
# translate('welcome', :inflector_lazy_methods => false)
|
88
|
+
attr_accessor :lazy_methods
|
89
|
+
|
90
|
+
# @private
|
91
|
+
alias_method :_rai_orig_reset, :reset
|
92
|
+
|
93
|
+
# This method resets inflector's
|
94
|
+
# switches to default values.
|
95
|
+
def reset
|
96
|
+
@verify_methods = false
|
97
|
+
@auto_helper = true
|
98
|
+
@lazy_methods = true
|
99
|
+
_rai_orig_reset
|
49
100
|
end
|
50
|
-
|
51
|
-
|
52
|
-
end
|
101
|
+
|
102
|
+
end # class InflectionOptions
|
103
|
+
end # module Inflector
|
104
|
+
end # module I18n
|
data/spec/inflector_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
class ApplicationController < ActionController::Base; end
|
4
4
|
class InflectedTranslateController < ApplicationController; end
|
5
|
+
class InflectedStrictController < ApplicationController; end
|
5
6
|
|
6
7
|
describe I18n.inflector.options.class do
|
7
8
|
|
@@ -25,28 +26,47 @@ describe ApplicationController do
|
|
25
26
|
|
26
27
|
I18n.locale = :xx
|
27
28
|
I18n.backend.store_translations(:xx, :i18n => { :inflections => {
|
28
|
-
|
29
|
+
:gender => {
|
30
|
+
:m => 'male',
|
31
|
+
:f => 'female',
|
32
|
+
:n => 'neuter',
|
33
|
+
:s => 'strange',
|
34
|
+
:masculine => '@m',
|
35
|
+
:feminine => '@f',
|
36
|
+
:neuter => '@n',
|
37
|
+
:neutral => '@neuter',
|
38
|
+
:default => 'neutral' },
|
39
|
+
:time => {
|
40
|
+
:present => 'present',
|
41
|
+
:past => 'past',
|
42
|
+
:future => 'future'},
|
43
|
+
:@gender => {
|
29
44
|
:m => 'male',
|
30
45
|
:f => 'female',
|
46
|
+
:d => 'dude',
|
47
|
+
:x => 'tester',
|
31
48
|
:n => 'neuter',
|
32
|
-
:
|
33
|
-
:masculine => '@m',
|
34
|
-
:feminine => '@f',
|
35
|
-
:neuter => '@n',
|
36
|
-
:neutral => '@neuter',
|
37
|
-
:default => 'neutral' },
|
49
|
+
:default => 'n'},
|
38
50
|
:person => {
|
39
51
|
:i => 'I',
|
40
52
|
:you => 'You',
|
41
|
-
:it => 'It'}
|
53
|
+
:it => 'It'},
|
54
|
+
:@person => {
|
55
|
+
:i => 'I',
|
56
|
+
:you => 'You',
|
57
|
+
:it => 'It'},
|
42
58
|
} })
|
43
|
-
|
44
|
-
I18n.backend.store_translations(:xx, '
|
59
|
+
|
60
|
+
I18n.backend.store_translations(:xx, 'welcome' => 'Dear @{f:Lady|m:Sir|n:You|All}!')
|
61
|
+
I18n.backend.store_translations(:xx, 'welcome_strict' => 'Dear @gender{f:Lady|m:Sir|d:Dude|n:You|All}!')
|
62
|
+
I18n.backend.store_translations(:xx, 'to_be' => 'Oh @{i:I am|you:You are|it:It is}')
|
63
|
+
I18n.backend.store_translations(:xx, 'to_be_strict' => 'Oh @person{i:me|you:You are|it:It is}')
|
64
|
+
I18n.backend.store_translations(:xx, 'hitime' => '@{present,past,future:~}!')
|
45
65
|
|
46
66
|
end
|
47
67
|
|
48
68
|
describe ".inflection_method" do
|
49
|
-
|
69
|
+
|
50
70
|
before do
|
51
71
|
class AnotherController < InflectedTranslateController; end
|
52
72
|
end
|
@@ -54,28 +74,33 @@ describe ApplicationController do
|
|
54
74
|
it "should be albe to assign a mehtod to the inflection kind" do
|
55
75
|
lambda{AnotherController.inflection_method(:users_gender => :gender)}.should_not raise_error
|
56
76
|
end
|
57
|
-
|
77
|
+
|
78
|
+
it "should be albe to assign a mehtod to the strict inflection kind" do
|
79
|
+
lambda{AnotherController.inflection_method(:users_gender => :@gender)}.should_not raise_error
|
80
|
+
end
|
81
|
+
|
58
82
|
it "should be albe to accept single Symbol argument" do
|
59
83
|
lambda{AnotherController.inflection_method(:time)}.should_not raise_error
|
84
|
+
lambda{AnotherController.inflection_method(:@time)}.should_not raise_error
|
60
85
|
end
|
61
86
|
|
62
87
|
it "should be albe to accept single String argument" do
|
63
88
|
lambda{AnotherController.inflection_method('time')}.should_not raise_error
|
89
|
+
lambda{AnotherController.inflection_method('@time')}.should_not raise_error
|
64
90
|
end
|
65
91
|
|
66
92
|
it "should be albe to accept Array<Symbol> argument" do
|
67
93
|
lambda{AnotherController.inflection_method([:time])}.should_not raise_error
|
68
|
-
|
69
|
-
|
70
|
-
it "should be albe to assign a mehtod to the inflection kind with proc" do
|
71
|
-
lambda{AnotherController.inflection_method(:users_gender => :gender){|a,b,c,d,e| :m} }.should_not raise_error
|
72
|
-
lambda{AnotherController.inflection_method(:time){|a,b,c,d,e| :m} }.should_not raise_error
|
94
|
+
lambda{AnotherController.inflection_method([:@time])}.should_not raise_error
|
73
95
|
end
|
74
96
|
|
75
97
|
it "should raise an error when method name is wrong" do
|
76
98
|
lambda{AnotherController.inflection_method}.should raise_error
|
77
99
|
lambda{AnotherController.inflection_method(nil => :blabla)}.should raise_error
|
78
100
|
lambda{AnotherController.inflection_method(:blabla => nil)}.should raise_error
|
101
|
+
lambda{AnotherController.inflection_method(nil => :@blabla)}.should raise_error
|
102
|
+
lambda{AnotherController.inflection_method(:@blabla => nil)}.should raise_error
|
103
|
+
lambda{AnotherController.inflection_method(:"@")}.should raise_error
|
79
104
|
lambda{AnotherController.inflection_method({''=>''})}.should raise_error
|
80
105
|
lambda{AnotherController.inflection_method(nil => nil)}.should raise_error
|
81
106
|
lambda{AnotherController.inflection_method(nil)}.should raise_error
|
@@ -88,99 +113,155 @@ describe ApplicationController do
|
|
88
113
|
end
|
89
114
|
|
90
115
|
describe ".no_inflection_method" do
|
91
|
-
|
116
|
+
|
92
117
|
before do
|
93
118
|
class AnotherController < InflectedTranslateController; end
|
94
119
|
end
|
95
120
|
|
96
|
-
it "should be albe to
|
121
|
+
it "should be albe to split a mehtod of the inflection kind" do
|
97
122
|
lambda{AnotherController.no_inflection_method(:users_gender)}.should_not raise_error
|
98
123
|
end
|
99
|
-
|
124
|
+
|
100
125
|
it "should be albe to accept single Symbol argument" do
|
101
126
|
lambda{AnotherController.no_inflection_method(:time)}.should_not raise_error
|
127
|
+
lambda{AnotherController.no_inflection_method(:@time)}.should_not raise_error
|
102
128
|
end
|
103
129
|
|
104
130
|
it "should be albe to accept single String argument" do
|
105
131
|
lambda{AnotherController.no_inflection_method('time')}.should_not raise_error
|
132
|
+
lambda{AnotherController.no_inflection_method('@time')}.should_not raise_error
|
106
133
|
end
|
107
134
|
|
108
135
|
it "should be albe to accept Array<Symbol> argument" do
|
109
136
|
lambda{AnotherController.no_inflection_method([:time])}.should_not raise_error
|
137
|
+
lambda{AnotherController.no_inflection_method([:@time])}.should_not raise_error
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should raise an error when method name is wrong" do
|
141
|
+
lambda{AnotherController.no_inflection_method}.should raise_error
|
142
|
+
lambda{AnotherController.no_inflection_method(nil)}.should raise_error
|
143
|
+
lambda{AnotherController.no_inflection_method([nil])}.should raise_error
|
144
|
+
lambda{AnotherController.no_inflection_method(:"@")}.should raise_error
|
145
|
+
lambda{AnotherController.no_inflection_method([''])}.should raise_error
|
146
|
+
lambda{AnotherController.no_inflection_method([])}.should raise_error
|
147
|
+
lambda{AnotherController.no_inflection_method({})}.should raise_error
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
describe ".no_inflection_kind" do
|
153
|
+
|
154
|
+
before do
|
155
|
+
class AnotherController < InflectedTranslateController; end
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should be albe to spit a mehtod of the inflection kind" do
|
159
|
+
lambda{AnotherController.no_inflection_kind(:gender)}.should_not raise_error
|
160
|
+
lambda{AnotherController.no_inflection_kind(:@gender)}.should_not raise_error
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should be albe to accept single Symbol argument" do
|
164
|
+
lambda{AnotherController.no_inflection_kind(:time)}.should_not raise_error
|
165
|
+
lambda{AnotherController.no_inflection_kind(:@time)}.should_not raise_error
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should be albe to accept single String argument" do
|
169
|
+
lambda{AnotherController.no_inflection_kind('time')}.should_not raise_error
|
170
|
+
lambda{AnotherController.no_inflection_kind('@time')}.should_not raise_error
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should be albe to accept Array<Symbol> argument" do
|
174
|
+
lambda{AnotherController.no_inflection_kind([:time])}.should_not raise_error
|
175
|
+
lambda{AnotherController.no_inflection_kind([:@time])}.should_not raise_error
|
110
176
|
end
|
111
177
|
|
112
178
|
it "should raise an error when method name is wrong" do
|
113
|
-
lambda{AnotherController.
|
114
|
-
lambda{AnotherController.
|
115
|
-
lambda{AnotherController.
|
116
|
-
lambda{AnotherController.
|
117
|
-
lambda{AnotherController.
|
118
|
-
lambda{AnotherController.
|
179
|
+
lambda{AnotherController.no_inflection_kind}.should raise_error
|
180
|
+
lambda{AnotherController.no_inflection_kind(nil)}.should raise_error
|
181
|
+
lambda{AnotherController.no_inflection_kind(:"@")}.should raise_error
|
182
|
+
lambda{AnotherController.no_inflection_kind([nil])}.should raise_error
|
183
|
+
lambda{AnotherController.no_inflection_kind([''])}.should raise_error
|
184
|
+
lambda{AnotherController.no_inflection_kind([])}.should raise_error
|
185
|
+
lambda{AnotherController.no_inflection_kind({})}.should raise_error
|
119
186
|
end
|
120
187
|
|
121
188
|
end
|
122
189
|
|
123
|
-
describe ".
|
190
|
+
describe ".i18n_inflector_kinds" do
|
124
191
|
|
125
192
|
before do
|
126
193
|
InflectedTranslateController.inflection_method(:users_gender => :gender)
|
127
194
|
InflectedTranslateController.inflection_method(:time)
|
128
|
-
@expected_hash =
|
129
|
-
:time=>{:kind=>:time, :proc=>nil}}
|
195
|
+
@expected_hash = { :gender => :users_gender, :time => :time }
|
130
196
|
end
|
131
197
|
|
132
198
|
it "should be callable" do
|
133
|
-
lambda{InflectedTranslateController.
|
199
|
+
lambda{InflectedTranslateController.i18n_inflector_kinds}.should_not raise_error
|
134
200
|
end
|
135
201
|
|
136
202
|
it "should be able to read methods assigned to inflection kinds" do
|
137
|
-
InflectedTranslateController.
|
203
|
+
InflectedTranslateController.i18n_inflector_kinds.should == @expected_hash
|
138
204
|
end
|
139
205
|
|
140
206
|
end
|
141
|
-
|
207
|
+
|
142
208
|
describe "controller instance methods" do
|
143
|
-
|
209
|
+
|
144
210
|
before do
|
145
211
|
|
146
212
|
class InflectedTranslateController
|
147
|
-
|
148
|
-
def
|
149
|
-
def
|
150
|
-
def
|
151
|
-
|
213
|
+
def trn(*args); translate(*args) end
|
214
|
+
def t_male; t('welcome') end
|
215
|
+
def users_gender; :m end
|
216
|
+
def time
|
217
|
+
kind, locale = yield
|
218
|
+
kind == :time ? :past : nil
|
219
|
+
end
|
152
220
|
end
|
153
221
|
|
154
|
-
class
|
155
|
-
inflection_method
|
156
|
-
|
157
|
-
def
|
222
|
+
class InflectedStrictController
|
223
|
+
inflection_method :@gender
|
224
|
+
|
225
|
+
def gender; :m end
|
226
|
+
def trn(*args); translate(*args) end
|
158
227
|
end
|
159
228
|
|
160
|
-
class
|
161
|
-
inflection_method :
|
229
|
+
class InflectedStrictOverrideController < InflectedTranslateController
|
230
|
+
inflection_method :users_dude => :@gender
|
231
|
+
inflection_method :users_female => :gender
|
232
|
+
inflection_method :person_i => :person
|
233
|
+
|
234
|
+
no_inflection_method_for :@person
|
235
|
+
|
236
|
+
def users_female; :f end
|
237
|
+
def users_dude; :d end
|
238
|
+
def person_i; :i end
|
162
239
|
end
|
163
240
|
|
164
241
|
class NomethodController < InflectedTranslateController
|
165
242
|
inflection_method :nonexistent => :gender
|
166
|
-
|
167
|
-
|
243
|
+
end
|
244
|
+
|
245
|
+
class MethodDisabledController < InflectedTranslateController
|
246
|
+
no_inflection_method :users_gender
|
168
247
|
end
|
169
248
|
|
170
249
|
@controller = InflectedTranslateController.new
|
171
|
-
@
|
172
|
-
@
|
250
|
+
@strict_controller = InflectedStrictController.new
|
251
|
+
@strict_over_controller = InflectedStrictOverrideController.new
|
252
|
+
@disabled_controller = MethodDisabledController.new
|
173
253
|
@nomethod_controller = NomethodController.new
|
174
254
|
|
175
|
-
@expected_hash = {:users_gender=>{:kind=>:gender, :proc=>nil},
|
176
|
-
:time=>{:kind=>:time, :proc=>nil}}
|
177
|
-
|
178
255
|
end
|
179
256
|
|
180
|
-
describe "#
|
257
|
+
describe "#i18n_inflector_kinds" do
|
258
|
+
|
259
|
+
before do
|
260
|
+
@expected_hash = {:gender => :users_gender, :time => :time }
|
261
|
+
end
|
181
262
|
|
182
263
|
it "should be able to read methods assigned to inflection kinds" do
|
183
|
-
@controller.
|
264
|
+
@controller.i18n_inflector_kinds.should == @expected_hash
|
184
265
|
end
|
185
266
|
|
186
267
|
end
|
@@ -188,25 +269,35 @@ describe ApplicationController do
|
|
188
269
|
describe "#translate" do
|
189
270
|
|
190
271
|
it "should translate using inflection patterns and pick up the right value" do
|
191
|
-
@controller.
|
272
|
+
@controller.trn('welcome').should == 'Dear Sir!'
|
273
|
+
@controller.trn('welcome_strict').should == 'Dear Sir!'
|
274
|
+
@strict_controller.trn('welcome_strict').should == 'Dear Sir!'
|
192
275
|
end
|
193
276
|
|
194
|
-
it "should make use of
|
195
|
-
@
|
277
|
+
it "should make use of a block passed to inflection method" do
|
278
|
+
@controller.trn('hitime').should == 'past!'
|
196
279
|
end
|
197
280
|
|
198
281
|
it "should make use of inherited inflection method assignments" do
|
199
|
-
@
|
282
|
+
@strict_over_controller.trn('hitime').should == 'past!'
|
200
283
|
end
|
201
284
|
|
202
285
|
it "should make use of overriden inflection method assignments" do
|
203
|
-
@
|
286
|
+
@strict_over_controller.trn('welcome').should == 'Dear Lady!'
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should prioritize strict kinds when both inflection options are passed" do
|
290
|
+
@strict_over_controller.trn('welcome_strict').should == 'Dear Dude!'
|
291
|
+
@strict_over_controller.trn('welcome').should == 'Dear Lady!'
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should use regular kind option when strict kind option is missing" do
|
295
|
+
@strict_over_controller.trn('to_be').should == 'Oh I am'
|
296
|
+
@strict_over_controller.trn('to_be_strict').should == 'Oh me'
|
204
297
|
end
|
205
|
-
|
298
|
+
|
206
299
|
it "should make use of disabled inflection method assignments" do
|
207
|
-
|
208
|
-
@personprim_controller.translated_person.should == 'Oh '
|
209
|
-
@person_controller.translated_person.should == 'Oh It is'
|
300
|
+
@disabled_controller.trn('welcome').should == 'Dear You!'
|
210
301
|
end
|
211
302
|
|
212
303
|
it "should raise exception when method does not exists" do
|
@@ -214,19 +305,23 @@ describe ApplicationController do
|
|
214
305
|
end
|
215
306
|
|
216
307
|
it "should not raise when method does not exists and verify_methods is enabled" do
|
217
|
-
lambda{@nomethod_controller.
|
308
|
+
lambda{@nomethod_controller.trn('welcome', :inflector_verify_methods => true)}.should_not raise_error(NameError)
|
218
309
|
I18n.inflector.options.verify_methods = true
|
219
|
-
lambda{@nomethod_controller.
|
310
|
+
lambda{@nomethod_controller.trn('welcome')}.should_not raise_error(NameError)
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should translate with the :inflector_lazy_methods switch turned off" do
|
314
|
+
@strict_over_controller.trn('welcome', :inflector_lazy_methods => false).should == 'Dear Lady!'
|
220
315
|
end
|
221
316
|
|
222
317
|
end
|
223
|
-
|
318
|
+
|
224
319
|
describe "#t" do
|
225
|
-
|
320
|
+
|
226
321
|
it "should call translate" do
|
227
322
|
@controller.t_male.should == 'Dear Sir!'
|
228
323
|
end
|
229
|
-
|
324
|
+
|
230
325
|
end
|
231
326
|
|
232
327
|
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: i18n-inflector-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
|
-
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Pawe\xC5\x82 Wilk"
|
@@ -34,7 +34,7 @@ cert_chain:
|
|
34
34
|
NK3TIZaPCh1S2/ES6wXNvjQ+5EnEEL9j/pSEop9DYEBPaM2WDVR5i0jJTAaRWw==
|
35
35
|
-----END CERTIFICATE-----
|
36
36
|
|
37
|
-
date: 2011-
|
37
|
+
date: 2011-02-15 00:00:00 +01:00
|
38
38
|
default_executable:
|
39
39
|
dependencies:
|
40
40
|
- !ruby/object:Gem::Dependency
|
@@ -46,8 +46,8 @@ dependencies:
|
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
segments:
|
48
48
|
- 2
|
49
|
-
-
|
50
|
-
version: "2.
|
49
|
+
- 3
|
50
|
+
version: "2.3"
|
51
51
|
type: :runtime
|
52
52
|
prerelease: false
|
53
53
|
version_requirements: *id001
|
@@ -219,7 +219,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
219
219
|
requirements:
|
220
220
|
- - ">="
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
hash:
|
222
|
+
hash: 2455851808387903881
|
223
223
|
segments:
|
224
224
|
- 0
|
225
225
|
version: "0"
|
@@ -228,7 +228,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
228
228
|
requirements:
|
229
229
|
- - ">="
|
230
230
|
- !ruby/object:Gem::Version
|
231
|
-
hash: 2397947838501194867
|
232
231
|
segments:
|
233
232
|
- 0
|
234
233
|
version: "0"
|
metadata.gz.sig
CHANGED
Binary file
|