locale 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +55 -0
- data/ChangeLog +113 -0
- data/README +100 -0
- data/Rakefile +77 -0
- data/doc/classes/Locale/Driver.html +375 -0
- data/doc/classes/Locale/Driver/CGI.html +502 -0
- data/doc/classes/Locale/Driver/Env.html +440 -0
- data/doc/classes/Locale/Driver/JRuby.html +390 -0
- data/doc/classes/Locale/Driver/Posix.html +431 -0
- data/doc/classes/Locale/Driver/Win32.html +398 -0
- data/doc/classes/Locale/Driver/Win32Table.html +356 -0
- data/doc/classes/Locale/Info.html +586 -0
- data/doc/classes/Locale/Info/Language.html +764 -0
- data/doc/classes/Locale/Info/Region.html +470 -0
- data/doc/classes/Locale/Tag/Cldr.html +547 -0
- data/doc/classes/Locale/Tag/Common.html +655 -0
- data/doc/classes/Locale/Tag/Illegular.html +424 -0
- data/doc/classes/Locale/Tag/Posix.html +612 -0
- data/doc/classes/Locale/Tag/Rfc.html +610 -0
- data/doc/classes/Locale/Tag/Simple.html +684 -0
- data/doc/classes/Locale/TagList.html +783 -0
- data/doc/classes/Locale/Util.html +476 -0
- data/doc/classes/Locale/Util/Memoizable.html +356 -0
- data/doc/created.rid +1 -0
- data/doc/files/ChangeLog.html +497 -0
- data/doc/files/README.html +525 -0
- data/doc/files/lib/locale/driver/cgi_rb.html +342 -0
- data/doc/files/lib/locale/driver/env_rb.html +354 -0
- data/doc/files/lib/locale/driver/jruby_rb.html +359 -0
- data/doc/files/lib/locale/driver/posix_rb.html +349 -0
- data/doc/files/lib/locale/driver/win32_rb.html +359 -0
- data/doc/files/lib/locale/driver/win32_table_rb.html +342 -0
- data/doc/files/lib/locale/info/language_rb.html +353 -0
- data/doc/files/lib/locale/info/region_rb.html +353 -0
- data/doc/files/lib/locale/info_rb.html +354 -0
- data/doc/files/lib/locale/tag/cldr_rb.html +342 -0
- data/doc/files/lib/locale/tag/common_rb.html +342 -0
- data/doc/files/lib/locale/tag/illegular_rb.html +349 -0
- data/doc/files/lib/locale/tag/posix_rb.html +342 -0
- data/doc/files/lib/locale/tag/rfc_rb.html +342 -0
- data/doc/files/lib/locale/tag/simple_rb.html +349 -0
- data/doc/files/lib/locale/tag_rb.html +374 -0
- data/doc/files/lib/locale/taglist_rb.html +342 -0
- data/doc/files/lib/locale/util/memoizable_rb.html +360 -0
- data/doc/files/lib/locale/version_rb.html +342 -0
- data/doc/files/lib/locale_rb.html +379 -0
- data/doc/fr_class_index.html +20 -0
- data/doc/fr_file_index.html +23 -0
- data/doc/fr_method_index.html +80 -0
- data/doc/index.html +1 -0
- data/doc/rdoc-style.css +320 -0
- data/lib/locale.rb +248 -0
- data/lib/locale/data/languages.tab.gz +0 -0
- data/lib/locale/data/regions.tab.gz +0 -0
- data/lib/locale/driver/cgi.rb +132 -0
- data/lib/locale/driver/env.rb +64 -0
- data/lib/locale/driver/jruby.rb +53 -0
- data/lib/locale/driver/posix.rb +49 -0
- data/lib/locale/driver/win32.rb +61 -0
- data/lib/locale/driver/win32_table.rb +298 -0
- data/lib/locale/info.rb +12 -0
- data/lib/locale/info/language.rb +134 -0
- data/lib/locale/info/region.rb +74 -0
- data/lib/locale/tag.rb +36 -0
- data/lib/locale/tag/cldr.rb +93 -0
- data/lib/locale/tag/common.rb +122 -0
- data/lib/locale/tag/illegular.rb +38 -0
- data/lib/locale/tag/posix.rb +97 -0
- data/lib/locale/tag/rfc.rb +106 -0
- data/lib/locale/tag/simple.rb +145 -0
- data/lib/locale/taglist.rb +93 -0
- data/lib/locale/util/memoizable.rb +76 -0
- data/lib/locale/version.rb +12 -0
- data/samples/cgi/README +20 -0
- data/samples/cgi/cookie.cgi +62 -0
- data/samples/cgi/http.rb +52 -0
- data/samples/cgi/index.cgi +90 -0
- data/samples/cgi/locale.css +115 -0
- data/samples/sample_1.rb +25 -0
- data/samples/sample_info.rb +6 -0
- data/setup.rb +1585 -0
- data/test/test_detect_cgi.rb +179 -0
- data/test/test_detect_general.rb +159 -0
- data/test/test_info.rb +28 -0
- data/test/test_tag.rb +1183 -0
- data/test/test_thread.rb +37 -0
- metadata +162 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
# Refer from activesupport-2.2.0.
|
2
|
+
# * Remove the dependecies to activesupport.
|
3
|
+
# * change the key to hash value of args.
|
4
|
+
# * Not Thread safe
|
5
|
+
# * Add the clear method.
|
6
|
+
module Locale
|
7
|
+
module Util
|
8
|
+
module Memoizable
|
9
|
+
MEMOIZED_IVAR = Proc.new do |symbol|
|
10
|
+
"#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
mod = self
|
15
|
+
base.class_eval do
|
16
|
+
extend mod
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
alias :freeze_without_memoizable :freeze #:nodoc:
|
21
|
+
def freeze #:nodoc:
|
22
|
+
unless frozen?
|
23
|
+
@_memoized_ivars = {}
|
24
|
+
freeze_without_memoizable
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Clear memoized values.
|
29
|
+
def clear
|
30
|
+
@_memoized_ivars = {}
|
31
|
+
end
|
32
|
+
|
33
|
+
# Cache the result of the methods.
|
34
|
+
#
|
35
|
+
# include Memoizable
|
36
|
+
# def foo
|
37
|
+
# ......
|
38
|
+
# end
|
39
|
+
# def bar(a, b)
|
40
|
+
# ......
|
41
|
+
# end
|
42
|
+
# memoize :foo, :bar(a, b)
|
43
|
+
#
|
44
|
+
# To clear cache, #clear_foo, #clear_bar is also defined.
|
45
|
+
#
|
46
|
+
# (NOTE) Consider to use this with huge objects to avoid memory leaks.
|
47
|
+
def memoize(*symbols)
|
48
|
+
symbols.each do |symbol|
|
49
|
+
original_method = "_unmemoized_#{symbol}"
|
50
|
+
memoized_ivar = MEMOIZED_IVAR.call(symbol)
|
51
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
52
|
+
raise "Already memoized #{symbol}" if method_defined?(:#{original_method})
|
53
|
+
alias #{original_method} #{symbol}
|
54
|
+
|
55
|
+
def #{symbol}(*args)
|
56
|
+
@_memoized_ivars ||= {}
|
57
|
+
@_memoized_ivars[:#{memoized_ivar}] ||= {}
|
58
|
+
|
59
|
+
key = args.hash
|
60
|
+
|
61
|
+
ret = @_memoized_ivars[:#{memoized_ivar}][key]
|
62
|
+
|
63
|
+
if ret
|
64
|
+
ret
|
65
|
+
else
|
66
|
+
@_memoized_ivars[:#{memoized_ivar}][key] = #{original_method}(*args).freeze
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
EOS
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/samples/cgi/README
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Sample script for CGI/ERB and Ruby/Locale.
|
2
|
+
|
3
|
+
Run the http server.
|
4
|
+
$ ruby http.rb
|
5
|
+
|
6
|
+
Access the http server from WWW browser:
|
7
|
+
|
8
|
+
http://localhost:10080/
|
9
|
+
|
10
|
+
or
|
11
|
+
|
12
|
+
If you want to set locale(lang) forcely, then:
|
13
|
+
|
14
|
+
http://localhost:10080/?lang=ja
|
15
|
+
#ja is a target locale in this sample.
|
16
|
+
|
17
|
+
http.rb - an http server for samples using WEBrick
|
18
|
+
index.cgi - a sample menu (CGI sample)
|
19
|
+
cookie.cgi - Set lang to cookie value.
|
20
|
+
locale.css - CSS file.
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
=begin
|
3
|
+
cookie.cgi - Set a selected locale to the cookie of WWW Browser.
|
4
|
+
|
5
|
+
Set UTF-8 forcely as output charset.
|
6
|
+
|
7
|
+
Recommanded to set UTF-8 forcely because some web browser
|
8
|
+
doesn't send HTTP_ACCEPT_CHARSET correctly.
|
9
|
+
|
10
|
+
Copyright (C) 2008 Masao Mutoh
|
11
|
+
|
12
|
+
You may redistribute it and/or modify it under the same
|
13
|
+
license terms as Ruby.
|
14
|
+
=end
|
15
|
+
|
16
|
+
$:<< "../../lib"
|
17
|
+
|
18
|
+
require 'locale'
|
19
|
+
require 'cgi'
|
20
|
+
|
21
|
+
# Initialization of Locale library.
|
22
|
+
Locale.init(:driver => :cgi)
|
23
|
+
cgi = CGI.new
|
24
|
+
Locale.set_cgi(cgi)
|
25
|
+
|
26
|
+
# Get "lang" from the query string.
|
27
|
+
lang = cgi["lang"] or ""
|
28
|
+
|
29
|
+
#
|
30
|
+
# CGI part
|
31
|
+
#
|
32
|
+
print "Set-Cookie:lang=#{lang}\n"
|
33
|
+
print "Content-type:text/html; charset=UTF-8\n\n"
|
34
|
+
|
35
|
+
puts %Q[
|
36
|
+
<html>
|
37
|
+
<head>
|
38
|
+
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
39
|
+
<link rel="stylesheet" type="text/css" href="locale.css" media="all">
|
40
|
+
<title>Sample script for CGI and Ruby/Locale</title>
|
41
|
+
</head>
|
42
|
+
<body>
|
43
|
+
|
44
|
+
<div style="text-align:center; margin:5em;">
|
45
|
+
]
|
46
|
+
|
47
|
+
if lang.size > 0
|
48
|
+
puts %Q!<p><div id="result">Set [#{lang}] as the cookie of your WWW browser.</div></p>!
|
49
|
+
else
|
50
|
+
puts %Q!<p><div id="result">Clear "lang" value from your WWW browser.</div></p>!
|
51
|
+
end
|
52
|
+
|
53
|
+
puts %Q!
|
54
|
+
<p style="margin:2em;"><a href="/">Back</a></p>
|
55
|
+
</div>
|
56
|
+
<div style="text-align:right">
|
57
|
+
<p>Copyright (C) 2008 Masao Mutoh</p>
|
58
|
+
</div>
|
59
|
+
</body>
|
60
|
+
</html>
|
61
|
+
!
|
62
|
+
|
data/samples/cgi/http.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
=begin
|
3
|
+
http.rb - An WebServer for hello locale sample.
|
4
|
+
|
5
|
+
Copyright (C) 2005-2008 Masao Mutoh
|
6
|
+
|
7
|
+
You may redistribute it and/or modify it under the same
|
8
|
+
license terms as Ruby.
|
9
|
+
|
10
|
+
Original: Ruby-GetText-Package 1.92.0
|
11
|
+
|
12
|
+
$Id$
|
13
|
+
=end
|
14
|
+
|
15
|
+
require 'webrick'
|
16
|
+
require 'cgi'
|
17
|
+
require 'rbconfig'
|
18
|
+
|
19
|
+
interpreter = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) +
|
20
|
+
Config::CONFIG['EXEEXT']
|
21
|
+
|
22
|
+
srv = WEBrick::HTTPServer.new({:BindAddress => '127.0.0.1',
|
23
|
+
:Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
|
24
|
+
# :CGIInterpreter => "ruby -d",
|
25
|
+
:Port => 10080})
|
26
|
+
|
27
|
+
['INT', 'TERM'].each { |signal|
|
28
|
+
trap(signal){ srv.shutdown}
|
29
|
+
}
|
30
|
+
|
31
|
+
srv.mount("/", WEBrick::HTTPServlet::FileHandler, File.expand_path('.'))
|
32
|
+
|
33
|
+
srv.mount_proc("/src/") do |req, res|
|
34
|
+
res.header["Content-Type"] = "text/html; charset=UTF-8"
|
35
|
+
if req.query_string
|
36
|
+
file = File.open(req.query_string).read
|
37
|
+
res.body = %Q[<html>
|
38
|
+
<head>
|
39
|
+
<title>View a source code</title>
|
40
|
+
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
41
|
+
<link rel="stylesheet" type="text/css" href="/gettext.css" media="all">
|
42
|
+
</head>
|
43
|
+
<body><h1>#{req.query_string}</h1>
|
44
|
+
<pre>#{CGI.escapeHTML(file)}</pre>
|
45
|
+
<p><a href="/">Back</a></p>
|
46
|
+
</body>
|
47
|
+
</html>
|
48
|
+
]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
srv.start
|
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
=begin
|
3
|
+
index.cgi - Sample script for CGI
|
4
|
+
|
5
|
+
Copyright (C) 2008 Masao Mutoh
|
6
|
+
|
7
|
+
You may redistribute it and/or modify it under the same
|
8
|
+
license terms as Ruby.
|
9
|
+
=end
|
10
|
+
$:<< "../../lib"
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'rubygems'
|
14
|
+
rescue LoadError
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'locale'
|
18
|
+
require 'cgi'
|
19
|
+
|
20
|
+
# Initialize Locale library.
|
21
|
+
Locale.init(:driver => :cgi)
|
22
|
+
cgi = CGI.new
|
23
|
+
Locale.set_cgi(cgi)
|
24
|
+
|
25
|
+
|
26
|
+
url_base = "http://#{cgi.server_name}:#{cgi.server_port}/"
|
27
|
+
|
28
|
+
sample_locales = ["en-US", "fr-FR", "ja-JP"]
|
29
|
+
|
30
|
+
langs = Locale.candidates(:type => :common) #Try :rfc, :simple and others.
|
31
|
+
|
32
|
+
|
33
|
+
#
|
34
|
+
# CGI part
|
35
|
+
#
|
36
|
+
|
37
|
+
print "Content-type:text/html; charset=UTF-8\n\n"
|
38
|
+
|
39
|
+
puts %Q[<html><head>
|
40
|
+
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
41
|
+
<link rel="stylesheet" type="text/css" href="locale.css" media="all">
|
42
|
+
<title>Sample script for CGI/ERB and Ruby/Locale</title>
|
43
|
+
</head>
|
44
|
+
<body>
|
45
|
+
<h1>Ruby/Locale CGI sample scripts</h1>
|
46
|
+
<p>Requested Locales order by the priority:</p>
|
47
|
+
<div id="result">#{CGI.escapeHTML(langs.inspect)}</div>
|
48
|
+
|
49
|
+
<h2>Auto-Detect the locale from the WWW browser</h2>
|
50
|
+
<p><a href="./">#{url_base}</a></p>
|
51
|
+
|
52
|
+
<h2>Set locale as the "lang" parameter</h2>
|
53
|
+
<ol>
|
54
|
+
]
|
55
|
+
sample_locales.each do |lang|
|
56
|
+
url = url_base + "?lang=" + lang
|
57
|
+
puts %Q!<li> <a href="#{url}">#{CGI.escapeHTML(url)}</a> [#{lang}]</li>!
|
58
|
+
end
|
59
|
+
url = url_base + "?lang=zh_CN;lang=ko_KR"
|
60
|
+
puts %Q!<li> <a href="#{url}">#{CGI.escapeHTML(url)}</a> [Plural locales]</li>!
|
61
|
+
|
62
|
+
puts "</ol>"
|
63
|
+
puts %Q[<h2>Set "lang" as the cookie value.</h2>
|
64
|
+
<p>Click one of the link below, and then click "Auto-Detect the locale from the WWW browser".</p>
|
65
|
+
<ol>]
|
66
|
+
|
67
|
+
sample_locales.each do |lang|
|
68
|
+
url = url_base + "cookie.cgi?lang=" + lang
|
69
|
+
puts %Q!<li><a href="#{url}">#{CGI.escapeHTML(url)}</a> [#{lang}]</li>!
|
70
|
+
end
|
71
|
+
puts %Q!<li><a href="cookie.cgi">Clear the cookie value</a></li>!
|
72
|
+
|
73
|
+
puts "</ol>"
|
74
|
+
puts "<h2>Source codes</h2>"
|
75
|
+
puts "<ol>"
|
76
|
+
|
77
|
+
Dir.glob("*cgi\0*rb)").sort.each do |src|
|
78
|
+
unless /http.rb|makemo.rb/ =~ src
|
79
|
+
puts %Q[<li><a href="/src/?#{src}">#{src}</a></li>]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
puts %Q[</ol>
|
83
|
+
<hr/>
|
84
|
+
<div class="copyright">
|
85
|
+
<p>Copyright (C) 2008 Masao Mutoh</p>
|
86
|
+
</div>
|
87
|
+
</body>
|
88
|
+
</html>
|
89
|
+
]
|
90
|
+
|
@@ -0,0 +1,115 @@
|
|
1
|
+
/*
|
2
|
+
CSS for Ruby/Locale
|
3
|
+
by Masao Mutoh.
|
4
|
+
*/
|
5
|
+
|
6
|
+
body {
|
7
|
+
margin: 0px;
|
8
|
+
padding: 0px;
|
9
|
+
color: #000;
|
10
|
+
background-color: #fff;
|
11
|
+
font-size: 90%;
|
12
|
+
font-family: Times, Sans-Serif;
|
13
|
+
line-height: 1.5em;
|
14
|
+
}
|
15
|
+
|
16
|
+
h1 {
|
17
|
+
color: #002288;
|
18
|
+
text-align: left;
|
19
|
+
clear: both;
|
20
|
+
padding:0.4em;
|
21
|
+
font-weight: bold;
|
22
|
+
font-size: 1.8em;
|
23
|
+
padding-left:0.5em;
|
24
|
+
padding-right:10px;
|
25
|
+
margin-top: 10px;
|
26
|
+
margin-left: 20px;
|
27
|
+
margin-bottom:1em;
|
28
|
+
margin-right:5px;
|
29
|
+
border: solid thin;
|
30
|
+
border-left: solid;
|
31
|
+
border-color: #9999ff;
|
32
|
+
border-top-width: 0px;
|
33
|
+
border-right-width: 0px;
|
34
|
+
border-bottom-width: 1px;
|
35
|
+
border-left-width: 15px;
|
36
|
+
}
|
37
|
+
h2 {
|
38
|
+
margin: 0px;
|
39
|
+
font-size: large;
|
40
|
+
font-weight: bold;
|
41
|
+
font-size: 1.4em;
|
42
|
+
padding: 0.3em;
|
43
|
+
padding-left:0.5em;
|
44
|
+
padding-right:10px;
|
45
|
+
margin-top: 1em;
|
46
|
+
margin-left: 1.5em;
|
47
|
+
margin-right: 10px;
|
48
|
+
margin-bottom:0.8em;
|
49
|
+
border: solid thin;
|
50
|
+
border-left: solid;
|
51
|
+
border-color: #9999ff;
|
52
|
+
border-top-width: 0px;
|
53
|
+
border-right-width: 0px;
|
54
|
+
border-bottom-width: 1px;
|
55
|
+
border-left-width: 15px;
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
h3 {
|
60
|
+
margin:1em;
|
61
|
+
margin-bottom: 0.5em;
|
62
|
+
margin-top: 1.5em;
|
63
|
+
margin-left: 2em;
|
64
|
+
margin-right: 24px;
|
65
|
+
padding: 0.1em;
|
66
|
+
padding-top: 0;
|
67
|
+
padding-left: 0.7em;
|
68
|
+
font-size: 1.2em;
|
69
|
+
border-style: dashed;
|
70
|
+
border-width: 0px 0px 1px 1px;
|
71
|
+
border-color: #ddddff;
|
72
|
+
}
|
73
|
+
|
74
|
+
p {
|
75
|
+
padding-left: 1em;
|
76
|
+
padding-right: 1em;
|
77
|
+
margin-top: 0px;
|
78
|
+
margin-left: 1.5em;
|
79
|
+
margin-right: 1em;
|
80
|
+
margin-bottom: 0.5em;
|
81
|
+
}
|
82
|
+
|
83
|
+
ol, ul {
|
84
|
+
margin-top: 0.2em;
|
85
|
+
margin-left: 3em;
|
86
|
+
padding-left: 1.5em;
|
87
|
+
}
|
88
|
+
|
89
|
+
li {
|
90
|
+
margin-top: 0.2em;
|
91
|
+
margin-left: 0em;
|
92
|
+
padding-left: 0em;
|
93
|
+
line-height: 1.2em;
|
94
|
+
}
|
95
|
+
|
96
|
+
#result {
|
97
|
+
color: red;
|
98
|
+
font-weight: bold;
|
99
|
+
padding-left: 3em;
|
100
|
+
padding-right: 3em;
|
101
|
+
}
|
102
|
+
|
103
|
+
pre {
|
104
|
+
margin: 2em;
|
105
|
+
padding: 1em;
|
106
|
+
border-style: solid;
|
107
|
+
border-width: 1px 1px 1px 1px;
|
108
|
+
border-color: #ddddff;
|
109
|
+
background-color: #eeffee;
|
110
|
+
}
|
111
|
+
|
112
|
+
.copyright {
|
113
|
+
text-align: right;
|
114
|
+
padding: 2em;
|
115
|
+
}
|
data/samples/sample_1.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'locale'
|
3
|
+
|
4
|
+
puts "Get the current locale."
|
5
|
+
|
6
|
+
p Locale.current #=> Returns a language in a TagList.
|
7
|
+
p Locale.charset
|
8
|
+
p Locale.current.language
|
9
|
+
p Locale.current[0].language #=> same result.
|
10
|
+
|
11
|
+
ENV["LANGUAGE"] = "ja_JP.eucJP:fr_FR"
|
12
|
+
puts "Set LANGUAGE." + ENV["LANGUAGE"]
|
13
|
+
|
14
|
+
# Clear locale because the values are cached.
|
15
|
+
Locale.clear
|
16
|
+
|
17
|
+
p Locale.current #=> Return 2 languages in a TagList.
|
18
|
+
p Locale.current[0].language
|
19
|
+
p Locale.current[1].language
|
20
|
+
p Locale.current.language #=> Same with Locale.current[0].language.
|
21
|
+
p Locale.charset
|
22
|
+
|
23
|
+
p "Locale.candidates"
|
24
|
+
p Locale.current
|
25
|
+
p Locale.candidates
|