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
- .current ⇒ Object
- .current=(new_locale) ⇒ Object
- .from_accept_language(accept_language) ⇒ Object
-
.new(name) ⇒ Object
Create new locale object and returns it.
- .parse(header) ⇒ Object
- .pure_new ⇒ Object
- .support?(locale) ⇒ Boolean
- .supported_locales ⇒ Object
Instance Method Summary collapse
- #_mg_gettext ⇒ Object
- #any? ⇒ Boolean
- #base_locale ⇒ Object
-
#initialize(name) ⇒ Locale
constructor
A new instance of Locale.
- #java_locale ⇒ Object
- #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
134 135 136 |
# File 'lib/merb_global/locale.rb', line 134 def self.choose(except) new((supported_locales - except.map{|e| e.to_s}).first) end |
.current ⇒ Object
87 88 89 |
# File 'lib/merb_global/locale.rb', line 87 def self.current Thread.current.mg_locale end |
.current=(new_locale) ⇒ Object
91 92 93 |
# File 'lib/merb_global/locale.rb', line 91 def self.current=(new_locale) Thread.current.mg_locale = new_locale end |
.from_accept_language(accept_language) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/merb_global/locale.rb', line 71 def self.from_accept_language(accept_language) 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 end |
.new(name) ⇒ Object
Create new locale object and returns it.
Please note that this method is cached.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/merb_global/locale.rb', line 105 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/merb_global/locale.rb', line 55 def self.parse(header) 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
96 |
# File 'lib/merb_global/locale.rb', line 96 alias_method :pure_new, :new |
.support?(locale) ⇒ Boolean
126 127 128 |
# File 'lib/merb_global/locale.rb', line 126 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
19 20 21 |
# File 'lib/merb_global/locale.rb', line 19 def any? language == '*' && country.nil? end |
#base_locale ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/merb_global/locale.rb', line 23 def base_locale if not @country.nil? Locale.new(@language) else nil end end |
#java_locale ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/merb_global/locale.rb', line 40 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
31 32 33 34 35 36 37 |
# File 'lib/merb_global/locale.rb', line 31 def to_s if country.nil? "#{@language.downcase}" else "#{@language.downcase}_#{@country.upcase}" end end |