edgerunner-globalite 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,56 @@
1
+ class Locale
2
+ attr_reader :language, :country, :code
3
+
4
+ #
5
+ def self.language
6
+ Globalite.language
7
+ end
8
+
9
+ # Return the country
10
+ def self.country
11
+ Globalite.country
12
+ end
13
+
14
+ # Return the user's locale or the system's if the user doesn't have one set
15
+ def self.code
16
+ "#{Globalite.language}-#{Globalite.country}".to_sym
17
+ end
18
+
19
+ #
20
+ def self.set_code(locale)
21
+ if locale.to_s.split('-') && locale.to_s.length.between?(4,5) && Globalite.locales.include?(locale.to_sym)
22
+ Globalite.language = locale.to_s.split('-')[0].downcase.to_sym if locale.to_s.split('-')[0]
23
+ Globalite.country = locale.to_s.split('-')[1].upcase.to_sym if locale.to_s.split('-')[1]
24
+ end
25
+ end
26
+
27
+ def self.code=(locale)
28
+ self.set_code(locale)
29
+ end
30
+
31
+ # Return the available locales
32
+ def self.codes
33
+ Globalite.locales
34
+ end
35
+
36
+ # Return the locale name in its own language for instance fr-FR => Français
37
+ def self.name(locale)
38
+ Globalite.locale_name(locale)
39
+ end
40
+
41
+ # Return the list of the UI locales with their name
42
+ def self.ui_locales
43
+ Globalite.ui_locales
44
+ end
45
+
46
+ # Return the list of the Rails locales with their name
47
+ def self.rails_locales
48
+ Globalite.rails_locales
49
+ end
50
+
51
+ # Reset the Locale to the default settings
52
+ def self.reset!
53
+ Locale.set_code("#{Globalite.default_language}-#{Globalite.default_country}")
54
+ end
55
+
56
+ end
@@ -0,0 +1,19 @@
1
+ module SymbolExtension # :nodoc:
2
+ # Localizes the symbol into the current locale.
3
+ # If there is no translation available, the replacement string will be returned
4
+ def localize(replacement_string = '__localization_missing__', args={})
5
+ Globalite.localize(self, replacement_string, args)
6
+ end
7
+ alias :l :localize
8
+
9
+ def l_in(locale, args={})
10
+ Globalite.localize(self, '_localization_missing_', args, locale) unless locale.nil?
11
+ end
12
+
13
+ # Note that this method takes the replacement string after the args hash unlike other Globalite methods
14
+ def localize_with_args(args={}, replacement_string = '__localization_missing__')
15
+ Globalite.localize(self, replacement_string, args)
16
+ end
17
+ alias :l_with_args :localize_with_args
18
+
19
+ end
@@ -0,0 +1,58 @@
1
+
2
+ class Array
3
+ alias :orig_to_sentence :to_sentence
4
+ def to_sentence(options = {})
5
+ #Blend default options with sent through options
6
+ options.reverse_merge!({ :connector => :array_connector.l, :skip_last_comma => Boolean(:array_skip_last_comma.l) })
7
+ orig_to_sentence(options)
8
+ end
9
+ end
10
+
11
+
12
+ class Time
13
+ # Acts the same as #strftime, but returns a localized version of the
14
+ # formatted date/time string.
15
+ def localize(format='default')
16
+ # unabashedly stolen from Globalize which unabashedly stole this snippet from Tadayoshi Funaba's Date class
17
+ o = ''
18
+ format = :date_helper_time_formats.l[format.to_s.downcase] if :date_helper_time_formats.l[format.to_s.downcase]
19
+ format.scan(/%[EO]?.|./o) do |c|
20
+ cc = c.sub(/^%[EO]?(.)$/o, '%\\1')
21
+ case cc
22
+ when '%A'; o << :date_helper_day_names.l[wday]
23
+ when '%a'; o << :date_helper_abbr_day_names.l[wday]
24
+ when '%B'; o << :date_helper_month_names.l[mon]
25
+ when '%b'; o << :date_helper_abbr_month_names.l[mon]
26
+ #when '%c'; o << :date_helper_time_formats.l[:default] ? :date_helper_date_formats.l[:default] : strftime('%Y-%m-%d')
27
+ when '%p'; o << if hour < 12 then :date_helper_am.l else :date_helper_pm.l end
28
+ else; o << c
29
+ end
30
+ end
31
+ strftime(o)
32
+ end
33
+ alias :l :localize
34
+ end
35
+
36
+ class Date
37
+ # Acts the same as #strftime, but returns a localized version of the formatted date string.
38
+ def localize(format='default')
39
+ # unabashedly stolen from Globalize which unabashedly stole this snippet from Tadayoshi Funaba's Date class
40
+ o = ''
41
+ format = :date_helper_date_formats.l[format.to_s.downcase] if :date_helper_date_formats.l[format.to_s.downcase]
42
+ format.scan(/%[EO]?.|./o) do |c|
43
+ cc = c.sub(/^%[EO]?(.)$/o, '%\\1')
44
+ case cc
45
+ when '%A'; o << :date_helper_day_names.l[wday]
46
+ when '%a'; o << :date_helper_abbr_day_names.l[wday]
47
+ when '%B'; o << :date_helper_month_names.l[mon]
48
+ when '%b'; o << :date_helper_abbr_month_names.l[mon]
49
+ #when '%c'; o << :date_helper_time_formats.l[:default] ? :date_helper_date_formats.l[:default] : strftime('%Y-%m-%d')
50
+ when '%p'; o << if hour < 12 then :date_helper_am.l else :date_helper_pm.l end
51
+ else; o << c
52
+ end
53
+ end
54
+ strftime(o)
55
+ end
56
+ alias :l :localize
57
+
58
+ end
@@ -0,0 +1,18 @@
1
+ module ActionController
2
+ class Base
3
+
4
+ # Error messages modified in lang file
5
+ @@resources_path_names.update ({
6
+ :new => :resources_path_new.l('new') ,
7
+ :edit => :resources_path_edit.l('edit')
8
+ }) unless RAILS_GEM_VERSION < "2.1.0"
9
+
10
+ # Reloads the localization
11
+ def self.relocalize
12
+ @@resources_path_names.update ({
13
+ :new => :resources_path_new.l('new') ,
14
+ :edit => :resources_path_edit.l('edit')
15
+ }) unless RAILS_GEM_VERSION < "2.1.0"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,229 @@
1
+ module ActionView
2
+
3
+ # Modify DateHelper to use localization keys
4
+ module Helpers
5
+
6
+ #Modify DateHelper distance_of_time_in_words
7
+ module DateHelper
8
+
9
+ def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
10
+ from_time = from_time.to_time if from_time.respond_to?(:to_time)
11
+ to_time = to_time.to_time if to_time.respond_to?(:to_time)
12
+ distance_in_minutes = (((to_time - from_time).abs)/60).round
13
+ distance_in_seconds = ((to_time - from_time).abs).round
14
+
15
+ case distance_in_minutes
16
+ when 0..1
17
+ return (distance_in_minutes==0) ? :date_helper_less_than_a_minute.l : :date_helper_one_minute.l unless include_seconds
18
+ case distance_in_seconds
19
+ when 0..5 then format( :date_helper_less_than_x_seconds.l , 5 )
20
+ when 6..10 then format( :date_helper_less_than_x_seconds.l , 10 )
21
+ when 11..20 then format( :date_helper_less_than_x_seconds.l , 20 )
22
+ when 21..40 then :date_helper_half_a_minute.l
23
+ when 41..59 then :date_helper_less_than_a_minute.l
24
+ else :date_helper_one_minute.l
25
+ end
26
+
27
+ when 2..44 then format(:date_helper_x_minutes.l, distance_in_minutes)
28
+ when 45..89 then :date_helper_one_hour.l
29
+ when 90..1439 then format( :date_helper_x_hours.l , (distance_in_minutes.to_f / 60.0).round )
30
+ when 1440..2879 then :date_helper_one_day.l
31
+ when 2880..43199 then format( :date_helper_x_days.l , (distance_in_minutes / 1440).round )
32
+ when 43200..86399 then :date_helper_one_month.l
33
+ when 86400..525959 then format( :date_helper_x_months.l , (distance_in_minutes / 43200).round )
34
+ when 525960..1051919 then :date_helper_one_year.l
35
+ else format( :date_helper_x_years.l , (distance_in_minutes / 525960).round )
36
+ end
37
+ end
38
+ end
39
+
40
+ module NumberHelper
41
+
42
+ # modify number_to_currency to accept :order option
43
+ def number_to_currency(number, options = {})
44
+ # Blend default options with localized currency options
45
+ options.reverse_merge!({:unit => :number_helper_unit.l, :separator => :number_helper_separator.l, :delimiter => :number_helper_delimiter.l, :order => :number_helper_order.l})
46
+ options = options.stringify_keys
47
+
48
+ precision, unit, separator, delimiter = options.delete("precision") { 2 }, options.delete("unit") { "$" }, options.delete("separator") { "." }, options.delete("delimiter") { "," }
49
+ separator = "" unless precision > 0
50
+
51
+ #add leading space before trailing unit
52
+ unit = " " + unit if options["order"] == ['number', 'unit']
53
+ output = ''
54
+ begin
55
+ options["order"].each do |param|
56
+ case param
57
+ when 'unit'
58
+ output << unit
59
+ when 'number'
60
+ parts = number_with_precision(number, precision).split('.')
61
+ output << number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s
62
+ end
63
+ end
64
+ rescue
65
+ output = number
66
+ end
67
+ output
68
+ end
69
+ end# module NumberHelper
70
+
71
+ module DateHelper
72
+
73
+ # Blend default options with localized :order option and add locale support
74
+ # Example:
75
+ # <% form_for(@page) do |f| %>
76
+ # <p>
77
+ # <b><%= :published_date.l_in(@locale) %></b><br />
78
+ # <%= f.date_select :published_date, :locale => @locale %>
79
+ # </p>
80
+ #
81
+ def date_select(object_name, method, options = {}, html_options = {})
82
+ if options[:locale]
83
+ @original_locale = Locale.code
84
+ Locale.code = options[:locale]
85
+ end
86
+ options.reverse_merge!( :order => :date_helper_order.l )
87
+ @selector = InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_date_select_tag(options)
88
+ Locale.code = @original_locale if options[:locale]
89
+ return @selector
90
+ end
91
+
92
+ # Blend default options with localized :order option
93
+ # Look at date_select for an usage example
94
+ def datetime_select(object_name, method, options = {}, html_options = {})
95
+ if options[:locale]
96
+ @original_locale = Locale.code
97
+ Locale.code = options[:locale]
98
+ end
99
+ options.reverse_merge!( :order => :date_helper_order.l )
100
+ @selector = InstanceTag.new(object_name, method, self, nil, options.delete(:object)).to_datetime_select_tag(options)
101
+ Locale.code = @original_locale if options[:locale]
102
+ return @selector
103
+ end
104
+
105
+ def select_month(date, options = {}, html_options = {})
106
+ if options[:locale]
107
+ @original_locale = Locale.code
108
+ Locale.code = options[:locale]
109
+ end
110
+ val = date ? (date.kind_of?(Fixnum) ? date : date.month) : ''
111
+ if options[:use_hidden]
112
+ @selector = hidden_html(options[:field_name] || 'month', val, options)
113
+ else
114
+ month_options = []
115
+ monthnames = :date_helper_month_names.l
116
+ abbr_monthnames = :date_helper_abbr_month_names.l
117
+ month_names = options[:use_month_names] || (options[:use_short_month] ? abbr_monthnames : monthnames)
118
+ month_names.unshift(nil) if month_names.size < 13
119
+ 1.upto(12) do |month_number|
120
+ month_name = if options[:use_month_numbers]
121
+ month_number
122
+ elsif options[:add_month_numbers]
123
+ month_number.to_s + ' - ' + month_names[month_number]
124
+ else
125
+ month_names[month_number]
126
+ end
127
+
128
+ month_options << ((val == month_number) ?
129
+ %(<option value="#{month_number}" selected="selected">#{month_name}</option>\n) :
130
+ %(<option value="#{month_number}">#{month_name}</option>\n)
131
+ )
132
+ end
133
+ @selector = select_html(options[:field_name] || 'month', month_options, options)
134
+ end
135
+ Locale.code = @original_locale if options[:locale]
136
+ return @selector
137
+ end
138
+
139
+ end #module DateHelper
140
+
141
+ class InstanceTag
142
+ def to_label_tag(text = nil, options = {})
143
+ if options[:locale]
144
+ @original_locale = Locale.code
145
+ Locale.code = options[:locale]
146
+ end
147
+ options = options.stringify_keys
148
+ name_and_id = options.dup
149
+ add_default_name_and_id(name_and_id)
150
+ options.delete("index")
151
+ options["for"] ||= name_and_id["id"]
152
+ content = (text.blank? ? nil : text.to_s) ||
153
+ (!method_name.blank? &&
154
+ method_name.to_sym.l != "__localization_missing__" ? method_name.to_sym.l : method_name.humanize)
155
+ Locale.code = @original_locale if options[:locale]
156
+ label_tag(name_and_id["id"], content, options)
157
+ end # to_label_tag
158
+ end # class InstanceTag
159
+
160
+ module FormOptionsHelper
161
+
162
+ def country_options_for_select(selected = nil, priority_countries = nil)
163
+ country_options = ""
164
+
165
+ if priority_countries
166
+ country_options += options_for_select(priority_countries, selected)
167
+ country_options += "<option value=\"\">-------------</option>\n"
168
+ end
169
+
170
+ if priority_countries && priority_countries.include?(selected)
171
+ country_options += options_for_select(:countries_list.l - priority_countries, selected)
172
+ else
173
+ country_options += options_for_select(:countries_list.l, selected)
174
+ end
175
+
176
+ return country_options
177
+ end
178
+
179
+ end #module FormOptionsHelper
180
+
181
+ module ActiveRecordHelper
182
+ def error_messages_for(*params)
183
+ options = params.extract_options!.symbolize_keys
184
+ if object = options.delete(:object)
185
+ objects = [object].flatten
186
+ else
187
+ objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
188
+ end
189
+ count = objects.inject(0) {|sum, object| sum + object.errors.count }
190
+ unless count.zero?
191
+ html = {}
192
+ [:id, :class].each do |key|
193
+ if options.include?(key)
194
+ value = options[key]
195
+ html[key] = value unless value.blank?
196
+ else
197
+ html[key] = 'errorExplanation'
198
+ end
199
+ end
200
+
201
+ options[:object_name] ||= params.first
202
+
203
+ original_failed_object = options[:object_name].to_s.gsub('_', ' ')
204
+ failed_object = options[:object_name].to_sym.l(original_failed_object)
205
+
206
+ original_header_message = "#{pluralize(count, 'error')} prohibited this #{original_failed_object} from being saved"
207
+ header_message = :active_record_helper_header_message.l_with_args({:error_count => count, :failed_object => failed_object }, original_header_message)
208
+ message = :active_record_helper_error_description.l('There were problems with the following fields:')
209
+
210
+ # Fix for :header_message and :message values if present
211
+ # It works just like regular rails functionality
212
+ options[:header_message] = header_message unless options.include?(:header_message)
213
+ options[:message] = message unless options.include?(:message)
214
+ error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }
215
+
216
+ contents = ''
217
+ contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank?
218
+ contents << content_tag(:p, options[:message]) unless options[:message].blank?
219
+ contents << content_tag(:ul, error_messages)
220
+
221
+ content_tag(:div, contents, html)
222
+ else
223
+ ''
224
+ end
225
+ end
226
+ end #module ActiveRecordHelper
227
+
228
+ end #module Helpers
229
+ end #module ActionView
@@ -0,0 +1,80 @@
1
+ module ActiveRecord
2
+ class Errors
3
+
4
+ # Error messages modified in lang file
5
+ @@default_error_messages.update({
6
+ :inclusion => :error_message_inclusion.l,
7
+ :exclusion => :error_message_exclusion.l,
8
+ :invalid => :error_message_invalid.l,
9
+ :confirmation => :error_message_confirmation.l,
10
+ :accepted => :error_message_accepted.l,
11
+ :empty => :error_message_empty.l,
12
+ :blank => :error_message_blank.l,
13
+ :too_long => :error_message_too_long.l,
14
+ :too_short => :error_message_too_short.l,
15
+ :wrong_length => :error_message_wrong_length.l,
16
+ :taken => :error_message_taken.l,
17
+ :not_a_number => :error_message_not_a_number.l,
18
+ })
19
+
20
+ # Reloads the localization
21
+ def self.relocalize
22
+ @@default_error_messages.update({
23
+ :inclusion => :error_message_inclusion.l,
24
+ :exclusion => :error_message_exclusion.l,
25
+ :invalid => :error_message_invalid.l,
26
+ :confirmation => :error_message_confirmation.l,
27
+ :accepted => :error_message_accepted.l,
28
+ :empty => :error_message_empty.l,
29
+ :blank => :error_message_blank.l,
30
+ :too_long => :error_message_too_long.l,
31
+ :too_short => :error_message_too_short.l,
32
+ :wrong_length => :error_message_wrong_length.l,
33
+ :taken => :error_message_taken.l,
34
+ :not_a_number => :error_message_not_a_number.l,
35
+ })
36
+ end
37
+
38
+ # Redefine the ActiveRecord::Errors::full_messages method:
39
+ # Returns all the full error messages in an array. 'Base' messages are handled as usual.
40
+ # Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
41
+ # in which case the attribute name is omitted.
42
+ # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
43
+ #
44
+ #
45
+ # If field name has the same key like in language yaml file, its replaced by its corresponding language file value.
46
+ # This fixes the problem of translating validation messages but not field names. Now you can fully localize them.
47
+ # E.g. validates_presence_of :name
48
+ # produces (in en-UK and pl-PL:
49
+ # Name can't be empty
50
+ # Nazwa jest wymagana
51
+ # By convetion yaml language key falue for field is the same as ActiveRecords model field name
52
+ # If plugin can't find such key, it behaves just like without plugin.
53
+ def full_messages
54
+ full_messages = []
55
+
56
+ @errors.each_key do |attr|
57
+ @errors[attr].each do |msg|
58
+ next if msg.nil?
59
+
60
+ if attr == "base"
61
+ full_messages << msg
62
+ elsif msg =~ /^\^/
63
+ full_messages << msg[1..-1]
64
+ else
65
+ full_messages << attr.intern.l(attr).humanize + " " + msg
66
+ end
67
+ end
68
+ end
69
+
70
+ return full_messages
71
+ end
72
+
73
+ # # Handle model error localization
74
+ # def add(attribute, msg = @@default_error_messages[:invalid])
75
+ # @errors[attribute.l] = [] if @errors[attribute.to_s].nil?
76
+ # @errors[attribute.l] << msg
77
+ # end
78
+
79
+ end
80
+ end