diff --git a/lib/locale/driver/env.rb b/lib/locale/driver/env.rb index 66b56ac..df54503 100644 --- a/lib/locale/driver/env.rb +++ b/lib/locale/driver/env.rb @@ -34,11 +34,11 @@ module Driver module Env module_function - # Gets the locale from environment variable. (LC_ALL > LC_CTYPE > LANG) + # Gets the locale from environment variable. (LC_ALL > LC_MESSAGES > 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_CTYPE"], ENV["LANG"]].each do |loc| + [ENV["LC_ALL"], ENV["LC_MESSAGES"], ENV["LANG"]].each do |loc| if loc != nil and loc.size > 0 return Locale::Tag::Posix.parse(loc) end @@ -46,7 +46,7 @@ def locale nil end - # Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_CTYPE > LANG) + # Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_MESSAGES > LANG) # * Returns: an Array of the locale as Locale::Tag::Posix or nil. def locales locales = ENV["LANGUAGE"] @@ -61,14 +61,16 @@ def locales nil end - # Gets the charset from environment variable or return nil. + # Gets the charset from environment variables + # (LC_ALL > LC_CTYPE > LANG) or return nil. # * Returns: the system charset. def charset # :nodoc: - if loc = locale - loc.charset - else - nil + [ENV["LC_ALL"], ENV["LC_CTYPE"], ENV["LANG"]].each do |env| + next if env.nil? + next if env.empty? + return Locale::Tag::Posix.parse(env).charset end + nil end end diff --git a/lib/locale/driver/posix.rb b/lib/locale/driver/posix.rb index eaf03e4..11838f5 100644 --- a/lib/locale/driver/posix.rb +++ b/lib/locale/driver/posix.rb @@ -30,7 +30,7 @@ module Posix $stderr.puts self.name + " is loaded." if $DEBUG module_function - # Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_CTYPE > LANG) + # Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_MESSAGES > LANG) # Only LANGUAGE accept plural languages such as "nl_BE; # * Returns: an Array of the locale as Locale::Tag::Posix or nil. def locales diff --git a/test/test_detect_general.rb b/test/test_detect_general.rb index 5edfba6..16d0ff3 100644 --- a/test/test_detect_general.rb +++ b/test/test_detect_general.rb @@ -29,13 +29,14 @@ def setup Locale.clear_all ENV["LC_ALL"] = nil ENV["LC_CTYPE"] = nil + ENV["LC_MESSAGES"] = nil ENV["LANG"] = nil ENV["LANGUAGE"] = nil end def test_lc_all ENV["LC_ALL"] = "ja_JP.eucJP" - ENV["LC_CTYPE"] = "zh_CN.UTF-8" #Ignored. + ENV["LC_MESSAGES"] = "zh_CN.UTF-8" #Ignored. ENV["LANG"] = "ko_KR.UTF-8" #Ignored. ENV["LANGUAGE"] = nil @@ -49,9 +50,9 @@ def test_lc_all assert_equal "eucJP", Locale.charset end - def test_lc_ctype + def test_lc_messages ENV["LC_ALL"] = nil - ENV["LC_CTYPE"] = "ja_JP.eucJP" + ENV["LC_MESSAGES"] = "ja_JP.eucJP" ENV["LANG"] = "ko_KR.UTF-8" #Ignored. ENV["LANGUAGE"] = nil @@ -62,12 +63,12 @@ def test_lc_ctype assert_equal "eucJP", lang.charset assert_equal Locale::Tag::Posix.new("ja", "JP", "eucJP"), lang - assert_equal "eucJP", Locale.charset + assert_equal "UTF-8", Locale.charset end def test_lang ENV["LC_ALL"] = nil - ENV["LC_CTYPE"] = nil + ENV["LC_MESSAGES"] = nil ENV["LANG"] = "ja_JP.eucJP" ENV["LANGUAGE"] = nil @@ -83,7 +84,7 @@ def test_lang def test_lang_complex ENV["LC_ALL"] = "zh_CN.UTF-8" # Ignored. - ENV["LC_CTYPE"] = "ko_KR.UTF-8" #Ingored. + ENV["LC_MESSAGES"] = "ko_KR.UTF-8" #Ingored. ENV["LANG"] = "en_US.UTF-8" # Ignored. ENV["LANGUAGE"] ="ja_JP.eucJP:zh_CN.UTF-8" @@ -232,6 +233,40 @@ def test_clear end + class TestCharset < self + def test_lc_all + ENV["LC_ALL"] = "ja_JP.eucJP" + ENV["LC_CTYPE"] = "ko_KR.eucKR" # Ignored. + ENV["LANG"] = "fr_FR.ISO-8859-1" # Ignored. + + assert_equal("eucJP", Locale.charset) + end + + def test_lc_ctype + ENV["LC_ALL"] = nil + ENV["LC_CTYPE"] = "ko_KR.eucKR" + ENV["LANG"] = "fr_FR.ISO-8859-1" # Ignored. + + assert_equal("eucKR", Locale.charset) + end + + def test_lc_messages + ENV["LC_ALL"] = nil + ENV["LC_MESSAGES"] = "ko_KR.eucKR" # Ignored. + ENV["LANG"] = "fr_FR.ISO-8859-1" + + assert_equal("ISO-8859-1", Locale.charset) + end + + def test_lang + ENV["LC_ALL"] = nil + ENV["LC_CTYPE"] = nil + ENV["LANG"] = "fr_FR.ISO-8859-1" + + assert_equal("ISO-8859-1", Locale.charset) + end + end + private def jruby? RUBY_PLATFORM == "java"