| Module | GettextLocalize::Controller |
| In: |
vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb
|
extends controller to by default set locale and overwrite using before_filters
binds default textdomain in every controller can be overriden by calling bindtextdomain
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 41
41: def bind_default_textdomain(textdomain=nil)
42: textdomain = GettextLocalize::textdomain if textdomain.nil?
43: textdomain = ActionController::Base.textdomainname if textdomain.nil?
44: unless textdomain.nil?
45: GetText::bindtextdomain(textdomain,:path=>GettextLocalize::get_locale_path)
46: true
47: end
48: end
loads default gettext in every controller can be overriden in a controller by calling init_gettext
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 15
15: def init_default_gettext(textdomain=nil)
16: textdomain = GettextLocalize::textdomain if textdomain.nil?
17: unless textdomain.nil?
18: ActionController::Base::init_gettext(textdomain)
19: true
20: end
21: end
sets a controllers country by default uses GettextLocalize::default_country
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 52
52: def set_country(country)
53: unless country.nil?
54: GettextLocalize::set_country(country)
55: true
56: end
57: end
by default sets locale, textdomain and gettext in all controllers forces default locale if nothing found, not set when executing set_locale_by directly
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 63
63: def set_default_gettext_locale(locale=nil,textdomain=nil,methods=nil)
64: methods = GettextLocalize::methods if methods.nil?
65: methods << :default
66: set_locale_by(*methods)
67: bind_default_textdomain(textdomain)
68: init_default_gettext(textdomain)
69: end
loads default locale in every controller can be overriden by calling set_locale
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 25
25: def set_default_locale(locale=nil)
26: locale = GettextLocalize::locale if locale.nil?
27: unless locale.nil?
28: GettextLocalize::set_locale(locale)
29: true
30: end
31: end
sets controllers locale by the methods specified tries them in order till one works available methods = :header, :cookie, :session, :param example use in controller class, calling before_filter with params: before_filter(:except=>"feed"){|c| c.set_locale_by :param, :session, :header }
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 76
76: def set_locale_by(*methods)
77: params = []
78: if methods.first.kind_of? Array
79: params = methods[1..-1]
80: methods = methods.first
81: end
82: methods << :default
83: methods.each do |method|
84: func = "set_locale_by_#{method}".to_sym
85: if respond_to?(func)
86: return true if self.send(func,*params) == true
87: end
88: end
89: end
sets the controllers locale by the content of a cookie by default takes the cookie named ‘lang’ tries to find the language localizations starting from the first language specified, separated by commas, example cookies[‘lang’] = ‘es-es,es,en,en-us‘ add to your controller: before_filter :set_locale_by_cookie
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 117
117: def set_locale_by_cookie(name="lang")
118: GettextLocalize::set_locale(nil)
119: locales = self.get_locales_from_hash(cookies,name)
120: return unless locales
121: locales.each do |locale|
122: if GettextLocalize.has_locale?(locale)
123: return set_default_locale(locale)
124: end
125: end
126: end
sets the default locale used to define set_locale_by :param, :default
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 171
171: def set_locale_by_default(name='lang')
172: set_default_locale
173: end
sets the controllers locale by the user header tries to find the language localizations starting from the best language example header: HTTP_ACCEPT_LANGUAGE = es-es,es;q=0.8,en-us;q=0.5,en;q=0.3 add to your controller: before_filter :set_locale_by_header
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 98
98: def set_locale_by_header(name='lang')
99: name = 'HTTP_ACCEPT_LANGUAGE'
100: GettextLocalize::set_locale(nil)
101: locales = self.get_locales_from_hash(request.env,name)
102: return unless locales
103: locales.each do |locale|
104: if GettextLocalize.has_locale?(locale)
105: return set_default_locale(locale)
106: end
107: end
108: end
sets the controllers locale by the value of a param passed by GET or POST, by default named ‘lang’ with values of locales separated by commas, tries to find them in order, for example calling the url localhost:3000/?lang=es and in the controller: before_filter :set_locale_by_param
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 158
158: def set_locale_by_param(name='lang')
159: GettextLocalize::set_locale(nil)
160: locales = self.get_locales_from_hash(params,name)
161: return unless locales
162: locales.each do |locale|
163: if GettextLocalize.has_locale?(locale)
164: return set_default_locale(locale)
165: end
166: end
167: end
sets the controllers locale by the content of a cookie by default takes the cookie named ‘lang’ tries to find the language localizations starting from the first language specified, separated by commas, example session[‘lang’] = ‘es,fr‘ add to your controller: before_filter :set_locale_by_session remember this only saves the session lang if use session is activated
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 137
137: def set_locale_by_session(name='lang')
138: GettextLocalize::set_locale(nil)
139: locales = self.get_locales_from_hash(session,name)
140: return unless locales
141: locales.each do |locale|
142: # has_locale? checks if locale file exists
143: # FIXME: could return false if locale file
144: # outside of the application
145: if GettextLocalize.has_locale?(locale)
146: return set_default_locale(locale)
147: end
148: end
149: end
sets locale unless nil or empty
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 34
34: def set_locale_if(locale=nil)
35: locale = locale.respond_to?(:to_s) ? locale.to_s : ""
36: set_default_locale locale unless locale.strip.empty?
37: end
reads a locales parameter from a hash accepts header format with priorities (see set_locale_by_header) and a list of locales separated by commas
# File vendor/plugins/gettext_localize/lib/gettext_localize_rails.rb, line 180
180: def get_locales_from_hash(hash,name='lang')
181: name = name.to_sym if hash[name.to_sym]
182: name = name.to_s if hash[name].respond_to?(:empty?) and hash[name].empty?
183: return unless hash[name]
184: value = hash[name].dup
185: return unless value.respond_to?(:to_s)
186: value = value.to_s.strip
187: return if value.empty?
188:
189: if value.include?("q=") # format with priorities
190: locales = {}
191: value.scan(/([^;]+);q=([^,]+),?/).each do |langs,priority|
192: locales[priority.to_f] = langs.split(",")
193: end
194: locales = locales.sort.reverse.map{|p| p.last }.flatten
195: else # format separated commas
196: locales = []
197: locales = value.split(",")
198: end
199: return locales
200: end