sinatra-r18n 0.2.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 +80 -0
- data/lib/sinatra/r18n.rb +57 -0
- data/spec/app/app.rb +19 -0
- data/spec/app/i18n/en.yml +6 -0
- data/spec/app/i18n/ru.yml +2 -0
- data/spec/app/views/post.erb +1 -0
- data/spec/sinatra-r18n_spec.rb +43 -0
- data/spec/spec_helper.rb +9 -0
- metadata +74 -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,80 @@
|
|
1
|
+
= Sinatra R18n Plugin
|
2
|
+
|
3
|
+
Sinatra extension that provides i18n support to translate your web application.
|
4
|
+
|
5
|
+
It is just a wrap for R18n core library. See R18n documentation for more
|
6
|
+
information.
|
7
|
+
|
8
|
+
== Features
|
9
|
+
|
10
|
+
* It has special support for countries with two official languages. If there
|
11
|
+
isn’t translation in user locale, it will be found in locales, which
|
12
|
+
user may know (not only in default locale). For example, many people in
|
13
|
+
Belarus can understand Russian, and locale has information about it.
|
14
|
+
* It can format numbers and time to the rules of the user locale, translate
|
15
|
+
month and week days name and give other locale information.
|
16
|
+
* It has translation for commons words, like “OK”, “Cancel”, etc.
|
17
|
+
* It storage translation in rich YAML format. You can put procedures and
|
18
|
+
pluralization (“1 comment”, “5 comments”) in your translation.
|
19
|
+
|
20
|
+
== How To
|
21
|
+
1. Create translations dir <tt>./i18n/</tt>.
|
22
|
+
2. Add file with translation in some language. For example
|
23
|
+
<tt>./i18n/en.yml</tt>:
|
24
|
+
|
25
|
+
post:
|
26
|
+
friends: Post only for friends
|
27
|
+
tags: Post tags: %1
|
28
|
+
|
29
|
+
comments: !!pl
|
30
|
+
0: No comments
|
31
|
+
1: One comment
|
32
|
+
n: %1 comments
|
33
|
+
|
34
|
+
3. Add R18n to your application:
|
35
|
+
|
36
|
+
require 'rubygems'
|
37
|
+
require 'sinatra'
|
38
|
+
require 'sinatra/r18n'
|
39
|
+
|
40
|
+
4. Set default locale (if it isn’t English):
|
41
|
+
|
42
|
+
set :default_locale, 'ru'
|
43
|
+
|
44
|
+
5. Add locale to your URLs. For example:
|
45
|
+
|
46
|
+
get '/:locale/posts/:id' do
|
47
|
+
@post = Post.find(params[:id])
|
48
|
+
haml :post
|
49
|
+
end
|
50
|
+
|
51
|
+
Or you save locale in session, when user change it:
|
52
|
+
|
53
|
+
before do
|
54
|
+
session[:locale] = params[:locale] if params[:locale]
|
55
|
+
end
|
56
|
+
|
57
|
+
6. Use translation messages in view. For example in HAML:
|
58
|
+
|
59
|
+
%p= i18n.post.friends
|
60
|
+
%p= i18n.post.tags(@post.tags.join(', '))
|
61
|
+
|
62
|
+
%h2= i18n.comments(@post.comments.size)
|
63
|
+
|
64
|
+
7. Print localized time and numbers. For example:
|
65
|
+
|
66
|
+
i18n.l @post.created_at, :date
|
67
|
+
|
68
|
+
8. Print available translation. For example in HAML:
|
69
|
+
|
70
|
+
%ul
|
71
|
+
- i18n.translations.each_pair do |locale, title|
|
72
|
+
%li
|
73
|
+
%a{ href: "/#{locale}/" }= title
|
74
|
+
|
75
|
+
== License
|
76
|
+
R18n is licensed under the GNU Lesser General Public License version 3.
|
77
|
+
You can read it in LICENSE file or in http://www.gnu.org/licenses/lgpl.html.
|
78
|
+
|
79
|
+
== Author
|
80
|
+
Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
data/lib/sinatra/r18n.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
=begin
|
3
|
+
Sinatra extension to i18n support.
|
4
|
+
|
5
|
+
Copyright (C) 2009 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
|
6
|
+
|
7
|
+
This program is free software: you can redistribute it and/or modify
|
8
|
+
it under the terms of the GNU Lesser General Public License as published by
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
10
|
+
(at your option) any later version.
|
11
|
+
|
12
|
+
This program is distributed in the hope that it will be useful,
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15
|
+
GNU Lesser General Public License for more details.
|
16
|
+
|
17
|
+
You should have received a copy of the GNU Lesser General Public License
|
18
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
=end
|
20
|
+
|
21
|
+
require 'rubygems'
|
22
|
+
require 'sinatra/base'
|
23
|
+
|
24
|
+
gem 'r18n-core', '~>0.2'
|
25
|
+
require 'r18n-core'
|
26
|
+
|
27
|
+
module Sinatra #::nodoc::
|
28
|
+
module R18n #::nodoc::
|
29
|
+
module Helpers
|
30
|
+
# Return tool for i18n support. It will be R18n::I18n object, see it
|
31
|
+
# documentation for more information.
|
32
|
+
def i18n
|
33
|
+
unless @i18n
|
34
|
+
::R18n::I18n.default = options.default_locale
|
35
|
+
|
36
|
+
locales = ::R18n::I18n.parse_http(request.env['HTTP_ACCEPT_LANGUAGE'])
|
37
|
+
if params[:locale]
|
38
|
+
locales.insert(0, params[:locale])
|
39
|
+
elsif session[:locale]
|
40
|
+
locales.insert(0, session[:locale])
|
41
|
+
end
|
42
|
+
|
43
|
+
@i18n = ::R18n::I18n.new(locales, options.translations)
|
44
|
+
end
|
45
|
+
@i18n
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.registered(app) #::nodoc::
|
50
|
+
app.helpers Helpers
|
51
|
+
app.set :default_locale, 'en'
|
52
|
+
app.set :translations, Proc.new { File.join(app.root, 'i18n/') }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
register R18n
|
57
|
+
end
|
data/spec/app/app.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'sinatra'
|
3
|
+
|
4
|
+
get '/:locale/posts/:name' do
|
5
|
+
@post = params[:name]
|
6
|
+
erb :post
|
7
|
+
end
|
8
|
+
|
9
|
+
get '/:locale/posts/:name/comments' do
|
10
|
+
i18n.post.comments(3).to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
get '/locale' do
|
14
|
+
i18n.locale['title']
|
15
|
+
end
|
16
|
+
|
17
|
+
get '/locales' do
|
18
|
+
i18n.translations.map { |i| i.join(': ') }.join("; ")
|
19
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1><%= i18n.post.title(@post) %></h1>
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe Sinatra::R18n do
|
5
|
+
|
6
|
+
it "should translate messages" do
|
7
|
+
get '/ru/posts/1'
|
8
|
+
response.should be_ok
|
9
|
+
response.body.should == "<h1>Запись 1</h1>\n"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should use translations from default locale" do
|
13
|
+
get '/ru/posts/1/comments'
|
14
|
+
response.should be_ok
|
15
|
+
response.body.should == '3 comments'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should use default locale" do
|
19
|
+
set :default_locale, 'ru'
|
20
|
+
get '/locale'
|
21
|
+
response.should be_ok
|
22
|
+
response.body.should == 'Русский'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should autodetect user locale" do
|
26
|
+
get '/locale', :env => {'HTTP_ACCEPT_LANGUAGE' => 'ru,en;q=0.9'}
|
27
|
+
response.should be_ok
|
28
|
+
response.body.should == 'Русский'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should use locale from session" do
|
32
|
+
get '/locale', :env => { 'rack.session' => { :locale => 'ru' } }
|
33
|
+
response.should be_ok
|
34
|
+
response.body.should == 'Русский'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return locales list" do
|
38
|
+
get '/locales'
|
39
|
+
response.should be_ok
|
40
|
+
response.body.should == 'ru: Русский; en: English'
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), '../lib/sinatra/r18n')
|
3
|
+
require File.join(File.dirname(__FILE__), 'app/app')
|
4
|
+
|
5
|
+
require 'spec/interop/test'
|
6
|
+
require 'sinatra/test'
|
7
|
+
Test::Unit::TestCase.send :include, Sinatra::Test
|
8
|
+
|
9
|
+
set :environment, :test
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-r18n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey "A.I." Sitnik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-16 00:00:00 +04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: r18n-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.2.1
|
24
|
+
version:
|
25
|
+
description: A Sinatra extension that provides i18n support to translate your web application. It is just a wrap for R18n core library. It can format numbers and time to the rules of the user locale, has translation for common words, storage translation in YAML format with pluralization and procedures and has special support for countries with two official languages.
|
26
|
+
email: andrey@sitnik.ru
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- LICENSE
|
34
|
+
files:
|
35
|
+
- lib/sinatra
|
36
|
+
- lib/sinatra/r18n.rb
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://r18n.rubyforge.org/
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: sinatra-r18n
|
61
|
+
rubygems_version: 1.3.1
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: A Sinatra extension that provides i18n support to translate your web application.
|
65
|
+
test_files:
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/sinatra-r18n_spec.rb
|
68
|
+
- spec/app
|
69
|
+
- spec/app/i18n
|
70
|
+
- spec/app/i18n/ru.yml
|
71
|
+
- spec/app/i18n/en.yml
|
72
|
+
- spec/app/app.rb
|
73
|
+
- spec/app/views
|
74
|
+
- spec/app/views/post.erb
|