From 0e693cefb0c5ad91cf332bc2bc993cabac010d68 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Fri, 20 Sep 2013 22:25:17 +0900 Subject: [PATCH] Use LC_CTYPE env for Locale.current[0].charset Becauese existing library such as sub-mail uses the API. GitHub: fix #2 Debian Bug: #690572 Reported by Stefano Zacchiroli. Thanks!!! Reported by Hleb Valoshka. Thanks!!! --- lib/locale/driver/env.rb | 24 ++++++++++++++-------- test/test_detect_general.rb | 40 ++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/lib/locale/driver/env.rb b/lib/locale/driver/env.rb index 69232f5..e672600 100644 --- a/lib/locale/driver/env.rb +++ b/lib/locale/driver/env.rb @@ -34,16 +34,24 @@ module Driver module Env module_function - # Gets the locale from environment variable. (LC_ALL > LC_MESSAGES > LANG) + # Gets the locale from environment variable. + # Priority order except charset is LC_ALL > LC_MESSAGES > LANG. + # Priority order for charset is 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| - if loc != nil and loc.size > 0 - return Locale::Tag::Posix.parse(loc) - end - end - nil + lc_all = Private.parse(ENV["LC_ALL"]) + return lc_all if lc_all + + lc_messages = Private.parse(ENV["LC_MESSAGES"]) + lang = Private.parse(ENV["LANG"]) + + tag = lc_messages || lang + return nil if tag.nil? + + lc_ctype = Private.parse(ENV["LC_CTYPE"]) + tag.charset = lc_ctype.charset if lc_ctype + + tag end # Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_MESSAGES > LANG) diff --git a/test/test_detect_general.rb b/test/test_detect_general.rb index 16d0ff3..cd9dad2 100644 --- a/test/test_detect_general.rb +++ b/test/test_detect_general.rb @@ -36,6 +36,7 @@ def setup def test_lc_all ENV["LC_ALL"] = "ja_JP.eucJP" + ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1" #Ignored. ENV["LC_MESSAGES"] = "zh_CN.UTF-8" #Ignored. ENV["LANG"] = "ko_KR.UTF-8" #Ignored. ENV["LANGUAGE"] = nil @@ -52,8 +53,9 @@ def test_lc_all def test_lc_messages ENV["LC_ALL"] = nil + ENV["LC_CTYPE"] = nil ENV["LC_MESSAGES"] = "ja_JP.eucJP" - ENV["LANG"] = "ko_KR.UTF-8" #Ignored. + ENV["LANG"] = "ko_KR.UTF-8" #Ignored except charset. ENV["LANGUAGE"] = nil lang = Locale.current[0] @@ -66,8 +68,26 @@ def test_lc_messages assert_equal "UTF-8", Locale.charset end + def test_lc_messages_with_lc_ctype + ENV["LC_ALL"] = nil + ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1" + ENV["LC_MESSAGES"] = "ja_JP.eucJP" + ENV["LANG"] = "ko_KR.UTF-8" #Ignored. + ENV["LANGUAGE"] = nil + + lang = Locale.current[0] + assert_equal Locale::Tag::Posix, lang.class + assert_equal "ja", lang.language + assert_equal "JP", lang.region + assert_equal "ISO-8859-1", lang.charset + assert_equal Locale::Tag::Posix.new("ja", "JP", "ISO-8859-1"), lang + + assert_equal "ISO-8859-1", 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 @@ -82,8 +102,26 @@ def test_lang assert_equal "eucJP", Locale.charset end + def test_lang_with_ctype + ENV["LC_ALL"] = nil + ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1" + ENV["LC_MESSAGES"] = nil + ENV["LANG"] = "ja_JP.eucJP" + ENV["LANGUAGE"] = nil + + lang = Locale.current[0] + assert_equal Locale::Tag::Posix, lang.class + assert_equal "ja", lang.language + assert_equal "JP", lang.region + assert_equal "ISO-8859-1", lang.charset + assert_equal Locale::Tag::Posix.new("ja", "JP", "ISO-8859-1"), lang + + assert_equal "ISO-8859-1", Locale.charset + end + def test_lang_complex ENV["LC_ALL"] = "zh_CN.UTF-8" # Ignored. + ENV["LC_CTYPE"] = "fr_FR.ISO-8859-1" #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"