rubi18n 0.3

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/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 ADDED
@@ -0,0 +1,177 @@
1
+ = Rubi18n
2
+
3
+ Rubi18n is a i18n tool to translate your Ruby application in several languages.
4
+
5
+ == Features
6
+
7
+ * If there isn’t translation in user locale, it will be found in locales, which
8
+ user may know (not only in default locale). For example, many people in
9
+ Belarus can understand Russian, and locale has information about it.
10
+ * It can localize your application: format numbers to the rules of the user
11
+ locale, translate month and week days name and give other locale information.
12
+ * It has translation for commons words, like “OK”, “Cancel”, etc.
13
+ * It storage translation in rich YAML format. You can put procedures and
14
+ pluralization (“1 comment”, “5 comments”) in your translation.
15
+
16
+ == How to
17
+ 1. Add rubi18n to your application (and rubygems if you didn't do it):
18
+
19
+ require "rubygems"
20
+ require "rubi18n"
21
+
22
+ 2. Create translations dir. For exmaple: <tt>_YOUR_APP_/translations/</tt>.
23
+ 3. Add file with translation in some language. For example:
24
+ <tt>_YOUR_APP_/translations/en.yml</tt>
25
+
26
+ post:
27
+ add: Add post
28
+ delete: Delete post %1
29
+
30
+ comments: !!pl
31
+ 0: No comments
32
+ 1: One comment
33
+ n: %1 comments
34
+
35
+ author: !!proc |name| "This article was written by #{name.capitalize}"
36
+ 4. Put auto detection user locale and loading translations:
37
+
38
+ Rubi18n::Locale.current = ENV["LANG"]
39
+ i18n = Rubi18n::Translation.load(ENV["LANG"], "YOUR_APP/translations/")
40
+
41
+ 5. Print translation string to user:
42
+
43
+ i18n.post.add #=> "Add post"
44
+ i18n.post.delete("Test") #=> "Delete post Test"
45
+ i18n.strftime(post.created_at, "%B") #=> "September"
46
+ i18n.comments(1) #=> "6 comments"
47
+ i18n.comments(12) #=> "12 comments"
48
+ i18n.author("poet") #=> "This article was written by Poet"
49
+
50
+ == Usage
51
+
52
+ === Locale
53
+ All supported locales are storage in Rubi18n gem at +locales+ dir. If you want
54
+ to add your locale, please write me to andrey@sitnik.ru .
55
+
56
+ To get information about locale create Rubi18n::Locale instance:
57
+
58
+ en = Rubi18n::Locale.new("en")
59
+
60
+ To get first available user locale use Rubi18n::Locale.find methods. It will be
61
+ looking also general language locale for dialect (if Canadian French (+fr_CA+)
62
+ willn't be available it return French (+fr+) locale).
63
+
64
+ locale = Rubi18n::Locale.new(locales_array)
65
+
66
+ You can get from locale:
67
+ * Locale title and RFC 3066 code:
68
+
69
+ locale["title"] #=> "English"
70
+ locale["code"] #=> "en"
71
+
72
+ * Language direction (left to right, or right to left for Arabic and Hebrew):
73
+
74
+ locale["direction"] #=> "ltr"
75
+
76
+ * Week start day ("sunday" or "monday")
77
+
78
+ locale["week_start"] #=> "sunday"
79
+
80
+ === Translation
81
+ Translation files use YAML format and has name CODE.yml , where CODE is a
82
+ language/country code (RFC 3066) like en_US. For example: en.yaml is a English
83
+ translation, en_US.yml is a translation to USA English dialect.
84
+
85
+ In translation you can use:
86
+ * Strings
87
+ robot: This is robot
88
+ percent: "Percent sign (%)"
89
+ * Numbers
90
+ number: 123
91
+ float: 12.45
92
+ * Pluralizable messages
93
+ robots: !!pl
94
+ 0: No robots
95
+ 1: One robot
96
+ n: %1 robots
97
+ * Procedures
98
+ sum: !!proc |x, y| x + y
99
+
100
+ To load translations use Rubi18n::Translation.load method with array with user
101
+ locales codes. It will load all user locales and sublocales, which people in
102
+ this locale offten understand.
103
+
104
+ i18n = Rubi18n::Translation.load(user_locales, DIR_WITH_TRANSLATIONS)
105
+
106
+ To get translated string use method with key name or square brackets [] for
107
+ keys, which contain Object methods (+class+, +inspect+, etc):
108
+
109
+ i18n.robot #=> "This is robot"
110
+ i18n["robot"] #=> "This is robot"
111
+
112
+ Translation may be hierarchical:
113
+
114
+ i18n.post.add #=> "Add post"
115
+ i18n["post"]["add"] #=> "Add post"
116
+
117
+ If locale willn’t be found in user locale Rubi18n will search it in another
118
+ locale, which user know and they sublocales:
119
+
120
+ i18n.no.in.english #=> "В английском нет"
121
+
122
+ Translated string has +locale+ method and you can get it locale (Locale instance
123
+ or code string if locale is’t supported in Rubi18n):
124
+
125
+ i18n.no.in.english.locale #=> Locale ru (Русский)
126
+
127
+ You can replace some parameters in translated string by put it as arguments:
128
+
129
+ name: "My name is %1"
130
+
131
+ i18n.name("John") #=> "My name is John"
132
+
133
+ Pluralizable messages get item count from first argument. Or from next if you
134
+ pluralize several parameters (see spec/translation_spec.rb and
135
+ spec/translation/general/en.yml for samples).
136
+
137
+ i18n.robots(0) #=> "No robots"
138
+ i18n.robots(1) #=> "One robot"
139
+ i18n.robots(50) #=> "50 robots"
140
+
141
+ If there isn’t pluralization for some number (for example, 0), translation will
142
+ be use 'n'. If there isn’t locale file for translation for pluralization will be
143
+ using default (English) rule.
144
+
145
+ Rub18n already has translation for common words for most supported locales.
146
+ See <tt>base/</tt> in dir in gem.
147
+
148
+ === Formatters
149
+ You can print number and float according to the rules of the locale by +to_ls+
150
+ method:
151
+
152
+ 12000.to_ls #=> "12,000"
153
+ 15.34.to_ls #=> "15.34"
154
+
155
+ Number and float formatters will also put real typographic minus and put
156
+ non-break thin spaces (for locale, which use it as digit separator).
157
+
158
+ To translate month and week day names use Rubi18n::Translation.strftime method:
159
+
160
+ i18n.strftime(Time.now, "%A", %d/%B") #=> "Tuesday, 09 September"
161
+
162
+ You can put format string to +strftime+ method in translation file to get
163
+ locale specific time:
164
+
165
+ translations/en.yml
166
+ published:
167
+ format: "%A", %d/%B"
168
+
169
+ application.rb
170
+ i18n.strftime(Time.now, translation.published.format)
171
+
172
+ == License
173
+ Rubi18n is licensed under the GNU Lesser General Public License version 3.
174
+ You can read it in LICENSE file or in http://www.gnu.org/licenses/gpl.html.
175
+
176
+ == Author
177
+ Andrey “A.I.” Sitnik <andrey@sitnik.ru>
data/Rakefile ADDED
@@ -0,0 +1,95 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "rake/rdoctask"
4
+ require "spec/rake/spectask"
5
+ require "rake/gempackagetask"
6
+
7
+ require File.join(File.dirname(__FILE__), "lib", "rubi18n")
8
+
9
+ PKG_NAME = "rubi18n"
10
+ PKG_VERSION = "0.3"
11
+
12
+ ##############################################################################
13
+ # Tests
14
+ ##############################################################################
15
+
16
+ desc "Run all specs"
17
+ Spec::Rake::SpecTask.new('specs') do |t|
18
+ t.spec_opts = ["--format", "specdoc", "--colour"]
19
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
20
+ end
21
+
22
+ desc "Run a specific spec with TASK=xxxx"
23
+ Spec::Rake::SpecTask.new('spec') do |t|
24
+ t.spec_opts = ["--format", "specdoc", "--colour"]
25
+ t.libs = ['lib']
26
+ t.spec_files = ["spec/**/#{ENV['TASK']}_spec.rb"]
27
+ end
28
+
29
+ desc "Run all specs output html"
30
+ Spec::Rake::SpecTask.new('specs_html') do |t|
31
+ t.spec_opts = ["--format", "html"]
32
+ t.libs = ['lib']
33
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
34
+ end
35
+
36
+ desc "RCov"
37
+ Spec::Rake::SpecTask.new('rcov') do |t|
38
+ t.spec_opts = ["--format", "specdoc", "--colour"]
39
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
40
+ t.libs = ['lib']
41
+ t.rcov = true
42
+ end
43
+
44
+ ##############################################################################
45
+ # Documentation and distribution
46
+ ##############################################################################
47
+
48
+ Rake::RDocTask.new do |rdoc|
49
+ rdoc.main = "README"
50
+ rdoc.rdoc_files.include("README", "LICENSE", "lib/**/*.rb")
51
+ rdoc.title = "Rubi18n Documentation"
52
+ rdoc.rdoc_dir = "doc"
53
+ rdoc.options << "-c utf-8"
54
+ rdoc.options << "--all"
55
+ end
56
+
57
+ spec = Gem::Specification.new do |s|
58
+ s.platform = Gem::Platform::RUBY
59
+ s.name = PKG_NAME
60
+ s.version = PKG_VERSION
61
+ s.summary = "I18n tool to translate your Ruby application."
62
+ s.description = <<-EOF
63
+ Rubi18n is a i18n tool to translate your Ruby application.
64
+ It can localize your application, has translation for commons words,
65
+ storage translation in rich YAML format with pluralization and procedures
66
+ and has sublocales list for each supported locale.
67
+ EOF
68
+
69
+ s.files = FileList[
70
+ "base/**/*",
71
+ "lib/**/*",
72
+ "locales/**/*",
73
+ "LICENSE",
74
+ "Rakefile",
75
+ "README"]
76
+ s.test_files = FileList[
77
+ "spec/**/*"]
78
+ s.require_path = 'lib'
79
+ s.has_rdoc = true
80
+
81
+ s.author = 'Andrey "A.I." Sitnik'
82
+ s.email = "andrey@sitnik.ru"
83
+ s.homepage = "http://rubi18n.rubyforge.org/"
84
+ s.rubyforge_project = PKG_NAME
85
+ end
86
+
87
+ Rake::GemPackageTask.new(spec) do |pkg|
88
+ pkg.gem_spec = spec
89
+ end
90
+
91
+ desc "Install Rubi18n as a gem."
92
+ task :install => [:package] do
93
+ sudo = RUBY_PLATFORM =~ /win32/ ? '' : 'sudo'
94
+ sh %{#{sudo} gem install pkg/#{PKG_NAME}-#{PKG_VERSION}}
95
+ end
data/base/en.yml ADDED
@@ -0,0 +1,15 @@
1
+ ok: OK
2
+ save: Save
3
+ canel: Cancel
4
+ exit: Exit
5
+ delete: Delete
6
+ am: AM
7
+ pm: PM
8
+
9
+ months:
10
+ names: [January, February, March, April, May, June, July, August, September, October, November, December]
11
+ abbr_names: [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
12
+
13
+ week:
14
+ days: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
15
+ abbr_days: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
data/base/ru.yml ADDED
@@ -0,0 +1,13 @@
1
+ ok: OK
2
+ save: Сохранить
3
+ canel: Отмена
4
+ exit: Выход
5
+ delete: Удалить
6
+
7
+ months:
8
+ names: [Январь, Февраль, Март, Апрель, Май, Июнь, Июль, Август, Сентябрь, Октябрь, Ноябрь, Декабрь]
9
+ abbr_names: [Янв, Фев, Мар, Апр, Май, Июн, Июл, Авг, Сен, Окт, Ноя, Дек]
10
+
11
+ week:
12
+ days: [Воскресенье, Понедельник, Вторник, Среда, Четверг, Пятница, Суббота]
13
+ abbr_days: [Вск, Пнд, Втр, Срд, Чтв, Птн, Сбт]
@@ -0,0 +1,74 @@
1
+ =begin
2
+ Formatters to 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
+ module Rubi18n
21
+ module Formatters
22
+ module Integer
23
+ # Returns the integer in String form, according to the rules of the
24
+ # locale (default currently active). It will also put real typographic
25
+ # minus.
26
+ def to_ls(locale = Rubi18n::Locale.current)
27
+ str = self.to_s
28
+ str[0] = "−" if 0 > self # Real typographic minus
29
+ group = locale["numbers"]["group_delimiter"]
30
+
31
+ if "indian" == locale["numbers"]["separation"]
32
+ str.gsub(/(\d)(?=((\d\d\d)(?!\d))|((\d\d)+(\d\d\d)(?!\d)))/) do |match|
33
+ match + group
34
+ end
35
+ else
36
+ str.gsub(/(\d)(?=(\d\d\d)+(?!\d))/) do |match|
37
+ match + group
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ module Float
44
+ # Returns the integer in String form, according to the rules of the
45
+ # locale (default currently active). It will also put real typographic
46
+ # minus.
47
+ def to_ls(locale = Rubi18n::Locale.current)
48
+ int, frac = self.to_s.split('.')
49
+ int[0] = "−" if 0 > self # Real typographic minus
50
+ group = locale["numbers"]["group_delimiter"]
51
+ decimal = locale["numbers"]["decimal_separator"]
52
+
53
+ if "indian" == locale["numbers"]["separation"]
54
+ int.gsub!(/(\d)(?=((\d\d\d)(?!\d))|((\d\d)+(\d\d\d)(?!\d)))/) do |match|
55
+ match + group
56
+ end
57
+ else
58
+ int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) do |match|
59
+ match + group
60
+ end
61
+ end
62
+ int + decimal + frac
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ class Integer
69
+ include Rubi18n::Formatters::Integer
70
+ end
71
+
72
+ class Float
73
+ include Rubi18n::Formatters::Float
74
+ end
@@ -0,0 +1,124 @@
1
+ =begin
2
+ Locale to 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
+ require "pathname"
21
+ require "yaml"
22
+
23
+ module Rubi18n
24
+ # Information about locale (language, country and other special variant
25
+ # preferences). Locale was named by RFC 3066. For example locale for French
26
+ # speaking people in Canada will be +fr_CA+.
27
+ #
28
+ # Locale files is placed in <tt>rubi18n/locales/</tt> dir in YAML files.
29
+ #
30
+ # == Usage
31
+ #
32
+ # Set system locale to Russian
33
+ # Rubi18n::Locale.current = "ru"
34
+ #
35
+ # Get locale for USA English and print it title
36
+ # locale = Rubi18n::Locale.new("en_US")
37
+ # puts locale["title"]
38
+ #
39
+ # Set locale to user system locale (in UNIX-like OS) and print it title
40
+ # Rubi18n::Locale.current = ENV["LANG"]
41
+ # puts "Your locale is #{Rubi18n::Locale.current['code']}"
42
+ #
43
+ # == Formatting
44
+ #
45
+ # You can print number and float according to the rules of the locale by
46
+ # +to_ls+ method. See Rubi18n::Formatters.
47
+ #
48
+ # For translate month and week day names in Time and Date see
49
+ # <tt>Translation.strftime</tt> method.
50
+ class Locale
51
+ LOCALES_DIR = Pathname(__FILE__).dirname.expand_path + "../../locales/"
52
+
53
+ # Set system locale to use in +to_ls+ methods in another classes
54
+ def self.current=(locale)
55
+ if String == locale.class
56
+ locale = Locale.find(locale)
57
+ end
58
+ @@current = locale
59
+ end
60
+
61
+ # Return system locale
62
+ def self.current
63
+ @@current
64
+ end
65
+
66
+ # All available locales
67
+ def self.locales
68
+ Dir.glob(File.join(LOCALES_DIR, "*.yml")).map do |i|
69
+ File.basename(i, ".yml")
70
+ end
71
+ end
72
+
73
+ # Is +locale+ has info file
74
+ def self.exists?(locale)
75
+ File.exists?(File.join(LOCALES_DIR, locale + ".yml"))
76
+ end
77
+
78
+ # Find and load first available locale from +locales+
79
+ def self.find(locales)
80
+ locales = locales.to_a if String == locales.class
81
+
82
+ locales.each_with_index do |locale, i|
83
+ locale = locale[0..4] if locale.length > 5
84
+ locales.insert(i + 1, locale[0..1]) if locale.length > 2
85
+ return self.new(locale) if self.exists? locale
86
+ end
87
+ end
88
+
89
+ # Load locale by in +code+ (RFC 3066)
90
+ def initialize(code)
91
+ code.delete!("/", "\\")
92
+
93
+ @locale = {}
94
+ while code
95
+ file = File.join(LOCALES_DIR, "#{code}.yml")
96
+ raise "Locale #{code} isn't exists" if not File.exists? file
97
+ loaded = YAML.load_file(file)
98
+ @locale = loaded.merge @locale
99
+ code = loaded["include"]
100
+ end
101
+
102
+ eval("def pluralize(n); #{@locale["pluralization"]}; end", binding)
103
+ end
104
+
105
+ # Get information about locale
106
+ def [](name)
107
+ @locale[name]
108
+ end
109
+
110
+ # Is another locale has same language code
111
+ def ==(locale)
112
+ @locale['code'] == locale['code']
113
+ end
114
+
115
+ # Human readable language code and title
116
+ def inspect
117
+ "Locale #{@locale['code']} (#{@locale['title']})"
118
+ end
119
+
120
+ # Return pluralization type for this locale for +n+ items.
121
+ # Will be replacing code from locale info file.
122
+ def pluralize(n); end
123
+ end
124
+ end