Skip to content

Commit

Permalink
Use LC_CTYPE env for Locale.current[0].charset
Browse files Browse the repository at this point in the history
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!!!
  • Loading branch information
kou committed Sep 20, 2013
1 parent b4b556e commit 0e693ce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
24 changes: 16 additions & 8 deletions lib/locale/driver/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 39 additions & 1 deletion test/test_detect_general.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 0e693ce

Please sign in to comment.