rubi18n 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,13 +14,8 @@ Rubi18n is a i18n tool to translate your Ruby application in several languages.
14
14
  pluralization (“1 comment”, “5 comments”) in your translation.
15
15
 
16
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:
17
+ 1. Create translations dir. For exmaple: <tt>_YOUR_APP_/translations/</tt>.
18
+ 2. Add file with translation in some language. For example:
24
19
  <tt>_YOUR_APP_/translations/en.yml</tt>
25
20
 
26
21
  post:
@@ -33,10 +28,24 @@ Rubi18n is a i18n tool to translate your Ruby application in several languages.
33
28
  n: %1 comments
34
29
 
35
30
  author: !!proc |name| "This article was written by #{name.capitalize}"
36
- 4. Put auto detection user locale and loading translations:
31
+ 3. Add Rubi18n::Mixin to your application. For example:
32
+
33
+ require "rubygems"
34
+ require "rubi18n"
35
+
36
+ class Application
37
+ include Rubi18n::Mixin
38
+
39
+ end
40
+ 4. Set translations dir and autodetect user locale:
37
41
 
38
- Rubi18n::Locale.current = ENV["LANG"]
39
- i18n = Rubi18n::Translation.load(ENV["LANG"], "YOUR_APP/translations/")
42
+ class Application
43
+ def initialize
44
+ translations_dir File.dirname(__FILE__) + "/translations/"
45
+ set_locales ENV["LANG"]
46
+ end
47
+
48
+ end
40
49
 
41
50
  5. Print translation string to user:
42
51
 
@@ -163,11 +172,10 @@ You can put format string to +strftime+ method in translation file to get
163
172
  locale specific time:
164
173
 
165
174
  translations/en.yml
166
- published:
167
- format: "%A", %d/%B"
175
+ published: "%A, %d/%B"
168
176
 
169
177
  application.rb
170
- i18n.strftime(Time.now, translation.published.format)
178
+ i18n.strftime(Time.now, i18n.published)
171
179
 
172
180
  == License
173
181
  Rubi18n is licensed under the GNU Lesser General Public License version 3.
data/Rakefile CHANGED
@@ -1,13 +1,10 @@
1
1
  require "rubygems"
2
- require "rake"
3
2
  require "rake/rdoctask"
4
- require "spec/rake/spectask"
5
3
  require "rake/gempackagetask"
6
-
7
- require File.join(File.dirname(__FILE__), "lib", "rubi18n")
4
+ require "spec/rake/spectask"
8
5
 
9
6
  PKG_NAME = "rubi18n"
10
- PKG_VERSION = "0.3"
7
+ PKG_VERSION = "0.4"
11
8
 
12
9
  ##############################################################################
13
10
  # Tests
@@ -46,8 +43,8 @@ end
46
43
  ##############################################################################
47
44
 
48
45
  Rake::RDocTask.new do |rdoc|
49
- rdoc.main = "README"
50
- rdoc.rdoc_files.include("README", "LICENSE", "lib/**/*.rb")
46
+ rdoc.main = "README.rdoc"
47
+ rdoc.rdoc_files.include("README.rdoc", "LICENSE", "lib/**/*.rb")
51
48
  rdoc.title = "Rubi18n Documentation"
52
49
  rdoc.rdoc_dir = "doc"
53
50
  rdoc.options << "-c utf-8"
@@ -72,9 +69,10 @@ spec = Gem::Specification.new do |s|
72
69
  "locales/**/*",
73
70
  "LICENSE",
74
71
  "Rakefile",
75
- "README"]
72
+ "README.rdoc"]
76
73
  s.test_files = FileList[
77
74
  "spec/**/*"]
75
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE"]
78
76
  s.require_path = 'lib'
79
77
  s.has_rdoc = true
80
78
 
@@ -27,3 +27,4 @@ require dir + "locale"
27
27
  require dir + "translation"
28
28
  require dir + "translated_string"
29
29
  require dir + "formatters"
