locale 2.0.5 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +2 -1
- data/Gemfile +22 -0
- data/README.rdoc +23 -24
- data/Rakefile +44 -82
- data/lib/locale.rb +25 -19
- data/lib/locale/driver.rb +24 -0
- data/lib/locale/driver/cgi.rb +61 -47
- data/lib/locale/driver/env.rb +35 -20
- data/lib/locale/driver/jruby.rb +24 -14
- data/lib/locale/driver/posix.rb +25 -16
- data/lib/locale/driver/win32.rb +33 -20
- data/lib/locale/info/language.rb +5 -1
- data/lib/locale/middleware.rb +38 -0
- data/lib/locale/version.rb +1 -1
- data/locale.gemspec +52 -0
- data/samples/rack/hello_rack.rb +4 -5
- data/samples/rack/locale_rack.rb +11 -0
- data/test/test_detect_cgi.rb +59 -58
- data/test/test_detect_general.rb +80 -5
- data/test/test_driver_jruby.rb +65 -43
- data/test/test_driver_win32.rb +65 -65
- data/test/test_info.rb +1 -0
- data/test/test_locale.rb +35 -0
- metadata +193 -69
- data/setup.rb +0 -1585
data/lib/locale/driver/env.rb
CHANGED
@@ -1,18 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
# Copyright (C) 2012 Hleb Valoshka
|
5
|
+
# Copyright (C) 2008 Masao Mutoh
|
6
|
+
#
|
7
|
+
# Original: Ruby-GetText-Package-1.92.0.
|
8
|
+
# License: Ruby's or LGPL
|
9
|
+
#
|
10
|
+
# This library is free software: you can redistribute it and/or modify
|
11
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
12
|
+
# the Free Software Foundation, either version 3 of the License, or
|
13
|
+
# (at your option) any later version.
|
14
|
+
#
|
15
|
+
# This library is distributed in the hope that it will be useful,
|
16
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
# GNU Lesser General Public License for more details.
|
19
|
+
#
|
20
|
+
# You should have received a copy of the GNU Lesser General Public License
|
21
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
13
22
|
|
14
23
|
require 'locale/tag'
|
15
24
|
require 'locale/taglist'
|
25
|
+
require "locale/driver"
|
16
26
|
|
17
27
|
module Locale
|
18
28
|
module Driver
|
@@ -24,11 +34,11 @@ module Locale
|
|
24
34
|
module Env
|
25
35
|
module_function
|
26
36
|
|
27
|
-
# Gets the locale from environment variable. (LC_ALL >
|
37
|
+
# Gets the locale from environment variable. (LC_ALL > LC_CTYPES > LANG)
|
28
38
|
# Returns: the locale as Locale::Tag::Posix.
|
29
39
|
def locale
|
30
40
|
# At least one environment valiables should be set on *nix system.
|
31
|
-
[ENV["LC_ALL"], ENV["
|
41
|
+
[ENV["LC_ALL"], ENV["LC_CTYPES"], ENV["LANG"]].each do |loc|
|
32
42
|
if loc != nil and loc.size > 0
|
33
43
|
return Locale::Tag::Posix.parse(loc)
|
34
44
|
end
|
@@ -36,16 +46,19 @@ module Locale
|
|
36
46
|
nil
|
37
47
|
end
|
38
48
|
|
39
|
-
# Gets the locales from environment variables. (LANGUAGE > LC_ALL >
|
49
|
+
# Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_CTYPES > LANG)
|
40
50
|
# * Returns: an Array of the locale as Locale::Tag::Posix or nil.
|
41
51
|
def locales
|
42
|
-
|
43
|
-
|
52
|
+
locales = ENV["LANGUAGE"]
|
53
|
+
if (locales != nil and locales.size > 0)
|
54
|
+
locs = locales.split(/:/).collect{|v| Locale::Tag::Posix.parse(v)}.compact
|
55
|
+
if locs.size > 0
|
56
|
+
return Locale::TagList.new(locs)
|
57
|
+
end
|
44
58
|
elsif (loc = locale)
|
45
|
-
Locale::TagList.new([loc])
|
46
|
-
else
|
47
|
-
nil
|
59
|
+
return Locale::TagList.new([loc])
|
48
60
|
end
|
61
|
+
nil
|
49
62
|
end
|
50
63
|
|
51
64
|
# Gets the charset from environment variable or return nil.
|
@@ -59,6 +72,8 @@ module Locale
|
|
59
72
|
end
|
60
73
|
|
61
74
|
end
|
75
|
+
|
76
|
+
MODULES[:env] = Env
|
62
77
|
end
|
63
78
|
end
|
64
79
|
|
data/lib/locale/driver/jruby.rb
CHANGED
@@ -1,19 +1,28 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
# Copyright (C) 2007-2008 Masao Mutoh
|
5
|
+
#
|
6
|
+
# Original: Ruby-GetText-Package-1.92.0.
|
7
|
+
# License: Ruby's or LGPL
|
8
|
+
#
|
9
|
+
# This library is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# This library is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU Lesser General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU Lesser General Public License
|
20
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
3
21
|
|
4
|
-
Copyright (C) 2007,2008 Masao Mutoh
|
5
|
-
|
6
|
-
You may redistribute it and/or modify it under the same
|
7
|
-
license terms as Ruby.
|
8
|
-
|
9
|
-
Original: Ruby-GetText-Package-1.92.0.
|
10
|
-
|
11
|
-
$Id: jruby.rb 27 2008-12-03 15:06:50Z mutoh $
|
12
|
-
=end
|
13
|
-
|
14
|
-
require File.join(File.dirname(__FILE__), 'env')
|
15
22
|
require 'java'
|
16
23
|
|
24
|
+
require "locale/driver/env"
|
25
|
+
|
17
26
|
module Locale
|
18
27
|
module Driver
|
19
28
|
# Locale::Driver::JRuby module for JRuby
|
@@ -47,6 +56,7 @@ module Locale
|
|
47
56
|
charset
|
48
57
|
end
|
49
58
|
end
|
59
|
+
|
60
|
+
MODULES[:jruby] = JRuby
|
50
61
|
end
|
51
|
-
@@locale_driver_module = Driver::JRuby
|
52
62
|
end
|
data/lib/locale/driver/posix.rb
CHANGED
@@ -1,17 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
# Copyright (C) 2002-2008 Masao Mutoh
|
5
|
+
#
|
6
|
+
# Original: Ruby-GetText-Package-1.92.0.
|
7
|
+
# License: Ruby's or LGPL
|
8
|
+
#
|
9
|
+
# This library is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# This library is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU Lesser General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU Lesser General Public License
|
20
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
|
22
|
+
require "locale/driver/env"
|
15
23
|
|
16
24
|
module Locale
|
17
25
|
# Locale::Driver::Posix module for Posix OS (Unix)
|
@@ -22,7 +30,7 @@ module Locale
|
|
22
30
|
$stderr.puts self.name + " is loaded." if $DEBUG
|
23
31
|
|
24
32
|
module_function
|
25
|
-
# Gets the locales from environment variables. (LANGUAGE > LC_ALL >
|
33
|
+
# Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_CTYPES > LANG)
|
26
34
|
# Only LANGUAGE accept plural languages such as "nl_BE;
|
27
35
|
# * Returns: an Array of the locale as Locale::Tag::Posix or nil.
|
28
36
|
def locales
|
@@ -43,7 +51,8 @@ module Locale
|
|
43
51
|
charset
|
44
52
|
end
|
45
53
|
end
|
54
|
+
|
55
|
+
MODULES[:posix] = Posix
|
46
56
|
end
|
47
|
-
@@locale_driver_module = Driver::Posix
|
48
57
|
end
|
49
58
|
|
data/lib/locale/driver/win32.rb
CHANGED
@@ -1,20 +1,28 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
# Copyright (C) 2002-2010 Masao Mutoh
|
5
|
+
#
|
6
|
+
# Original: Ruby-GetText-Package-1.92.0.
|
7
|
+
# License: Ruby's or LGPL
|
8
|
+
#
|
9
|
+
# This library is free software: you can redistribute it and/or modify
|
10
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
+
# the Free Software Foundation, either version 3 of the License, or
|
12
|
+
# (at your option) any later version.
|
13
|
+
#
|
14
|
+
# This library is distributed in the hope that it will be useful,
|
15
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
# GNU Lesser General Public License for more details.
|
18
|
+
#
|
19
|
+
# You should have received a copy of the GNU Lesser General Public License
|
20
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
3
21
|
|
4
|
-
|
5
|
-
|
6
|
-
You may redistribute it and/or modify it under the same
|
7
|
-
license terms as Ruby.
|
8
|
-
|
9
|
-
Original: Ruby-GetText-Package-1.92.0.
|
10
|
-
|
11
|
-
$Id: win32.rb 27 2008-12-03 15:06:50Z mutoh $
|
12
|
-
=end
|
13
|
-
|
14
|
-
require File.join(File.dirname(__FILE__), 'env')
|
15
|
-
require File.join(File.dirname(__FILE__), 'win32_table')
|
16
|
-
require 'dl/win32'
|
22
|
+
require "locale/driver/env"
|
23
|
+
require "locale/driver/win32_table"
|
17
24
|
|
25
|
+
require "dl/import"
|
18
26
|
|
19
27
|
module Locale
|
20
28
|
# Locale::Driver::Win32 module for win32.
|
@@ -22,11 +30,16 @@ module Locale
|
|
22
30
|
# This is a low-level class. Application shouldn't use this directly.
|
23
31
|
module Driver
|
24
32
|
module Win32
|
33
|
+
module Kernel32
|
34
|
+
extend DL::Importer
|
35
|
+
dlload "kernel32.dll"
|
36
|
+
extern "int GetThreadLocale()"
|
37
|
+
end
|
38
|
+
|
25
39
|
include Win32Table
|
26
40
|
|
27
41
|
$stderr.puts self.name + " is loaded." if $DEBUG
|
28
|
-
|
29
|
-
@@win32 = nil
|
42
|
+
|
30
43
|
@@current_locale_id = nil
|
31
44
|
|
32
45
|
module_function
|
@@ -51,8 +64,7 @@ module Locale
|
|
51
64
|
if @@current_locale_id
|
52
65
|
@@current_locale_id
|
53
66
|
else
|
54
|
-
|
55
|
-
@@win32.call
|
67
|
+
Kernel32.GetThreadLocale
|
56
68
|
end
|
57
69
|
end
|
58
70
|
|
@@ -75,7 +87,8 @@ module Locale
|
|
75
87
|
locales
|
76
88
|
end
|
77
89
|
end
|
90
|
+
|
91
|
+
MODULES[:win32] = Win32
|
78
92
|
end
|
79
|
-
@@locale_driver_module = Driver::Win32
|
80
93
|
end
|
81
94
|
|
data/lib/locale/info/language.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
2
|
+
#
|
3
|
+
# License: Ruby's or LGPL
|
4
|
+
#
|
5
|
+
# This library is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require "locale"
|
19
|
+
|
20
|
+
module Locale
|
21
|
+
class Middleware
|
22
|
+
def initialize(application, options={})
|
23
|
+
@application = application
|
24
|
+
@options = options
|
25
|
+
Locale.init(:driver => :cgi)
|
26
|
+
end
|
27
|
+
|
28
|
+
def call(environment)
|
29
|
+
request = Rack::Request.new(environment)
|
30
|
+
Locale.set_request([request["lang"]],
|
31
|
+
[request.cookies["lang"]],
|
32
|
+
environment["HTTP_ACCEPT_LANGUAGE"],
|
33
|
+
environment["HTTP_ACCEPT_CHARSET"])
|
34
|
+
@application.call(environment)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
data/lib/locale/version.rb
CHANGED
data/locale.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
# Copyright (C) 2009 Masao Mutoh
|
5
|
+
#
|
6
|
+
# License: Ruby's or LGPL
|
7
|
+
#
|
8
|
+
# This library is free software: you can redistribute it and/or modify
|
9
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
10
|
+
# the Free Software Foundation, either version 3 of the License, or
|
11
|
+
# (at your option) any later version.
|
12
|
+
#
|
13
|
+
# This library is distributed in the hope that it will be useful,
|
14
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
# GNU Lesser General Public License for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU Lesser General Public License
|
19
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
|
21
|
+
base_dir = File.dirname(__FILE__)
|
22
|
+
$LOAD_PATH.unshift(File.join(base_dir, "lib"))
|
23
|
+
require "locale/version"
|
24
|
+
|
25
|
+
Gem::Specification.new do |s|
|
26
|
+
s.name = "locale"
|
27
|
+
s.version = Locale::VERSION
|
28
|
+
s.summary = 'Ruby-Locale is the pure ruby library which provides basic APIs for localization.'
|
29
|
+
s.description = <<-EOD
|
30
|
+
Ruby-Locale is the pure ruby library which provides basic APIs for localization.
|
31
|
+
EOD
|
32
|
+
s.authors = ["Kouhei Sutou", "Masao Mutoh"]
|
33
|
+
s.email = ["kou@clear-code.com", "mutomasa at gmail.com"]
|
34
|
+
s.homepage = "https://github.com/ruby-gettext/locale"
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
Dir.chdir(base_dir) do
|
37
|
+
s.files = Dir.glob("{lib,samples}/**/*").find_all do |path|
|
38
|
+
File.file?(path)
|
39
|
+
end
|
40
|
+
s.files += ["COPYING", "ChangeLog", "README.rdoc", "Rakefile"]
|
41
|
+
s.files += ["Gemfile", "#{s.name}.gemspec"]
|
42
|
+
s.test_files = Dir.glob("test/test_*.rb")
|
43
|
+
end
|
44
|
+
|
45
|
+
s.add_development_dependency("rake")
|
46
|
+
s.add_development_dependency("bundler")
|
47
|
+
s.add_development_dependency("yard")
|
48
|
+
s.add_development_dependency("redcarpet")
|
49
|
+
s.add_development_dependency("test-unit")
|
50
|
+
s.add_development_dependency("test-unit-notify")
|
51
|
+
s.add_development_dependency("test-unit-rr")
|
52
|
+
end
|
data/samples/rack/hello_rack.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rack'
|
3
|
-
require '
|
3
|
+
require 'locale_rack'
|
4
4
|
|
5
|
-
Locale.init(:driver => :cgi)
|
6
|
-
|
7
5
|
class HelloRackApp
|
6
|
+
include Locale::Rack
|
7
|
+
|
8
8
|
def call(env)
|
9
9
|
req = Rack::Request.new(env)
|
10
|
-
|
11
|
-
env["HTTP_ACCEPT_LANGUAGE"], env["HTTP_ACCEPT_CHARSET"])
|
10
|
+
init_locale(env, req)
|
12
11
|
str = "Language tag candidates of your request order by the priority:\n\n"
|
13
12
|
str += Locale.candidates(:type => :rfc).map{|v| v.inspect + "\n"}.join
|
14
13
|
[200, {"Content-Type" => "text/plain", "Content-Length" => str.length.to_s}, [str]]
|
data/test/test_detect_cgi.rb
CHANGED
@@ -1,55 +1,61 @@
|
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
|
4
|
+
# Copyright (C) 2009-2010 Masao Mutoh
|
5
|
+
#
|
6
|
+
# License: Ruby's or LGPL
|
7
|
+
#
|
8
|
+
# This library is free software: you can redistribute it and/or modify
|
9
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
10
|
+
# the Free Software Foundation, either version 3 of the License, or
|
11
|
+
# (at your option) any later version.
|
12
|
+
#
|
13
|
+
# This library is distributed in the hope that it will be useful,
|
14
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
# GNU Lesser General Public License for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU Lesser General Public License
|
19
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
|
1
21
|
require 'cgi'
|
2
22
|
require 'locale'
|
3
23
|
require 'test/unit'
|
4
24
|
|
5
|
-
class CGI
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def readlines=(str)
|
10
|
-
@@lines = [str]
|
11
|
-
end
|
12
|
-
def readlines
|
13
|
-
@@lines
|
14
|
-
end
|
15
|
-
def read_from_cmdline
|
16
|
-
require "shellwords"
|
17
|
-
string = readlines.join(' ').gsub(/\n/n, '').gsub(/\\=/n, '%3D').gsub(/\\&/n, '%26')
|
18
|
-
|
19
|
-
words = Shellwords.shellwords(string)
|
20
|
-
|
21
|
-
if words.find{|x| /=/n.match(x) }
|
22
|
-
words.join('&')
|
23
|
-
else
|
24
|
-
words.join('+')
|
25
|
-
end
|
26
|
-
end
|
27
|
-
private :read_from_cmdline
|
25
|
+
class CustomizableCGI < CGI
|
26
|
+
def initialize(options={})
|
27
|
+
yield(self)
|
28
|
+
super(options)
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
class TestDetectCGI < Test::Unit::TestCase
|
32
|
-
def setup_cgi(
|
33
|
-
CGI::QueryExtension.readlines = str
|
33
|
+
def setup_cgi(env={})
|
34
34
|
Locale.init(:driver => :cgi)
|
35
|
-
cgi =
|
35
|
+
cgi = CustomizableCGI.new do |_cgi|
|
36
|
+
default_env = {
|
37
|
+
"REQUEST_METHOD" => "GET",
|
38
|
+
}
|
39
|
+
env = default_env.merge(env)
|
40
|
+
stub(_cgi).env_table {env}
|
41
|
+
end
|
36
42
|
Locale.cgi = cgi
|
37
43
|
Locale.clear_all
|
38
44
|
end
|
39
45
|
|
40
46
|
def test_query_string
|
41
47
|
#query string
|
42
|
-
setup_cgi("lang=ja_JP")
|
48
|
+
setup_cgi("QUERY_STRING" => "lang=ja_JP")
|
43
49
|
lang = Locale.current[0]
|
44
50
|
assert_equal(Locale::Tag::Simple, lang.class)
|
45
51
|
assert_equal("ja_JP", lang.to_s)
|
46
52
|
|
47
|
-
setup_cgi("lang=ja-jp")
|
53
|
+
setup_cgi("QUERY_STRING" => "lang=ja-jp")
|
48
54
|
lang = Locale.current[0]
|
49
55
|
assert_equal(Locale::Tag::Simple, lang.class)
|
50
56
|
assert_equal("ja_JP", lang.to_s)
|
51
57
|
assert_equal("ja-JP", lang.to_rfc.to_s)
|
52
|
-
setup_cgi("lang=ja-jp")
|
58
|
+
setup_cgi("QUERY_STRING" => "lang=ja-jp")
|
53
59
|
assert_equal("ja_JP", lang.to_s)
|
54
60
|
assert_equal("ja-JP", lang.to_rfc.to_s)
|
55
61
|
|
@@ -57,64 +63,55 @@ class TestDetectCGI < Test::Unit::TestCase
|
|
57
63
|
|
58
64
|
def test_cookie
|
59
65
|
#cockie
|
60
|
-
setup_cgi("
|
66
|
+
setup_cgi("HTTP_COOKIE" => "lang=en-us")
|
61
67
|
assert_equal("en_US", Locale.current.to_s)
|
62
68
|
end
|
63
69
|
|
64
70
|
def test_accept_language
|
65
|
-
|
66
|
-
|
67
|
-
setup_cgi("")
|
71
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "",
|
72
|
+
"HTTP_ACCEPT_CHARSET" => "")
|
68
73
|
lang = Locale.current[0]
|
69
74
|
assert_equal(Locale::Tag::Simple, lang.class)
|
70
75
|
assert_equal("en", lang.to_s)
|
71
76
|
assert_equal("en", lang.to_rfc.to_s)
|
72
77
|
|
73
|
-
|
74
|
-
setup_cgi("")
|
78
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "ja,en-us;q=0.7,en;q=0.3")
|
75
79
|
lang1, lang2, lang3 = Locale.current
|
76
80
|
assert_equal("ja", lang1.to_rfc.to_s)
|
77
81
|
assert_equal("en-US", lang2.to_rfc.to_s)
|
78
82
|
assert_equal("en", lang3.to_rfc.to_s)
|
79
83
|
|
80
|
-
|
81
|
-
setup_cgi("")
|
84
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "en-us,ja;q=0.7,en;q=0.3")
|
82
85
|
lang1, lang2, lang3 = Locale.current
|
83
86
|
assert_equal("en-US", lang1.to_rfc.to_s)
|
84
87
|
assert_equal("ja", lang2.to_rfc.to_s)
|
85
88
|
assert_equal("en", lang3.to_rfc.to_s)
|
86
89
|
|
87
|
-
|
88
|
-
setup_cgi("")
|
90
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "en")
|
89
91
|
lang = Locale.current[0]
|
90
92
|
assert_equal("en", lang.to_rfc.to_s)
|
91
93
|
end
|
92
94
|
|
93
95
|
def test_accept_charset
|
94
96
|
#accept charset
|
95
|
-
|
96
|
-
setup_cgi("")
|
97
|
+
setup_cgi("HTTP_ACCEPT_CHARSET" => "Shift_JIS")
|
97
98
|
assert_equal("Shift_JIS", Locale.charset)
|
98
99
|
|
99
|
-
|
100
|
-
setup_cgi("")
|
100
|
+
setup_cgi("HTTP_ACCEPT_CHARSET" => "EUC-JP,*,utf-8")
|
101
101
|
assert_equal("EUC-JP", Locale.charset)
|
102
102
|
|
103
|
-
|
104
|
-
setup_cgi("")
|
103
|
+
setup_cgi("HTTP_ACCEPT_CHARSET" => "*")
|
105
104
|
assert_equal("UTF-8", Locale.charset)
|
106
105
|
|
107
|
-
|
108
|
-
setup_cgi("")
|
106
|
+
setup_cgi("HTTP_ACCEPT_CHARSET" => "")
|
109
107
|
assert_equal("UTF-8", Locale.charset)
|
110
108
|
end
|
111
109
|
|
112
110
|
def test_default
|
113
111
|
Locale.set_default(nil)
|
114
112
|
Locale.set_default("ja-JP")
|
115
|
-
|
116
|
-
|
117
|
-
setup_cgi("")
|
113
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "",
|
114
|
+
"HTTP_ACCEPT_CHARSET" => "")
|
118
115
|
assert_equal("ja-JP", Locale.default.to_rfc.to_s)
|
119
116
|
assert_equal("ja-JP", Locale.current.to_rfc.to_s)
|
120
117
|
Locale.set_default(nil)
|
@@ -138,8 +135,7 @@ class TestDetectCGI < Test::Unit::TestCase
|
|
138
135
|
|
139
136
|
def test_candidates
|
140
137
|
|
141
|
-
|
142
|
-
setup_cgi("")
|
138
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")
|
143
139
|
|
144
140
|
assert_equal common("fr-FR", "zh-CN", "zh-TW", "ja-JP",
|
145
141
|
"fr", "zh", "ja", "en"), Locale.candidates
|
@@ -161,8 +157,7 @@ class TestDetectCGI < Test::Unit::TestCase
|
|
161
157
|
end
|
162
158
|
|
163
159
|
def test_candidates_with_supported_language_tags
|
164
|
-
|
165
|
-
setup_cgi("")
|
160
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")
|
166
161
|
|
167
162
|
assert_equal common("fr_FR", "zh", "ja"), Locale.candidates(:type => :common,
|
168
163
|
:supported_language_tags => ["fr_FR", "ja", "zh"])
|
@@ -177,8 +172,7 @@ class TestDetectCGI < Test::Unit::TestCase
|
|
177
172
|
end
|
178
173
|
|
179
174
|
def test_candidates_with_default
|
180
|
-
|
181
|
-
setup_cgi("")
|
175
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")
|
182
176
|
|
183
177
|
Locale.default = "zh_TW"
|
184
178
|
assert_equal simple("fr-FR", "zh", "ja"),
|
@@ -200,8 +194,7 @@ class TestDetectCGI < Test::Unit::TestCase
|
|
200
194
|
def test_candidates_with_app_language_tags
|
201
195
|
Locale.set_app_language_tags("fr-FR", "ja")
|
202
196
|
|
203
|
-
|
204
|
-
setup_cgi("")
|
197
|
+
setup_cgi("HTTP_ACCEPT_LANGUAGE" => "fr-fr,zh_CN;q=0.7,zh_TW;q=0.2,ja_JP;q=0.1")
|
205
198
|
|
206
199
|
assert_equal common("fr-FR", "ja"), Locale.candidates
|
207
200
|
|
@@ -213,4 +206,12 @@ class TestDetectCGI < Test::Unit::TestCase
|
|
213
206
|
Locale.default = "en"
|
214
207
|
Locale.set_app_language_tags(nil)
|
215
208
|
end
|
209
|
+
|
210
|
+
def test_request
|
211
|
+
Locale.set_request(["ja"], [""], "", "")
|
212
|
+
assert_equal common("ja", "en"), Locale.candidates
|
213
|
+
|
214
|
+
Locale.set_request(["en"], [""], "", "")
|
215
|
+
assert_equal common("en"), Locale.candidates #Cache should be cleared.
|
216
|
+
end
|
216
217
|
end
|