Skip to content

Commit

Permalink
Use LC_MESSAGES env for Locale.locale
Browse files Browse the repository at this point in the history
Locale.charset keep using LC_CTYPE.

This change is backward imcompatible change. But the current behavior
is a bug. So this change is reasonable.

GitHub: fix #2

Debian Bug: #690572

Reported by Stefano Zacchiroli. Thanks!!!
Reported by Hleb Valoshka. Thanks!!!
  • Loading branch information
kou committed Sep 19, 2013
1 parent 2f0eeea commit 6916573
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
18 changes: 10 additions & 8 deletions lib/locale/driver/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ 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
end
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"]
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/locale/driver/posix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 41 additions & 6 deletions test/test_detect_general.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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"

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6916573

Please sign in to comment.