roda-i18n 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/Gemfile +19 -0
- data/README.md +150 -125
- data/Rakefile +9 -66
- data/lib/roda/i18n.rb +3 -0
- data/lib/roda/i18n/version.rb +7 -0
- data/lib/roda/plugins/i18n.rb +100 -48
- data/roda-i18n.gemspec +50 -0
- metadata +122 -13
- data/spec/fixtures/app/i18n/de.yml +0 -14
- data/spec/fixtures/app/i18n/en.yml +0 -14
- data/spec/fixtures/app/i18n/es.yml +0 -8
- data/spec/fixtures/i18n/de.yml +0 -34
- data/spec/fixtures/i18n/en.yml +0 -68
- data/spec/fixtures/i18n/sv-se.yml +0 -53
- data/spec/roda_i18n_coverage.rb +0 -13
- data/spec/roda_i18n_spec.rb +0 -413
- data/spec/spec_helper.rb +0 -98
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61da5676e50ba7ef694e025bfd717c279c0dfe01
|
4
|
+
data.tar.gz: 57e1b41f3d7856e464e51542fa9c6172f6fe93bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 741426239b80ef746c01ba0e3551855af218b12412147d8117da8a2524eba80502b7c934bca41ae1ad6cbbeaa2f1375581c08f639fe233776dbf69a397227437
|
7
|
+
data.tar.gz: 931482a0bbd46455887a2f67affc4a93c13329db1acf73c723e386f3cdf29aaecbe4f340cbcabee2ea4f2bdd8f83fc3f22243714eb4734966d602067befe3f17
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in roda-tags.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# gem 'roda', '~> 2.5.1'
|
7
|
+
# gem 'r18n-core'
|
8
|
+
#
|
9
|
+
# group :test do
|
10
|
+
# gem 'minitest'
|
11
|
+
# gem 'minitest-hooks'
|
12
|
+
# gem "rack-test"
|
13
|
+
#
|
14
|
+
# gem 'tilt'
|
15
|
+
# gem 'erubis'
|
16
|
+
# gem 'kramdown'
|
17
|
+
#
|
18
|
+
# gem 'simplecov', :require => false
|
19
|
+
# end
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Roda-i18n
|
2
2
|
|
3
|
-
|
3
|
+
Add Internationalisation (i18n) and localisation support to your [Roda](http://roda.jeremyevans.net/)
|
4
|
+
apps, based upon the [R18n](https://github.com/ai/r18n) gem.
|
4
5
|
|
5
6
|
Extensively tested and with 100% code test coverage.
|
6
7
|
|
@@ -9,120 +10,136 @@ Extensively tested and with 100% code test coverage.
|
|
9
10
|
|
10
11
|
To use this gem, just do
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
```bash
|
14
|
+
$ (sudo) gem install roda-i18n
|
15
|
+
```
|
16
|
+
|
14
17
|
or if you use Bundler
|
15
18
|
|
16
|
-
|
17
|
-
|
19
|
+
```ruby
|
20
|
+
gem "roda-i18n"
|
21
|
+
```
|
18
22
|
|
19
23
|
<br>
|
20
24
|
|
21
25
|
|
22
26
|
## Getting Started
|
23
27
|
|
24
|
-
To add internationalisation and localisation support to your app just add the following code snippet
|
28
|
+
To add internationalisation and localisation support to your app just add the following code snippet
|
29
|
+
in your app.
|
25
30
|
|
26
|
-
|
31
|
+
```ruby
|
32
|
+
plugin :i18n
|
33
|
+
```
|
27
34
|
|
28
|
-
By default the default locale is set to
|
35
|
+
By default the default locale is set to `'en'` and the translations directory is set to the `'i18n'`
|
36
|
+
directory in the root of your app, ie: `'/path/2/app/i18n'`.
|
29
37
|
|
30
38
|
|
31
|
-
**IMPORTANT! Make sure you create the 'i18n' folder and add an 'en.yml' file with
|
32
|
-
|
39
|
+
**IMPORTANT! Make sure you create the 'i18n' folder and add an `'en.yml'` file with at least one
|
40
|
+
translation within it.**
|
33
41
|
|
34
42
|
|
35
43
|
<br>
|
44
|
+
---
|
36
45
|
|
37
46
|
|
38
47
|
## Configuration
|
39
48
|
|
40
49
|
### Overriding defaults during configuration
|
41
50
|
|
42
|
-
Both
|
43
|
-
|
44
|
-
plugin :i18n, :locale => ['de'], :translations => ['absolute/path/2/i18n']
|
51
|
+
Both `:locale` and `:translations` can be configured (overridden) during plugin configuration:
|
45
52
|
|
53
|
+
```ruby
|
54
|
+
plugin :i18n, :locale => ['de'], :translations => ['absolute/path/2/i18n']
|
55
|
+
```
|
46
56
|
|
47
57
|
**NOTE!**
|
48
58
|
|
49
|
-
1. You must set
|
59
|
+
1. You must set `opts[:root]` in your app if you do not define the `:translations` path during
|
60
|
+
plugin configuration.
|
50
61
|
|
51
|
-
|
62
|
+
3. When overriding `:translations` the **any path(s) given must be absolute**.
|
52
63
|
|
53
64
|
|
54
65
|
#### Loading translations from multiple i18n directories
|
55
66
|
|
56
|
-
The
|
67
|
+
The `:translations` path supports 'wildcards', ie: `path/**/i18n` so you can load translations from
|
68
|
+
multiple combined apps, each with their own `i18n` folder with translations.
|
57
69
|
|
58
70
|
**Please Note!**
|
59
71
|
|
60
|
-
When loading translations from multiple sources and the same translation key is available in
|
72
|
+
When loading translations from multiple sources and the same translation key is available in
|
73
|
+
multiple files of the same locale, then **the translations in the first loaded translation file
|
74
|
+
takes precedence over subsequent loaded translations**.
|
61
75
|
|
62
|
-
* ie: translations in
|
76
|
+
* ie: translations in `./i18n/en.yml` takes precedence over translations in `./apps/app1/i18n/en.yml`
|
63
77
|
|
64
78
|
|
65
79
|
You can also set a list of preferred locales as an array ordered by priority.
|
66
80
|
|
67
|
-
|
68
|
-
|
81
|
+
```ruby
|
82
|
+
plugin :i18n, :locale => ['es','fr','en']
|
83
|
+
```
|
69
84
|
|
70
85
|
|
71
86
|
<br>
|
72
|
-
|
87
|
+
---
|
73
88
|
|
74
89
|
## USAGE
|
75
90
|
|
76
91
|
The **i18n** plugin depends upon simple YAML based translations files:
|
77
92
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
# app/views/layout.erb
|
117
|
-
<snip...>
|
118
|
-
<h1><%= t.welcome.message %></h1>
|
119
|
-
<snip...>
|
120
|
-
|
93
|
+
```ruby
|
94
|
+
# app/i18n/en.yml
|
95
|
+
|
96
|
+
user:
|
97
|
+
edit: Edit user
|
98
|
+
name: User name is %1
|
99
|
+
count: !!pl
|
100
|
+
1: There is 1 user
|
101
|
+
n: There are %1 users
|
102
|
+
```
|
103
|
+
|
104
|
+
...and the **`:t`** instance method to output the translations:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
t.user.edit #=> "Edit user"
|
108
|
+
t.user.name('John') #=> "User name is John"
|
109
|
+
t.user.count(5) #=> "There are 5 users"
|
110
|
+
|
111
|
+
t.does.not.exist | 'default' #=> "default"
|
112
|
+
```
|
113
|
+
|
114
|
+
...and the **`:l`** (lowercase L) instance method provides built-in localisations support:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
l Time.now #=> "03/01/2010 18:54"
|
118
|
+
l Time.now, :human #=> "now"
|
119
|
+
l Time.now, :full #=> "3rd of January, 2010 18:54"
|
120
|
+
```
|
121
|
+
|
122
|
+
|
123
|
+
Both the `:t` and `:l` methods are available within the route and template (erb) scopes. ie:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
route do |r|
|
127
|
+
r.root do
|
128
|
+
t.welcome.message
|
129
|
+
end
|
130
|
+
end
|
121
131
|
|
132
|
+
# app/views/layout.erb
|
133
|
+
<snip...>
|
134
|
+
<h1><%= t.welcome.message %></h1>
|
135
|
+
<snip...>
|
136
|
+
```
|
122
137
|
|
123
|
-
Please visit [R18n](https://github.com/ai/r18n/tree/master/r18n-core) for more information about the
|
138
|
+
Please visit [R18n](https://github.com/ai/r18n/tree/master/r18n-core) for more information about the
|
139
|
+
R18n gem used to create the above.
|
124
140
|
|
125
141
|
<br>
|
142
|
+
---
|
126
143
|
|
127
144
|
## Key Methods / Functionality
|
128
145
|
|
@@ -130,59 +147,63 @@ Please visit [R18n](https://github.com/ai/r18n/tree/master/r18n-core) for more i
|
|
130
147
|
<br>
|
131
148
|
|
132
149
|
|
133
|
-
###
|
150
|
+
### `#locale(opts = {}, &blk)` - (request method)
|
134
151
|
|
135
|
-
This request method makes it easy to handle translations based upon the
|
152
|
+
This request method makes it easy to handle translations based upon the **`:locale` prefix on a
|
153
|
+
route / URL**. ie: `blog.com/**de**/posts`.
|
136
154
|
|
137
155
|
|
138
156
|
To enable this, just use the following code structure:
|
139
157
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
end
|
151
|
-
|
158
|
+
```ruby
|
159
|
+
route do |r|
|
160
|
+
|
161
|
+
# all routes are prefixed with '/:locale'
|
162
|
+
# ie: GET /de/posts => will use DE translations
|
163
|
+
# ie: GET /es/posts => will use ES translations
|
164
|
+
|
165
|
+
r.locale do # also aliased as #i18n_locale
|
166
|
+
r.is 'posts' do
|
167
|
+
t.posts.header # use translations or locales
|
152
168
|
end
|
153
|
-
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
```
|
154
173
|
|
155
174
|
|
156
175
|
|
157
|
-
**NOTE!** Any URL / request with a non-present or not supported locale will be given the
|
176
|
+
**NOTE!** Any URL / request with a non-present or not supported locale will be given the
|
177
|
+
**configured default locale** or the EN (English) default locale.
|
158
178
|
|
159
179
|
|
160
180
|
<br>
|
161
181
|
|
162
182
|
|
163
|
-
###
|
183
|
+
### ``#i18n_set_locale_from(type)` - (request method)
|
164
184
|
|
165
185
|
Obtains the locale from either ENV, HTTP (browser), Params or Session values.
|
166
186
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
187
|
+
```ruby
|
188
|
+
route do |r|
|
189
|
+
# A): set from session[:locale] (if present)
|
190
|
+
r.i18n_set_locale_from(:session)
|
191
|
+
|
192
|
+
# B): set from URL params ie: GET /posts?locale=de
|
193
|
+
r.i18n_set_locale_from(:params)
|
194
|
+
|
195
|
+
# C): set from the browser's HTTP request locale
|
196
|
+
r.i18n_set_locale_from(:http)
|
197
|
+
|
198
|
+
# D): set from the server ENV['LANG'] variable
|
199
|
+
r.i18n_set_locale_from(:ENV)
|
200
|
+
|
201
|
+
|
202
|
+
r.is 'posts' do
|
203
|
+
t.posts.header # use translations
|
204
|
+
end
|
205
|
+
end
|
206
|
+
```
|
186
207
|
|
187
208
|
**NOTE!** defaults to the configured default locale, or English, if the given locale type is invalid.
|
188
209
|
|
@@ -190,30 +211,30 @@ Obtains the locale from either ENV, HTTP (browser), Params or Session values.
|
|
190
211
|
<br>
|
191
212
|
|
192
213
|
|
193
|
-
###
|
214
|
+
### `i18n_set_locale(locale, &blk)` - (request method)
|
194
215
|
|
195
|
-
Enables overriding the default locale and setting a temporary
|
196
|
-
within a route block.
|
216
|
+
Enables overriding the default locale and setting a temporary `:locale` within a route block.
|
197
217
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
r.i18n_set_locale('es') do
|
208
|
-
# within this route block the locale is ES (Spanish)
|
209
|
-
end
|
210
|
-
|
211
|
-
end
|
218
|
+
```ruby
|
219
|
+
route do |r|
|
220
|
+
# configured default locale
|
221
|
+
|
222
|
+
<snip...>
|
223
|
+
|
224
|
+
r.i18n_set_locale('de') do
|
225
|
+
# within this route block the locale is DE (German)
|
226
|
+
end
|
212
227
|
|
228
|
+
r.i18n_set_locale('es') do
|
229
|
+
# within this route block the locale is ES (Spanish)
|
230
|
+
end
|
213
231
|
|
232
|
+
end
|
233
|
+
```
|
214
234
|
|
235
|
+
<br>
|
236
|
+
---
|
215
237
|
|
216
|
-
|
217
238
|
## InstanceMethods
|
218
239
|
|
219
240
|
### `#t`
|
@@ -236,8 +257,9 @@ Key localisation method. Handles dates etc. (See examples above)
|
|
236
257
|
|
237
258
|
Returns a two-dimensional array of available locales.
|
238
259
|
|
239
|
-
|
240
|
-
|
260
|
+
```ruby
|
261
|
+
puts i18n_available_locales #=> [ ['en', 'English'], ...]
|
262
|
+
```
|
241
263
|
|
242
264
|
|
243
265
|
|
@@ -249,12 +271,13 @@ Returns a two-dimensional array of available locales.
|
|
249
271
|
|
250
272
|
## Class Methods
|
251
273
|
|
252
|
-
###
|
274
|
+
### `#i18n_opts()`
|
253
275
|
|
254
276
|
Return the i18n options for this class as a Hash.
|
255
277
|
|
256
278
|
|
257
279
|
<br>
|
280
|
+
---
|
258
281
|
|
259
282
|
|
260
283
|
## Ideas
|
@@ -264,8 +287,9 @@ A few ideas that may be outlandish, but possible?
|
|
264
287
|
|
265
288
|
### Ability to load translations from multiple locations via an array.
|
266
289
|
|
267
|
-
|
268
|
-
|
290
|
+
```ruby
|
291
|
+
plugin :i18n, :translations => ['app1/i18n', 'app2/i18n', 'app3/i18n']
|
292
|
+
```
|
269
293
|
|
270
294
|
> [Concept Reference](https://github.com/ai/r18n/tree/master/r18n-core#loaders)
|
271
295
|
>
|
@@ -300,7 +324,8 @@ Som form of built-in support for storing / loading translations from a Sequel ba
|
|
300
324
|
|
301
325
|
## Credits
|
302
326
|
|
303
|
-
* This plugin have been inspired by the `sinatra-i18n` gem available at
|
327
|
+
* This plugin have been inspired by the `sinatra-i18n` gem available at
|
328
|
+
[github/ai/r18n](http://github.com/ai/r18n) created by [Andrey Sitnik](http://github.com/ai).
|
304
329
|
|
305
330
|
* Testing code have been partly copied from [Forme](github.com/jeremyevans/forme).
|
306
331
|
|
data/Rakefile
CHANGED
@@ -1,53 +1,12 @@
|
|
1
|
-
require "
|
2
|
-
require "rake/
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
desc "Packages #{NAME}"
|
10
|
-
task :package=>[:clean] do |p|
|
11
|
-
sh %{gem build #{NAME}.gemspec}
|
12
|
-
end
|
13
|
-
|
14
|
-
desc "Upload #{NAME} gem to rubygems"
|
15
|
-
task :release=>[:package] do
|
16
|
-
sh %{gem push ./#{NAME}-#{VERS.call}.gem}
|
4
|
+
Rake::TestTask.new(:spec) do |t|
|
5
|
+
t.libs << "spec"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
17
8
|
end
|
18
9
|
|
19
|
-
# ### RDoc
|
20
|
-
#
|
21
|
-
# RDOC_DEFAULT_OPTS = ["--line-numbers", "--inline-source", '--title', 'Roda I18n']
|
22
|
-
#
|
23
|
-
# begin
|
24
|
-
# gem 'hanna-nouveau'
|
25
|
-
# RDOC_DEFAULT_OPTS.concat(['-f', 'hanna'])
|
26
|
-
# rescue Gem::LoadError
|
27
|
-
# end
|
28
|
-
#
|
29
|
-
# rdoc_task_class = begin
|
30
|
-
# require "rdoc/task"
|
31
|
-
# RDoc::Task
|
32
|
-
# rescue LoadError
|
33
|
-
# require "rake/rdoctask"
|
34
|
-
# Rake::RDocTask
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# RDOC_OPTS = RDOC_DEFAULT_OPTS + ['--main', 'README.rdoc']
|
38
|
-
#
|
39
|
-
# rdoc_task_class.new do |rdoc|
|
40
|
-
# rdoc.rdoc_dir = "rdoc"
|
41
|
-
# rdoc.options += RDOC_OPTS
|
42
|
-
# rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
|
43
|
-
# end
|
44
|
-
|
45
|
-
### Specs
|
46
|
-
|
47
|
-
desc "Run specs"
|
48
|
-
task :spec do
|
49
|
-
sh "#{FileUtils::RUBY} -rubygems -I lib -e 'ARGV.each{|f| require f}' ./spec/*_spec.rb"
|
50
|
-
end
|
51
10
|
task :default => :spec
|
52
11
|
|
53
12
|
desc "Run specs with coverage"
|
@@ -56,23 +15,7 @@ task :coverage do
|
|
56
15
|
Rake::Task['spec'].invoke
|
57
16
|
end
|
58
17
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
# task :version do
|
63
|
-
# puts VERS.call
|
64
|
-
# end
|
65
|
-
|
66
|
-
desc "Check syntax of all .rb files"
|
67
|
-
task :check_syntax do
|
68
|
-
Dir['**/*.rb'].each{|file| print `#{ENV['RUBY'] || :ruby} -c #{file} | fgrep -v "Syntax OK"`}
|
18
|
+
desc "Run Rubocop report"
|
19
|
+
task :rubocop do
|
20
|
+
`rubocop -f html -o ./Rubocop-report.html lib/`
|
69
21
|
end
|
70
|
-
|
71
|
-
# desc "Start an IRB shell using the extension"
|
72
|
-
# task :irb do
|
73
|
-
# require 'rbconfig'
|
74
|
-
# ruby = ENV['RUBY'] || File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
|
75
|
-
# irb = ENV['IRB'] || File.join(RbConfig::CONFIG['bindir'], File.basename(ruby).sub('ruby', 'irb'))
|
76
|
-
# sh %{#{irb} -I lib -r forme}
|
77
|
-
# end
|
78
|
-
|