30
+ require dir + "mixin"
@@ -0,0 +1,107 @@
1
+ =begin
2
+ Application mixin 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
+ module Rubi18n
21
+ # Mixin for application to add i18n support. It just a wrap for Translation
22
+ # and Locale classes but has more pretty API for i18n.
23
+ #
24
+ # To use it you must set before +translations_dir+ and user locales.
25
+ #
26
+ # == Usage
27
+ #
28
+ # translations/en.yml
29
+ #
30
+ # hello: Hello, %1
31
+ #
32
+ # application.rb
33
+ #
34
+ # require "rubi18n"
35
+ #
36
+ # class Application
37
+ # include Rubi18n::Mixin
38
+ #
39
+ # def initialize
40
+ # translations_dir File.dirname(__FILE__) + "/translations/"
41
+ # set_locales ENV["LANG"]
42
+ #
43
+ # puts i18n.hello(ENV["USER"])
44
+ # end
45
+ # end
46
+ module Mixin
47
+ # Set dir with application translations. It must contain YAML files for
48
+ # each available locale.
49
+ def translations_dir(dir)
50
+ @translations_dir = dir
51
+ end
52
+
53
+ # Return all available translations (all file in +translations_dir+). It
54
+ # will be a hash with locale code as kay and locale title (in it language)
55
+ # as value.
56
+ def translations
57
+ Translation.available(@translations_dir).inject({}) do |all, code|
58
+ all[code] = if Locale.exists? code
59
+ Locale.new(code)["title"]
60
+ else
61
+ nil
62
+ end
63
+ all
64
+ end
65
+ end
66
+
67
+ # Set user locales to load translation. You must set a +translations_dir+
68
+ # before. Use Translation.default to set default locale.
69
+ def set_locales(locales)
70
+ locales = locales.to_a if Array != locales.class
71
+ @locales = locales
72
+ @translation = Translation.load(locales, @translations_dir)
73
+ Locale.current = Locale.find(locales + [Translation.default])
74
+ end
75
+
76
+ # Return user locales
77
+ def locales
78
+ @locales
79
+ end
80
+
81
+ # Return the first available user Locale (or Locale for default_locale)
82
+ def locale
83
+ Locale.current
84
+ end
85
+
86
+ # Return an application Translation.
87
+ def i18n
88
+ @translation
89
+ end
90
+
91
+ # Parse HTTP_ACCEPT_LANGUAGE and return array of users locales
92
+ def parse_http_accept_language(locales)
93
+ return locales if locales.nil?
94
+ locales = locales.split(",")
95
+ locales.map! do |locale|
96
+ locale = locale.split ";q="
97
+ if 1 == locale.size
98
+ [locale[0], 1.0]
99
+ else
100
+ [locale[0], locale[1].to_f]
101
+ end
102
+ end
103
+ locales.sort! { |a, b| b[1] <=> a[1] }
104
+ locales.map! { |i| i[0] }
105
+ end
106
+ end
107
+ end
@@ -134,8 +134,8 @@ module Rubi18n
134
134
  # next in "ru" and "en" (because many Belarusians know Russian and "be"
135
135
  # locale contain in +sublocales+ "ru" and "en").
136
136
  def self.load(locales, translations_dir)
137
- locales = locales.to_a if String == locales.class
138
- locales << @@default
137
+ locales = locales.to_a if Array != locales.class
138
+ locales = locales + [@@default]
139
139
 
140
140
  locales += Locale.find(locales)["sublocales"]
141
141
  locales.each_with_index do |locale, i|
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ class Application
4
+ include Rubi18n::Mixin
5
+
6
+ def initialize
7
+ translations_dir Pathname(__FILE__).dirname + "translations/general"
8
+ set_locales "en"
9
+ end
10
+ end
11
+
12
+ describe Rubi18n::Mixin do
13
+ before :each do
14
+ @app = Application.new
15
+ end
16
+
17
+ it "should get all available translations" do
18
+ @app.translations.should == {
19
+ "en" => "English", "ru" => "Русский", "no_LC" => nil}
20
+ end
21
+
22
+ it "should return user locales" do
23
+ @app.locales.should == ["en"]
24
+ end
25
+
26
+ it "should return user locale info" do
27
+ @app.locale.should == Rubi18n::Locale.new("en")
28
+ end
29
+
30
+ it "should return application translation" do
31
+ @app.i18n.one.should == "One"
32
+ end
33
+
34
+ it "should parse HTTP_ACCEPT_LANGUAGE" do
35
+ @app.parse_http_accept_language("").should == []
36
+ @app.parse_http_accept_language("ru,en;q=0.9").should == ["ru", "en"]
37
+ @app.parse_http_accept_language("ru;q=0.8,en;q=0.9").should == ["en", "ru"]
38
+ end
39
+
40
+ end
@@ -1,3 +1,3 @@
1
- require File.join(File.dirname(__FILE__), "..", "lib", "rubi18n")
1
+ require "pathname"
2
2
 
