Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ module Locale

module_function
def require_driver(name) #:nodoc:
require File.join(ROOT, "locale/driver", name.to_s)
require File.join(ROOT, "locale/driver", name.to_s).untaint
end

def create_language_tag(tag) #:nodoc:
if tag
if tag.kind_of? Locale::Tag::Simple
tag
elsif tag.kind_of? Locale::TagList
tag[0]
else
Locale::Tag.parse(tag)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/locale/driver/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ module Driver
module Env
module_function

# Gets the locale from environment variable. (LC_ALL > LC_MESSAGES > LANG)
# Gets the locale from environment variable. (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|
[ENV["LC_ALL"], ENV["LC_CTYPE"], ENV["LANG"]].each do |loc|
if loc != nil and loc.size > 0
return Locale::Tag::Posix.parse(loc)
end
Expand Down
23 changes: 16 additions & 7 deletions lib/locale/tag/posix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def initialize(language, region = nil, charset = nil, modifier = nil)
end

def self.parse(tag)
if tag =~ /^(C|POSIX)$/
ret = self.new("en", "US")
ret.tag = tag
if tag =~ /\A(C|POSIX)(?:\.([^@]+))?\Z/
ret = self.new("en", "US", $2)
ret.tag = $1
ret
elsif tag =~ TAG_RE
ret = self.new($1, $2, $3, $4)
Expand All @@ -47,10 +47,15 @@ def self.parse(tag)
# <language>_<COUNTRY>.<CHARSET>@<MODIFIER>
# (e.g.) "ja_JP.EUC-JP@Modifier"
def to_s
s = @language.dup
s << "_#{@region}" if @region
s << ".#{@charset}" if @charset
s << "@#{@modifier}" if @modifier
if posix?
s = tag.dup
s << ".#{@charset}" if @charset
else
s = @language.dup
s << "_#{@region}" if @region
s << ".#{@charset}" if @charset
s << "@#{@modifier}" if @modifier
end
s
end

Expand Down Expand Up @@ -92,6 +97,10 @@ def convert_to(klass)
end
end

def posix?
['POSIX', 'C'].include? tag
end

end
end
end
6 changes: 1 addition & 5 deletions lib/locale/taglist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ def script
end
# Returns the top priority charset. (posix)
def charset
if self[0].respond_to? :charset
self[0].charset
else
::Locale.driver_module.charset
end
self[0].respond_to?(:charset) and self[0].charset or ::Locale.driver_module.charset
end
memoize :charset

Expand Down
10 changes: 5 additions & 5 deletions test/test_detect_general.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class TestDetectGeneral < Test::Unit::TestCase
def setup
Locale.clear_all
ENV["LC_ALL"] = nil
ENV["LC_MESSAGES"] = nil
ENV["LC_CTYPE"] = nil
ENV["LANG"] = nil
ENV["LANGUAGE"] = nil
end

def test_lc_all
ENV["LC_ALL"] = "ja_JP.eucJP"
ENV["LC_MESSAGES"] = "zh_CN.UTF-8" #Ignored.
ENV["LC_CTYPE"] = "zh_CN.UTF-8" #Ignored.
ENV["LANG"] = "ko_KR.UTF-8" #Ignored.
ENV["LANGUAGE"] = nil

Expand All @@ -29,7 +29,7 @@ def test_lc_all

def test_lc_messages
ENV["LC_ALL"] = nil
ENV["LC_MESSAGES"] = "ja_JP.eucJP"
ENV["LC_CTYPE"] = "ja_JP.eucJP"
ENV["LANG"] = "ko_KR.UTF-8" #Ignored.
ENV["LANGUAGE"] = nil

Expand All @@ -45,7 +45,7 @@ def test_lc_messages

def test_lang
ENV["LC_ALL"] = nil
ENV["LC_MESSAGES"] = nil
ENV["LC_CTYPE"] = nil
ENV["LANG"] = "ja_JP.eucJP"
ENV["LANGUAGE"] = nil

Expand All @@ -61,7 +61,7 @@ def test_lang

def test_lang_complex
ENV["LC_ALL"] = "zh_CN.UTF-8" # Ignored.
ENV["LC_MESSAGES"] = "ko_KR.UTF-8" #Ingored.
ENV["LC_CTYPE"] = "ko_KR.UTF-8" #Ingored.
ENV["LANG"] = "en_US.UTF-8" # Ignored.
ENV["LANGUAGE"] ="ja_JP.eucJP:zh_CN.UTF-8"

Expand Down
2 changes: 1 addition & 1 deletion test/test_driver_jruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup
end

def set_locale(tag)
java.util.Locale.setDefault(java.util.Locale.new(tag.language, tag.region, tag.variants.to_s))
java.util.Locale.setDefault(java.util.Locale.new(tag.language, tag.region, tag.variants.join('_')))
end

def test_charset
Expand Down