Class: Merb::Global::Locale
- Inherits:
-
Object
- Object
- Merb::Global::Locale
- Defined in:
- lib/merb_global/message_providers/gettext.rb,
lib/merb_global/locale.rb
Overview
:nodoc:
Constant Summary collapse
- @@current =
{}
- @@current_mutex =
Mutex.new
Instance Attribute Summary collapse
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
Class Method Summary collapse
-
.choose(except) ⇒ Object
Chooses one of the supported locales which is not in array given as argument.
-
.current ⇒ Object
Returns current locale.
-
.current=(new_locale) ⇒ Object
Sets the current locale.
-
.from_accept_language(accept_language) ⇒ Object
:nodoc:.
-
.new(name) ⇒ Object
Create new locale object and returns it.
-
.parse(header) ⇒ Object
:nodoc:.
- .pure_new ⇒ Object
-
.support?(locale) ⇒ Boolean
Checks if the locale is supported.
-
.supported_locales ⇒ Object
Lists the supported locale.
Instance Method Summary collapse
- #_mg_gettext ⇒ Object
-
#any? ⇒ Boolean
This method checks if the locale is ‘wildcard’ locale.
- #as_rfc1766 ⇒ Object
-
#base_locale ⇒ Object
This method returns the parent locale - for locales for countries (such as en_GB) it returns language(en).
-
#initialize(name) ⇒ Locale
constructor
A new instance of Locale.
-
#java_locale ⇒ Object
This method return corresponding java locales caching the result.
- #to_s ⇒ Object
Constructor Details
#initialize(name) ⇒ Locale
Returns a new instance of Locale.
14 15 16 17 |
# File 'lib/merb_global/locale.rb', line 14 def initialize(name) # TODO: Understand RFC 1766 fully @language, @country = name.split('-') end |
Instance Attribute Details
#country ⇒ Object (readonly)
Returns the value of attribute country.
12 13 14 |
# File 'lib/merb_global/locale.rb', line 12 def country @country end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
12 13 14 |
# File 'lib/merb_global/locale.rb', line 12 def language @language end |
Class Method Details
.choose(except) ⇒ Object
Chooses one of the supported locales which is not in array given as argument
160 161 162 |
# File 'lib/merb_global/locale.rb', line 160 def self.choose(except) new((supported_locales - except.map{|e| e.to_s}).first) end |
.current ⇒ Object
Returns current locale
108 109 110 |
# File 'lib/merb_global/locale.rb', line 108 def self.current Thread.current.mg_locale end |
.current=(new_locale) ⇒ Object
Sets the current locale
113 114 115 |
# File 'lib/merb_global/locale.rb', line 113 def self.current=(new_locale) Thread.current.mg_locale = new_locale end |
.from_accept_language(accept_language) ⇒ Object
:nodoc:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/merb_global/locale.rb', line 90 def self.from_accept_language(accept_language) #:nodoc: unless accept_language.nil? accept_language = Merb::Global::Locale.parse(accept_language) accept_language.each_with_index do |lang, i| if lang.any? # In this case we need to choose a locale that is not in accept_language[i+1..-1] return Merb::Global::Locale.choose(accept_language[i+1..-1]) elsif Merb::Global::Locale.support? lang return lang end lang = lang.base_locale return lang if lang && Merb::Global::Locale.support?(lang) end end return nil end |
.new(name) ⇒ Object
Create new locale object and returns it.
Please note that this method is cached.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/merb_global/locale.rb', line 127 def self.new(name) return nil if name.nil? return name if name.is_a? Locale @@current_mutex.synchronize do begin n = @@current[name] if n.nil? n = pure_new(name) @@current[name] = WeakRef.new(n) else n = n.__getobj__ end n rescue WeakRef::RefError n = pure_new(name) @@current[name] = WeakRef.new(n) n end end end |
.parse(header) ⇒ Object
:nodoc:
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/merb_global/locale.rb', line 74 def self.parse(header) #:nodoc: header = header.split(',') header.collect! {|lang| lang.delete ' ' "\n" "\r" "\t"} header.reject! {|lang| lang.empty?} header.collect! {|lang| lang.split ';q='} header.collect! do |lang| if lang.size == 1 [lang[0], 1.0] else [lang[0], lang[1].to_f] end end header.sort! {|lang_a, lang_b| lang_b[1] <=> lang_a[1]} # sorting by decreasing quality header.collect! {|lang| Locale.new(lang[0])} end |
.pure_new ⇒ Object
118 |
# File 'lib/merb_global/locale.rb', line 118 alias_method :pure_new, :new |
.support?(locale) ⇒ Boolean
Checks if the locale is supported
149 150 151 |
# File 'lib/merb_global/locale.rb', line 149 def self.support?(locale) supported_locales.include? locale.to_s end |
Instance Method Details
#_mg_gettext ⇒ Object
6 7 8 |
# File 'lib/merb_global/message_providers/gettext.rb', line 6 def _mg_gettext @mg_gettext ||= Merb::Global::MessageProviders::Gettext::GettextContext.new end |
#any? ⇒ Boolean
This method checks if the locale is ‘wildcard’ locale. I.e. if any locale will suit
31 32 33 |
# File 'lib/merb_global/locale.rb', line 31 def any? language == '*' && country.nil? end |
#as_rfc1766 ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/merb_global/locale.rb', line 19 def as_rfc1766 if @country.nil? "#{@language.downcase}" else "#{@language.downcase}-#{@country.downcase}" end end |
#base_locale ⇒ Object
This method returns the parent locale - for locales for countries (such as en_GB) it returns language(en). For languages it returns nil.
39 40 41 42 43 44 45 |
# File 'lib/merb_global/locale.rb', line 39 def base_locale if not @country.nil? Locale.new(@language) else nil end end |
#java_locale ⇒ Object
This method return corresponding java locales caching the result. Please note that if used outside jruby it returns nil.
59 60 61 62 63 64 65 66 67 |
# File 'lib/merb_global/locale.rb', line 59 def java_locale require 'java' @java_locale ||= if @country.nil? java.util.Locale.new(@language.downcase) else java.util.Locale.new(@language.downcase, @country.upcase) end end |
#to_s ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/merb_global/locale.rb', line 47 def to_s if country.nil? "#{@language.downcase}" else "#{@language.downcase}_#{@country.upcase}" end end |