##// END OF EJS Templates
removed translation of error messages inside ActiveRecord module...
Jean-Philippe Lang -
r219:4a988b0f9223
parent child
Show More
@@ -1,230 +1,230
1 1 # Copyright (c) 2005-2006 David Barri
2 2
3 3 require 'gloc'
4 4
5 5 module ActionController #:nodoc:
6 6 class Base #:nodoc:
7 7 include GLoc
8 8 end
9 9 module Filters #:nodoc:
10 10 module ClassMethods
11 11
12 12 # This filter attempts to auto-detect the clients desired language.
13 13 # It first checks the params, then a cookie and then the HTTP_ACCEPT_LANGUAGE
14 14 # request header. If a language is found to match or be similar to a currently
15 15 # valid language, then it sets the current_language of the controller.
16 16 #
17 17 # class ExampleController < ApplicationController
18 18 # set_language :en
19 19 # autodetect_language_filter :except => 'monkey', :on_no_lang => :lang_not_autodetected_callback
20 20 # autodetect_language_filter :only => 'monkey', :check_cookie => 'monkey_lang', :check_accept_header => false
21 21 # ...
22 22 # def lang_not_autodetected_callback
23 23 # redirect_to somewhere
24 24 # end
25 25 # end
26 26 #
27 27 # The <tt>args</tt> for this filter are exactly the same the arguments of
28 28 # <tt>before_filter</tt> with the following exceptions:
29 29 # * <tt>:check_params</tt> -- If false, then params will not be checked for a language.
30 30 # If a String, then this will value will be used as the name of the param.
31 31 # * <tt>:check_cookie</tt> -- If false, then the cookie will not be checked for a language.
32 32 # If a String, then this will value will be used as the name of the cookie.
33 33 # * <tt>:check_accept_header</tt> -- If false, then HTTP_ACCEPT_LANGUAGE will not be checked for a language.
34 34 # * <tt>:on_set_lang</tt> -- You can specify the name of a callback function to be called when the language
35 35 # is successfully detected and set. The param must be a Symbol or a String which is the name of the function.
36 36 # The callback function must accept one argument (the language) and must be instance level.
37 37 # * <tt>:on_no_lang</tt> -- You can specify the name of a callback function to be called when the language
38 38 # couldn't be detected automatically. The param must be a Symbol or a String which is the name of the function.
39 39 # The callback function must be instance level.
40 40 #
41 41 # You override the default names of the param or cookie by calling <tt>GLoc.set_config :default_param_name => 'new_param_name'</tt>
42 42 # and <tt>GLoc.set_config :default_cookie_name => 'new_cookie_name'</tt>.
43 43 def autodetect_language_filter(*args)
44 44 options= args.last.is_a?(Hash) ? args.last : {}
45 45 x= 'Proc.new { |c| l= nil;'
46 46 # :check_params
47 47 unless (v= options.delete(:check_params)) == false
48 48 name= v ? ":#{v}" : 'GLoc.get_config(:default_param_name)'
49 49 x << "l ||= GLoc.similar_language(c.params[#{name}]);"
50 50 end
51 51 # :check_cookie
52 52 unless (v= options.delete(:check_cookie)) == false
53 53 name= v ? ":#{v}" : 'GLoc.get_config(:default_cookie_name)'
54 54 x << "l ||= GLoc.similar_language(c.send(:cookies)[#{name}]);"
55 55 end
56 56 # :check_accept_header
57 57 unless options.delete(:check_accept_header) == false
58 58 x << %<
59 59 unless l
60 60 a= c.request.env['HTTP_ACCEPT_LANGUAGE'].split(/,|;/) rescue nil
61 61 a.each {|x| l ||= GLoc.similar_language(x)} if a
62 62 end; >
63 63 end
64 64 # Set language
65 65 x << 'ret= true;'
66 66 x << 'if l; c.set_language(l); c.headers[\'Content-Language\']= l.to_s; '
67 67 if options.has_key?(:on_set_lang)
68 68 x << "ret= c.#{options.delete(:on_set_lang)}(l);"
69 69 end
70 70 if options.has_key?(:on_no_lang)
71 71 x << "else; ret= c.#{options.delete(:on_no_lang)};"
72 72 end
73 73 x << 'end; ret }'
74 74
75 75 # Create filter
76 76 block= eval x
77 77 before_filter(*args, &block)
78 78 end
79 79
80 80 end
81 81 end
82 82 end
83 83
84 84 # ==============================================================================
85 85
86 86 module ActionMailer #:nodoc:
87 87 # In addition to including GLoc, <tt>render_message</tt> is also overridden so
88 88 # that mail templates contain the current language at the end of the file.
89 89 # Eg. <tt>deliver_hello</tt> will render <tt>hello_en.rhtml</tt>.
90 90 class Base
91 91 include GLoc
92 92 private
93 93 alias :render_message_without_gloc :render_message
94 94 def render_message(method_name, body)
95 95 render_message_without_gloc("#{method_name}_#{current_language}", body)
96 96 end
97 97 end
98 98 end
99 99
100 100 # ==============================================================================
101 101
102 102 module ActionView #:nodoc:
103 103 # <tt>initialize</tt> is overridden so that new instances of this class inherit
104 104 # the current language of the controller.
105 105 class Base
106 106 include GLoc
107 107
108 108 alias :initialize_without_gloc :initialize
109 109 def initialize(base_path = nil, assigns_for_first_render = {}, controller = nil)
110 110 initialize_without_gloc(base_path, assigns_for_first_render, controller)
111 111 set_language controller.current_language unless controller.nil?
112 112 end
113 113 end
114 114
115 115 module Helpers #:nodoc:
116 116 class InstanceTag
117 117 include GLoc
118 118 # Inherits the current language from the template object.
119 119 def current_language
120 120 @template_object.current_language
121 121 end
122 122 end
123 123 end
124 124 end
125 125
126 126 # ==============================================================================
127 127
128 128 module ActiveRecord #:nodoc:
129 129 class Base #:nodoc:
130 130 include GLoc
131 131 end
132 132
133 class Errors
134 include GLoc
135 alias :add_without_gloc :add
136 # The GLoc version of this method provides two extra features
137 # * If <tt>msg</tt> is a string, it will be considered a GLoc string key.
138 # * If <tt>msg</tt> is an array, the first element will be considered
139 # the string and the remaining elements will be considered arguments for the
140 # string. Eg. <tt>['Hi %s.','John']</tt>
141 def add(attribute, msg= @@default_error_messages[:invalid])
142 if msg.is_a?(Array)
143 args= msg.clone
144 msg= args.shift
145 args= nil if args.empty?
146 end
147 msg= ltry(msg)
148 msg= msg % args unless args.nil?
149 add_without_gloc(attribute, msg)
150 end
151 # Inherits the current language from the base record.
152 def current_language
153 @base.current_language
154 end
155 end
133 # class Errors
134 # include GLoc
135 # alias :add_without_gloc :add
136 # # The GLoc version of this method provides two extra features
137 # # * If <tt>msg</tt> is a string, it will be considered a GLoc string key.
138 # # * If <tt>msg</tt> is an array, the first element will be considered
139 # # the string and the remaining elements will be considered arguments for the
140 # # string. Eg. <tt>['Hi %s.','John']</tt>
141 # def add(attribute, msg= @@default_error_messages[:invalid])
142 # if msg.is_a?(Array)
143 # args= msg.clone
144 # msg= args.shift
145 # args= nil if args.empty?
146 # end
147 # msg= ltry(msg)
148 # msg= msg % args unless args.nil?
149 # add_without_gloc(attribute, msg)
150 # end
151 # # Inherits the current language from the base record.
152 # def current_language
153 # @base.current_language
154 # end
155 # end
156 156
157 157 module Validations #:nodoc:
158 158 module ClassMethods
159 159 # The default Rails version of this function creates an error message and then
160 160 # passes it to ActiveRecord.Errors.
161 161 # The GLoc version of this method, sends an array to ActiveRecord.Errors that will
162 162 # be turned into a string by ActiveRecord.Errors which in turn allows for the message
163 163 # of this validation function to be a GLoc string key.
164 164 def validates_length_of(*attrs)
165 165 # Merge given options with defaults.
166 166 options = {
167 167 :too_long => ActiveRecord::Errors.default_error_messages[:too_long],
168 168 :too_short => ActiveRecord::Errors.default_error_messages[:too_short],
169 169 :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]
170 170 }.merge(DEFAULT_VALIDATION_OPTIONS)
171 171 options.update(attrs.pop.symbolize_keys) if attrs.last.is_a?(Hash)
172 172
173 173 # Ensure that one and only one range option is specified.
174 174 range_options = ALL_RANGE_OPTIONS & options.keys
175 175 case range_options.size
176 176 when 0
177 177 raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
178 178 when 1
179 179 # Valid number of options; do nothing.
180 180 else
181 181 raise ArgumentError, 'Too many range options specified. Choose only one.'
182 182 end
183 183
184 184 # Get range option and value.
185 185 option = range_options.first
186 186 option_value = options[range_options.first]
187 187
188 188 case option
189 189 when :within, :in
190 190 raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range)
191 191
192 192 too_short = [options[:too_short] , option_value.begin]
193 193 too_long = [options[:too_long] , option_value.end ]
194 194
195 195 validates_each(attrs, options) do |record, attr, value|
196 196 if value.nil? or value.split(//).size < option_value.begin
197 197 record.errors.add(attr, too_short)
198 198 elsif value.split(//).size > option_value.end
199 199 record.errors.add(attr, too_long)
200 200 end
201 201 end
202 202 when :is, :minimum, :maximum
203 203 raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0
204 204
205 205 # Declare different validations per option.
206 206 validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
207 207 message_options = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }
208 208
209 209 message = [(options[:message] || options[message_options[option]]) , option_value]
210 210
211 211 validates_each(attrs, options) do |record, attr, value|
212 212 if value.kind_of?(String)
213 213 record.errors.add(attr, message) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value]
214 214 else
215 215 record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
216 216 end
217 217 end
218 218 end
219 219 end
220 220
221 221 alias_method :validates_size_of, :validates_length_of
222 222 end
223 223 end
224 224 end
225 225
226 226 # ==============================================================================
227 227
228 228 module ApplicationHelper #:nodoc:
229 229 include GLoc
230 230 end
General Comments 0
You need to be logged in to leave comments. Login now