Skip to content

Commit 0ef254f

Browse files
committed
fix bugs with charset handling
Fixes #520181, #521169
1 parent 7e496cd commit 0ef254f

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

lib/locale/driver/env.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ module Driver
2424
module Env
2525
module_function
2626

27-
# Gets the locale from environment variable. (LC_ALL > LC_MESSAGES > LANG)
27+
# Gets the locale from environment variable. (LC_ALL > LC_CTYPE > LANG)
2828
# Returns: the locale as Locale::Tag::Posix.
2929
def locale
3030
# At least one environment valiables should be set on *nix system.
31-
[ENV["LC_ALL"], ENV["LC_MESSAGES"], ENV["LANG"]].each do |loc|
31+
[ENV["LC_ALL"], ENV["LC_CTYPE"], ENV["LANG"]].each do |loc|
3232
if loc != nil and loc.size > 0
3333
return Locale::Tag::Posix.parse(loc)
3434
end

lib/locale/tag/posix.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def initialize(language, region = nil, charset = nil, modifier = nil)
3030
end
3131

3232
def self.parse(tag)
33-
if tag =~ /^(C|POSIX)$/
34-
ret = self.new("en", "US")
35-
ret.tag = tag
33+
if tag =~ /\A(C|POSIX)(?:\.([^@]+))?\Z/
34+
ret = self.new("en", "US", $2)
35+
ret.tag = $1
3636
ret
3737
elsif tag =~ TAG_RE
3838
ret = self.new($1, $2, $3, $4)
@@ -47,10 +47,15 @@ def self.parse(tag)
4747
# <language>_<COUNTRY>.<CHARSET>@<MODIFIER>
4848
# (e.g.) "ja_JP.EUC-JP@Modifier"
4949
def to_s
50-
s = @language.dup
51-
s << "_#{@region}" if @region
52-
s << ".#{@charset}" if @charset
53-
s << "@#{@modifier}" if @modifier
50+
if posix?
51+
s = tag.dup
52+
s << ".#{@charset}" if @charset
53+
else
54+
s = @language.dup
55+
s << "_#{@region}" if @region
56+
s << ".#{@charset}" if @charset
57+
s << "@#{@modifier}" if @modifier
58+
end
5459
s
5560
end
5661

@@ -92,6 +97,10 @@ def convert_to(klass)
9297
end
9398
end
9499

100+
def posix?
101+
['POSIX', 'C'].include? tag
102+
end
103+
95104
end
96105
end
97106
end

lib/locale/taglist.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ def script
4646
end
4747
# Returns the top priority charset. (posix)
4848
def charset
49-
if self[0].respond_to? :charset
50-
self[0].charset
51-
else
52-
::Locale.driver_module.charset
53-
end
49+
self[0].respond_to?(:charset) and self[0].charset or ::Locale.driver_module.charset
5450
end
5551
memoize :charset
5652

test/test_detect_general.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class TestDetectGeneral < Test::Unit::TestCase
66
def setup
77
Locale.clear_all
88
ENV["LC_ALL"] = nil
9-
ENV["LC_MESSAGES"] = nil
9+
ENV["LC_CTYPE"] = nil
1010
ENV["LANG"] = nil
1111
ENV["LANGUAGE"] = nil
1212
end
1313

1414
def test_lc_all
1515
ENV["LC_ALL"] = "ja_JP.eucJP"
16-
ENV["LC_MESSAGES"] = "zh_CN.UTF-8" #Ignored.
16+
ENV["LC_CTYPE"] = "zh_CN.UTF-8" #Ignored.
1717
ENV["LANG"] = "ko_KR.UTF-8" #Ignored.
1818
ENV["LANGUAGE"] = nil
1919

@@ -29,7 +29,7 @@ def test_lc_all
2929

3030
def test_lc_messages
3131
ENV["LC_ALL"] = nil
32-
ENV["LC_MESSAGES"] = "ja_JP.eucJP"
32+
ENV["LC_CTYPE"] = "ja_JP.eucJP"
3333
ENV["LANG"] = "ko_KR.UTF-8" #Ignored.
3434
ENV["LANGUAGE"] = nil
3535

@@ -45,7 +45,7 @@ def test_lc_messages
4545

4646
def test_lang
4747
ENV["LC_ALL"] = nil
48-
ENV["LC_MESSAGES"] = nil
48+
ENV["LC_CTYPE"] = nil
4949
ENV["LANG"] = "ja_JP.eucJP"
5050
ENV["LANGUAGE"] = nil
5151

@@ -61,7 +61,7 @@ def test_lang
6161

6262
def test_lang_complex
6363
ENV["LC_ALL"] = "zh_CN.UTF-8" # Ignored.
64-
ENV["LC_MESSAGES"] = "ko_KR.UTF-8" #Ingored.
64+
ENV["LC_CTYPE"] = "ko_KR.UTF-8" #Ingored.
6565
ENV["LANG"] = "en_US.UTF-8" # Ignored.
6666
ENV["LANGUAGE"] ="ja_JP.eucJP:zh_CN.UTF-8"
6767

0 commit comments

Comments
 (0)