3
- $KCODE = "u"
3
+ require Pathname(__FILE__).dirname + "../lib/rubi18n"
@@ -1,11 +1,11 @@
1
1
  require File.join(File.dirname(__FILE__), "spec_helper")
2
2
 
3
3
  describe Rubi18n::Translation do
4
- DIR = File.join(File.dirname(__FILE__), "translations", "general")
5
- EXT = File.join(File.dirname(__FILE__), "translations", "extention")
4
+ DIR = Pathname(__FILE__).dirname + "translations/general"
5
+ EXT = Pathname(__FILE__).dirname + "translations/extention"
6
6
 
7
7
  it "should return all available translations" do
8
- Rubi18n::Translation.available(DIR).sort.should == ["en", "fr", "no_LC", "ru"]
8
+ Rubi18n::Translation.available(DIR).sort.should == ["en", "no_LC", "ru"]
9
9
  end
10
10
 
11
11
  it "should has default locale to use when there aren't any user locales" do
@@ -21,8 +21,8 @@ describe Rubi18n::Translation do
21
21
  end
22
22
 
23
23
  it "should find in subtranslations" do
24
- translation = Rubi18n::Translation.load(["fr", "ru"], DIR)
25
- translation.one.should == "Un"
24
+ translation = Rubi18n::Translation.load(["no_LC", "ru"], DIR)
25
+ translation.one.should == "ONE"
26
26
  translation.two.should == "Два"
27
27
  translation.three.should == "Three"
28
28
  end
@@ -33,8 +33,8 @@ describe Rubi18n::Translation do
33
33
  end
34
34
 
35
35
  it "should search language locale for dialect" do
36
- translation = Rubi18n::Translation.load("fr_CA", DIR)
37
- translation.one.should == "Un"
36
+ translation = Rubi18n::Translation.load("ru_RU", DIR)
37
+ translation.one.should == "Один"
38
38
  end
39
39
 
40
40
  it "should can use params in translation" do
@@ -51,8 +51,8 @@ describe Rubi18n::Translation do
51
51
  end
52
52
 
53
53
  it "should return string with locale info" do
54
- translation = Rubi18n::Translation.load(["fr", "ru"], DIR)
55
- translation.one.locale.should == "fr"
54
+ translation = Rubi18n::Translation.load(["no_LC", "ru"], DIR)
55
+ translation.one.locale.should == "no_LC"
56
56
  translation.two.locale.should == Rubi18n::Locale.new("ru")
57
57
  translation.three.locale.should == Rubi18n::Locale.new("en")
58
58
  end
@@ -1,3 +1,5 @@
1
+ one: ONE
2
+
1
3
  entries: !!pl
2
4
  1: ONE
3
5
  2: TWO
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubi18n
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "A.I." Sitnik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-12 00:00:00 +04:00
12
+ date: 2008-09-14 00:00:00 +04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -19,8 +19,9 @@ executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
- extra_rdoc_files: []
23
-
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
24
25
  files:
25
26
  - base/en.yml
26
27
  - base/ru.yml
@@ -30,12 +31,13 @@ files:
30
31
  - lib/rubi18n/locale.rb
31
32
  - lib/rubi18n/formatters.rb
32
33
  - lib/rubi18n/translated_string.rb
34
+ - lib/rubi18n/mixin.rb
33
35
  - locales/en.yml
34
36
  - locales/ru.yml
35
37
  - locales/en_US.yml
36
38
  - LICENSE
37
39
  - Rakefile
38
- - README
40
+ - README.rdoc
39
41
  has_rdoc: true
40
42
  homepage: http://rubi18n.rubyforge.org/
41
43
  post_install_message:
@@ -69,10 +71,10 @@ test_files:
69
71
  - spec/translations/general
70
72
  - spec/translations/general/en.yml
71
73
  - spec/translations/general/ru.yml
72
- - spec/translations/general/fr.yml
73
74
  - spec/translations/general/no_LC.yml
74
75
  - spec/translations/extention
75
76
  - spec/translations/extention/en.yml
76
77
  - spec/translations/extention/no_TR.yml
77
78
  - spec/formatters_spec.rb
78
79
  - spec/translation_spec.rb
80
+ - spec/mixin_spec.rb
@@ -1 +0,0 @@
1
- one: Un