lib/merb_global/locale.rb in merb_global-0.0.6 vs lib/merb_global/locale.rb in merb_global-0.0.7
- old
+ new
@@ -8,18 +8,18 @@
module Merb
module Global
class Locale
attr_reader :language, :country
-
+
def initialize(name)
- # TODO: Understend RFC 1766 fully
+ # TODO: Understand RFC 1766 fully
@language, @country = name.split('-')
end
def any?
- language == '*' && country.nil?\
+ language == '*' && country.nil?
end
def base_locale
if not @country.nil?
Locale.new(@language)
@@ -33,10 +33,26 @@
"#{@language.downcase}"
else
"#{@language.downcase}_#{@country.upcase}"
end
end
+
+ if defined? RUBY_ENGINE and RUBY_ENGINE == "jruby"
+ 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
+ else
+ def java_locale
+ nil
+ end
+ end
def self.parse(header)
header = header.split(',')
header.collect! {|lang| lang.delete ' ' "\n" "\r" "\t"}
header.reject! {|lang| lang.empty?}
@@ -46,15 +62,30 @@
[lang[0], 1.0]
else
[lang[0], lang[1].to_f]
end
end
- header.sort! {|lang_a, lang_b| lang_a[1] <=> lang_b[1]}
- header.collect! {|lang| lang[0]}
- return header.collect! {|lang| Locale.new(lang)}
+ header.sort! {|lang_a, lang_b| lang_b[1] <=> lang_a[1]} # sorting by decreasing quality
+ header.collect! {|lang| Locale.new(lang[0])}
end
-
+
+ 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
+
def self.current
Thread.current.mg_locale
end
def self.current=(new_locale)
@@ -88,9 +119,21 @@
n = pure_new(name)
@@current[name] = WeakRef.new(n)
n
end
end
+ end
+
+ def self.support?(locale)
+ supported_locales.include? locale.to_s
+ end
+
+ def self.supported_locales
+ Merb::Global::config('locales', ['en'])
+ end
+
+ def self.choose(except)
+ new((supported_locales - except.map{|e| e.to_s}).first)
end
end
end
end