diff --git a/lib/locale.rb b/lib/locale.rb index 95b965a..10bc71e 100644 --- a/lib/locale.rb +++ b/lib/locale.rb @@ -29,13 +29,15 @@ module Locale module_function def require_driver(name) #:nodoc: - require File.join(ROOT, "locale/driver", name.to_s) + require File.join(ROOT, "locale/driver", name.to_s).untaint end def create_language_tag(tag) #:nodoc: if tag if tag.kind_of? Locale::Tag::Simple tag + elsif tag.kind_of? Locale::TagList + tag[0] else Locale::Tag.parse(tag) end diff --git a/lib/locale/driver/env.rb b/lib/locale/driver/env.rb index a3e1b34..4445746 100644 --- a/lib/locale/driver/env.rb +++ b/lib/locale/driver/env.rb @@ -24,11 +24,11 @@ module Driver module Env module_function - # Gets the locale from environment variable. (LC_ALL > LC_MESSAGES > LANG) + # Gets the locale from environment variable. (LC_ALL > LC_CTYPE > LANG) # Returns: the locale as Locale::Tag::Posix. def locale # At least one environment valiables should be set on *nix system. - [ENV["LC_ALL"], ENV["LC_MESSAGES"], ENV["LANG"]].each do |loc| + [ENV["LC_ALL"], ENV["LC_CTYPE"], ENV["LANG"]].each do |loc| if loc != nil and loc.size > 0 return Locale::Tag::Posix.parse(loc) end diff --git a/lib/locale/tag/posix.rb b/lib/locale/tag/posix.rb index b04aa54..1ce944a 100644 --- a/lib/locale/tag/posix.rb +++ b/lib/locale/tag/posix.rb @@ -30,9 +30,9 @@ def initialize(language, region = nil, charset = nil, modifier = nil) end def self.parse(tag) - if tag =~ /^(C|POSIX)$/ - ret = self.new("en", "US") - ret.tag = tag + if tag =~ /\A(C|POSIX)(?:\.([^@]+))?\Z/ + ret = self.new("en", "US", $2) + ret.tag = $1 ret elsif tag =~ TAG_RE ret = self.new($1, $2, $3, $4) @@ -47,10 +47,15 @@ def self.parse(tag) # _.@ # (e.g.) "ja_JP.EUC-JP@Modifier" def to_s - s = @language.dup - s << "_#{@region}" if @region - s << ".#{@charset}" if @charset - s << "@#{@modifier}" if @modifier + if posix? + s = tag.dup + s << ".#{@charset}" if @charset + else + s = @language.dup + s << "_#{@region}" if @region + s << ".#{@charset}" if @charset + s << "@#{@modifier}" if @modifier + end s end @@ -92,6 +97,10 @@ def convert_to(klass) end end + def posix? + ['POSIX', 'C'].include? tag + end + end end end diff --git a/lib/locale/taglist.rb b/lib/locale/taglist.rb index e5d879c..2b6c8e7 100644 --- a/lib/locale/taglist.rb +++ b/lib/locale/taglist.rb @@ -46,11 +46,7 @@ def script end # Returns the top priority charset. (posix) def charset - if self[0].respond_to? :charset - self[0].charset - else - ::Locale.driver_module.charset - end + self[0].respond_to?(:charset) and self[0].charset or ::Locale.driver_module.charset end memoize :charset diff --git a/test/test_detect_general.rb b/test/test_detect_general.rb index 2367354..47e397c 100644 --- a/test/test_detect_general.rb +++ b/test/test_detect_general.rb @@ -6,14 +6,14 @@ class TestDetectGeneral < Test::Unit::TestCase def setup Locale.clear_all ENV["LC_ALL"] = nil - ENV["LC_MESSAGES"] = nil + ENV["LC_CTYPE"] = nil ENV["LANG"] = nil ENV["LANGUAGE"] = nil end def test_lc_all ENV["LC_ALL"] = "ja_JP.eucJP" - ENV["LC_MESSAGES"] = "zh_CN.UTF-8" #Ignored. + ENV["LC_CTYPE"] = "zh_CN.UTF-8" #Ignored. ENV["LANG"] = "ko_KR.UTF-8" #Ignored. ENV["LANGUAGE"] = nil @@ -29,7 +29,7 @@ def test_lc_all def test_lc_messages ENV["LC_ALL"] = nil - ENV["LC_MESSAGES"] = "ja_JP.eucJP" + ENV["LC_CTYPE"] = "ja_JP.eucJP" ENV["LANG"] = "ko_KR.UTF-8" #Ignored. ENV["LANGUAGE"] = nil @@ -45,7 +45,7 @@ def test_lc_messages def test_lang ENV["LC_ALL"] = nil - ENV["LC_MESSAGES"] = nil + ENV["LC_CTYPE"] = nil ENV["LANG"] = "ja_JP.eucJP" ENV["LANGUAGE"] = nil @@ -61,7 +61,7 @@ def test_lang def test_lang_complex ENV["LC_ALL"] = "zh_CN.UTF-8" # Ignored. - ENV["LC_MESSAGES"] = "ko_KR.UTF-8" #Ingored. + ENV["LC_CTYPE"] = "ko_KR.UTF-8" #Ingored. ENV["LANG"] = "en_US.UTF-8" # Ignored. ENV["LANGUAGE"] ="ja_JP.eucJP:zh_CN.UTF-8" diff --git a/test/test_driver_jruby.rb b/test/test_driver_jruby.rb index 692d340..6fe7bbf 100644 --- a/test/test_driver_jruby.rb +++ b/test/test_driver_jruby.rb @@ -12,7 +12,7 @@ def setup end def set_locale(tag) - java.util.Locale.setDefault(java.util.Locale.new(tag.language, tag.region, tag.variants.to_s)) + java.util.Locale.setDefault(java.util.Locale.new(tag.language, tag.region, tag.variants.join('_'))) end def test_charset