r18n-core 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +166 -0
- data/README.rdoc +136 -0
- data/Rakefile +93 -0
- data/base/en.yml +7 -0
- data/base/ru.yml +7 -0
- data/lib/r18n-core.rb +42 -0
- data/lib/r18n-core/i18n.rb +191 -0
- data/lib/r18n-core/locale.rb +171 -0
- data/lib/r18n-core/translated_string.rb +33 -0
- data/lib/r18n-core/translation.rb +203 -0
- data/locales/en.yml +34 -0
- data/locales/en_US.yml +5 -0
- data/locales/ru.yml +34 -0
- data/spec/i18n_spec.rb +78 -0
- data/spec/locale_spec.rb +89 -0
- data/spec/r18n_spec.rb +11 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/translation_spec.rb +90 -0
- data/spec/translations/extension/en.yml +2 -0
- data/spec/translations/extension/no_TR.yml +1 -0
- data/spec/translations/general/en.yml +24 -0
- data/spec/translations/general/no_LC.yml +6 -0
- data/spec/translations/general/ru.yml +4 -0
- metadata +79 -0
data/LICENSE
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
166
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
= R18n
|
2
|
+
|
3
|
+
R18n is a i18n tool to translate your Ruby application in several languages.
|
4
|
+
|
5
|
+
Use <tt>merb_r18n</tt> to localize Merb Web application and
|
6
|
+
<tt>r18n-desktop</tt> to localize desktop application.
|
7
|
+
|
8
|
+
== Features
|
9
|
+
|
10
|
+
* If there isn’t translation in user locale, it will be found in locales, which
|
11
|
+
user may know (not only in default locale). For example, many people in
|
12
|
+
Belarus can understand Russian, and locale has information about it.
|
13
|
+
* It can localize your application: format numbers to the rules of the user
|
14
|
+
locale, translate month and week days name and give other locale information.
|
15
|
+
* It has translation for commons words, like “OK”, “Cancel”, etc.
|
16
|
+
* It storage translation in rich YAML format. You can put procedures and
|
17
|
+
pluralization (“1 comment”, “5 comments”) in your translation.
|
18
|
+
* It can translate Web and desktop applications.
|
19
|
+
|
20
|
+
== Usage
|
21
|
+
|
22
|
+
=== Locale
|
23
|
+
All supported locales are storage in R18n gem at +locales+ dir. If you want to
|
24
|
+
add your locale, please write me to andrey@sitnik.ru.
|
25
|
+
|
26
|
+
To get information about locale create R18n::Locale instance:
|
27
|
+
|
28
|
+
locale = R18n::Locale.new('en')
|
29
|
+
|
30
|
+
You can get from locale:
|
31
|
+
* Locale title and RFC 3066 code:
|
32
|
+
|
33
|
+
locale['title'] #=> "English"
|
34
|
+
locale['code'] #=> "en"
|
35
|
+
|
36
|
+
* Language direction (left to right, or right to left for Arabic and Hebrew):
|
37
|
+
|
38
|
+
locale['direction'] #=> "ltr"
|
39
|
+
|
40
|
+
* Week start day ("sunday" or "monday")
|
41
|
+
|
42
|
+
locale['week']['start'] #=> "sunday"
|
43
|
+
|
44
|
+
=== Translation
|
45
|
+
Translation files use YAML format and has name like en.yml (English) or
|
46
|
+
en_US.yml (USA English dialect) with language/country code (RFC 3066).
|
47
|
+
|
48
|
+
In translation you can use:
|
49
|
+
* Strings
|
50
|
+
robot: This is robot
|
51
|
+
percent: "Percent sign (%)"
|
52
|
+
* Numbers
|
53
|
+
number: 123
|
54
|
+
float: 12.45
|
55
|
+
* Pluralizable messages
|
56
|
+
robots: !!pl
|
57
|
+
0: No robots
|
58
|
+
1: One robot
|
59
|
+
n: %1 robots
|
60
|
+
* Procedures
|
61
|
+
sum: !!proc |x, y| x + y
|
62
|
+
|
63
|
+
To get translated string use method with key name or square brackets [] for
|
64
|
+
keys, which is same with Object methods (+class+, +inspect+, etc):
|
65
|
+
|
66
|
+
i18n.robot #=> "This is robot"
|
67
|
+
i18n["robot"] #=> "This is robot"
|
68
|
+
|
69
|
+
Translation may be hierarchical:
|
70
|
+
|
71
|
+
i18n.post.add #=> "Add post"
|
72
|
+
i18n['post']['add'] #=> "Add post"
|
73
|
+
|
74
|
+
If locale willn’t be found in user locale R18n will search it in they sublocales
|
75
|
+
or in another locale, which user know:
|
76
|
+
|
77
|
+
i18n.no.in.english #=> "В английском нет"
|
78
|
+
|
79
|
+
Translated string has +locale+ method and you can get it locale (Locale instance
|
80
|
+
or code string if locale is’t supported in R18n):
|
81
|
+
|
82
|
+
i18n.no.in.english.locale #=> Locale ru (Русский)
|
83
|
+
|
84
|
+
You can replace some parameters in translated string by put it as arguments:
|
85
|
+
|
86
|
+
name: "My name is %1"
|
87
|
+
|
88
|
+
i18n.name('John') #=> "My name is John"
|
89
|
+
|
90
|
+
Pluralizable messages get item count from first argument:
|
91
|
+
|
92
|
+
i18n.robots(0) #=> "No robots"
|
93
|
+
i18n.robots(1) #=> "One robot"
|
94
|
+
i18n.robots(50) #=> "50 robots"
|
95
|
+
|
96
|
+
If there isn’t pluralization for some number, translation will be use 'n'. If
|
97
|
+
there isn’t locale file for translation, it will be use English pluralization
|
98
|
+
rule (0, 1 and 'n').
|
99
|
+
|
100
|
+
R18n already has translation for common words for most supported locales.
|
101
|
+
See <tt>base/</tt> in dir in gem.
|
102
|
+
|
103
|
+
i18n.yes #=> "Yes"
|
104
|
+
i18n.cancel #=> "Cancel"
|
105
|
+
i18n.delete #=> "Delete"
|
106
|
+
|
107
|
+
=== Localization
|
108
|
+
You can print number and float according to the rules of the user locale:
|
109
|
+
|
110
|
+
i18n.l -12000.5 #=> "−12,000.5"
|
111
|
+
|
112
|
+
Number and float formatters will also put real typographic minus and put
|
113
|
+
non-break thin spaces (for locale, which use it as digit separator).
|
114
|
+
|
115
|
+
You can translate months and week days names in Time, Date and DateTime by
|
116
|
+
+strftime+ method:
|
117
|
+
|
118
|
+
i18n.l Time.now, "%B" #=> "September"
|
119
|
+
|
120
|
+
R18n has some time formats for locales: <tt>:time</tt>, <tt>:date</tt>,
|
121
|
+
<tt>:short_data</tt>, <tt>:long_data</tt>, <tt>:datetime</tt>,
|
122
|
+
<tt>:short_datetime</tt> and <tt>:long_datetime</tt>:
|
123
|
+
|
124
|
+
i18n.l Time.now, :date #=> "09/21/2008"
|
125
|
+
|
126
|
+
By default +strftime+ will be use <tt>:datetime</tt> format for Time and
|
127
|
+
DateTime and <tt>:date</tt> for Date:
|
128
|
+
|
129
|
+
i18n.l Time.now #=> "Sun Sep 21 19:50:04 GMT 2008"
|
130
|
+
|
131
|
+
== License
|
132
|
+
R18n is licensed under the GNU Lesser General Public License version 3.
|
133
|
+
You can read it in LICENSE file or in http://www.gnu.org/licenses/lgpl.html.
|
134
|
+
|
135
|
+
== Author
|
136
|
+
Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
data/Rakefile
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
PKG_NAME = 'r18n-core'
|
7
|
+
PKG_VERSION = '0.1'
|
8
|
+
|
9
|
+
##############################################################################
|
10
|
+
# Tests
|
11
|
+
##############################################################################
|
12
|
+
|
13
|
+
desc 'Run all specs'
|
14
|
+
Spec::Rake::SpecTask.new('specs') do |t|
|
15
|
+
t.spec_opts = ['--format', 'specdoc', '--colour']
|
16
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Run a specific spec with TASK=xxxx'
|
20
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
21
|
+
t.spec_opts = ['--format', 'specdoc', '--colour']
|
22
|
+
t.libs = ['lib']
|
23
|
+
t.spec_files = ["spec/**/#{ENV['TASK']}_spec.rb"]
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Run all specs output html'
|
27
|
+
Spec::Rake::SpecTask.new('specs_html') do |t|
|
28
|
+
t.spec_opts = ['--format', 'html']
|
29
|
+
t.libs = ['lib']
|
30
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'RCov'
|
34
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
35
|
+
t.spec_opts = ['--format', 'specdoc', '--colour']
|
36
|
+
t.spec_files = Dir['spec/**/*_spec.rb'].sort
|
37
|
+
t.libs = ['lib']
|
38
|
+
t.rcov = true
|
39
|
+
end
|
40
|
+
|
41
|
+
##############################################################################
|
42
|
+
# Documentation and distribution
|
43
|
+
##############################################################################
|
44
|
+
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
rdoc.main = 'README.rdoc'
|
47
|
+
rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'lib/**/*.rb')
|
48
|
+
rdoc.title = 'R18n Documentation'
|
49
|
+
rdoc.rdoc_dir = 'doc'
|
50
|
+
rdoc.options << '-c utf-8'
|
51
|
+
rdoc.options << '--all'
|
52
|
+
end
|
53
|
+
|
54
|
+
spec = Gem::Specification.new do |s|
|
55
|
+
s.platform = Gem::Platform::RUBY
|
56
|
+
s.name = PKG_NAME
|
57
|
+
s.version = PKG_VERSION
|
58
|
+
s.summary = 'I18n tool to translate your Ruby application.'
|
59
|
+
s.description = <<-EOF
|
60
|
+
R18n is a i18n tool to translate your Ruby application.
|
61
|
+
It can localize your application, has translation for commons words,
|
62
|
+
storage translation in rich YAML format with pluralization and procedures
|
63
|
+
and has sublocales list for each supported locale.
|
64
|
+
EOF
|
65
|
+
|
66
|
+
s.files = FileList[
|
67
|
+
'base/**/*',
|
68
|
+
'lib/**/*',
|
69
|
+
'locales/**/*',
|
70
|
+
'LICENSE',
|
71
|
+
'Rakefile',
|
72
|
+
'README.rdoc']
|
73
|
+
s.test_files = FileList[
|
74
|
+
'spec/**/*']
|
75
|
+
s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
|
76
|
+
s.require_path = 'lib'
|
77
|
+
s.has_rdoc = true
|
78
|
+
|
79
|
+
s.author = 'Andrey "A.I." Sitnik'
|
80
|
+
s.email = 'andrey@sitnik.ru'
|
81
|
+
s.homepage = 'http://r18n.rubyforge.org/'
|
82
|
+
s.rubyforge_project = PKG_NAME
|
83
|
+
end
|
84
|
+
|
85
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
86
|
+
pkg.gem_spec = spec
|
87
|
+
end
|
88
|
+
|
89
|
+
desc 'Install as a gem'
|
90
|
+
task :install => [:package] do
|
91
|
+
sudo = RUBY_PLATFORM =~ /win32/ ? '' : 'sudo'
|
92
|
+
sh %{#{sudo} gem install pkg/#{PKG_NAME}-#{PKG_VERSION}}
|
93
|
+
end
|
data/base/en.yml
ADDED
data/base/ru.yml
ADDED
data/lib/r18n-core.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
=begin
|
2
|
+
Main file to load all neccessary classes for i18n support.
|
3
|
+
|
4
|
+
Copyright (C) 2008 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
5
|
+
|
6
|
+
This program is free software: you can redistribute it and/or modify
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
9
|
+
(at your option) any later version.
|
10
|
+
|
11
|
+
This program is distributed in the hope that it will be useful,
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
GNU Lesser General Public License for more details.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
17
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
=end
|
19
|
+
|
20
|
+
$KCODE = 'u'
|
21
|
+
|
22
|
+
require 'pathname'
|
23
|
+
dir = Pathname(__FILE__).dirname.expand_path + 'r18n-core'
|
24
|
+
|
25
|
+
require dir + 'locale'
|
26
|
+
require dir + 'translated_string'
|
27
|
+
require dir + 'translation'
|
28
|
+
require dir + 'i18n'
|
29
|
+
|
30
|
+
module R18n
|
31
|
+
class << self
|
32
|
+
# Set <tt>i18n</tt> object to current thread
|
33
|
+
def set(i18n)
|
34
|
+
Thread.current['i18n'] = i18n
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get I18n object for current thread
|
38
|
+
def get
|
39
|
+
Thread.current['i18n']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|