sinatra-r18n 0.3.2 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +18 -17
- data/lib/sinatra/r18n.rb +13 -1
- data/spec/app/app.rb +10 -6
- data/spec/app/views/post.erb +1 -1
- data/spec/sinatra-r18n_spec.rb +6 -0
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -2,22 +2,21 @@
|
|
2
2
|
|
3
3
|
Sinatra extension that provides i18n support to translate your web application.
|
4
4
|
|
5
|
-
It is just a
|
5
|
+
It is just a wrapper for R18n core library. See R18n documentation for more
|
6
6
|
information.
|
7
7
|
|
8
8
|
== Features
|
9
9
|
|
10
|
-
*
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
*
|
15
|
-
|
16
|
-
*
|
17
|
-
* It storage translation in rich YAML format. You can put procedures and
|
18
|
-
pluralization (“1 comment”, “5 comments”) or create you own filters.
|
10
|
+
* Nice Ruby-style syntax.
|
11
|
+
* Filters.
|
12
|
+
* Flexible locales.
|
13
|
+
* Custom translations loaders.
|
14
|
+
* Translation support for any classes.
|
15
|
+
* Time and number localization.
|
16
|
+
* Several user language support.
|
19
17
|
|
20
18
|
== How To
|
19
|
+
|
21
20
|
1. Create translations dir <tt>./i18n/</tt>.
|
22
21
|
2. Add file with translation in some language. For example
|
23
22
|
<tt>./i18n/en.yml</tt>:
|
@@ -54,30 +53,32 @@ information.
|
|
54
53
|
Or you save locale in session, when user change it:
|
55
54
|
|
56
55
|
before do
|
57
|
-
|
56
|
+
session[:locale] = params[:locale] if params[:locale]
|
58
57
|
end
|
59
58
|
|
60
59
|
5. Use translation messages in view. For example in HAML:
|
61
60
|
|
62
|
-
%p=
|
63
|
-
%p=
|
61
|
+
%p= t.post.friends
|
62
|
+
%p= t.post.tags(@post.tags.join(', '))
|
64
63
|
|
65
|
-
%h2=
|
64
|
+
%h2= t.comments(@post.comments.size)
|
66
65
|
|
67
66
|
6. Print localized time and numbers. For example:
|
68
67
|
|
69
|
-
|
68
|
+
l @post.created_at, :human
|
70
69
|
|
71
70
|
7. Print available translations. For example in HAML:
|
72
71
|
|
73
72
|
%ul
|
74
|
-
-
|
73
|
+
- r18n.available_locales.each do |locale|
|
75
74
|
%li
|
76
|
-
%a{ href: "/#{locale}/" }= title
|
75
|
+
%a{ href: "/#{locale.code}/" }= locale.title
|
77
76
|
|
78
77
|
== License
|
78
|
+
|
79
79
|
R18n is licensed under the GNU Lesser General Public License version 3.
|
80
80
|
You can read it in LICENSE file or in http://www.gnu.org/licenses/lgpl.html.
|
81
81
|
|
82
82
|
== Author
|
83
|
+
|
83
84
|
Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
data/lib/sinatra/r18n.rb
CHANGED
@@ -30,7 +30,7 @@ module Sinatra #::nodoc::
|
|
30
30
|
module Helpers
|
31
31
|
# Return tool for i18n support. It will be R18n::I18n object, see it
|
32
32
|
# documentation for more information.
|
33
|
-
def
|
33
|
+
def r18n
|
34
34
|
unless @i18n
|
35
35
|
::R18n::I18n.default = options.default_locale
|
36
36
|
|
@@ -47,6 +47,18 @@ module Sinatra #::nodoc::
|
|
47
47
|
@i18n
|
48
48
|
end
|
49
49
|
end
|
50
|
+
alias i18n r18n
|
51
|
+
|
52
|
+
|
53
|
+
# Translate message. Alias for <tt>r18n.t</tt>.
|
54
|
+
def t(*params)
|
55
|
+
i18n.t(*params)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Localize object. Alias for <tt>r18n.l</tt>.
|
59
|
+
def l(*params)
|
60
|
+
i18n.l(*params)
|
61
|
+
end
|
50
62
|
end
|
51
63
|
|
52
64
|
def self.registered(app) #::nodoc::
|
data/spec/app/app.rb
CHANGED
@@ -8,25 +8,29 @@ get '/:locale/posts/:name' do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
get '/:locale/posts/:name/comments' do
|
11
|
-
|
11
|
+
t.post.comments(3).to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/time' do
|
15
|
+
l Time.at(0).utc
|
12
16
|
end
|
13
17
|
|
14
18
|
get '/locale' do
|
15
|
-
|
19
|
+
r18n.locale.title
|
16
20
|
end
|
17
21
|
|
18
22
|
get '/locales' do
|
19
|
-
|
23
|
+
r18n.available_locales.map { |i| "#{i.code}: #{i.title}" }.sort.join('; ')
|
20
24
|
end
|
21
25
|
|
22
26
|
get '/greater' do
|
23
|
-
|
27
|
+
t.greater
|
24
28
|
end
|
25
29
|
|
26
30
|
get '/warning' do
|
27
|
-
|
31
|
+
t.warning
|
28
32
|
end
|
29
33
|
|
30
34
|
get '/untranslated' do
|
31
|
-
"#{
|
35
|
+
"#{t.post.no}"
|
32
36
|
end
|
data/spec/app/views/post.erb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
<h1><%=
|
1
|
+
<h1><%= t.post.title(@post) %></h1>
|
data/spec/sinatra-r18n_spec.rb
CHANGED
@@ -58,4 +58,10 @@ describe Sinatra::R18n do
|
|
58
58
|
last_response.body.should == "post.<span style='color: red'>no</span>"
|
59
59
|
end
|
60
60
|
|
61
|
+
it "should localize objects" do
|
62
|
+
get '/time'
|
63
|
+
last_response.should be_ok
|
64
|
+
last_response.body.should == "01/01/1970 00:00"
|
65
|
+
end
|
66
|
+
|
61
67
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-r18n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
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:
|
12
|
+
date: 2010-01-03 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,9 +20,9 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - "="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.
|
23
|
+
version: "0.4"
|
24
24
|
version:
|
25
|
-
description: " A Sinatra extension that provides i18n support to
|
25
|
+
description: " A Sinatra extension that provides i18n support to translate your web\n application. It is just a wrapper for R18n core library.\n It has nice Ruby-style syntax, filters, flexible locales, custom loaders,\n translation support for any classes, time and number localization, several\n user language support, agnostic core package with out-of-box support for\n Rails, Sinatra, Merb and desktop applications.\n"
|
26
26
|
email: andrey@sitnik.ru
|
27
27
|
executables: []
|
28
28
|
|