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