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.
@@ -0,0 +1,3 @@
1
+ require 'roda'
2
+ require 'roda/i18n/version'
3
+ require 'roda/plugins/i18n'
@@ -0,0 +1,7 @@
1
+ class Roda
2
+
3
+ module I18n
4
+ VERSION = '0.2.0'
5
+ end
6
+
7
+ end
@@ -1,7 +1,11 @@
1
1
  require 'r18n-core'
2
2
 
3
+ #
3
4
  class Roda
5
+
6
+ #
4
7
  module RodaPlugins
8
+
5
9
  # The i18n plugin allows you to easily add internationalisation (i18n) and
6
10
  # localisation support to your Roda app, by adding the following:
7
11
  #
@@ -15,16 +19,16 @@ class Roda
15
19
  # plugin :i18n, :locale => ['de'], :translations => ['absolute/path/2/i18n']
16
20
  #
17
21
  # Please note!
18
- # 1) You must set <tt>opts[:root]</tt> in your app if you don't define the <tt>:translations</tt> path.
22
+ # 1) You must set +opts[:root]+ in your app if you don't define the +:translations+ path.
19
23
  #
20
- # 2) When overriding <tt>:translations</tt> the path given must be absolute.
24
+ # 2) When overriding <tt>:translations</tt> the path given <b>must be absolute</b>.
21
25
  #
22
26
  # The path supports 'wildcards', ie: path/**/i18n so you can load translations from multiple
23
27
  # combined apps each with their own <tt>i18n</tt> folder with translations.
24
28
  #
25
29
  # Note! when loading translations from multiple sources and the same translation key is used
26
- # in both files, the first loaded file takes precedence, ie: <tt>./i18n/en.yml</tt> takes precedence over
27
- # <tt>./apps/app1/i18n/en.yml</tt>
30
+ # in both files, the first loaded file takes precedence, ie: <tt>./i18n/en.yml</tt> takes
31
+ # precedence over <tt>./apps/app1/i18n/en.yml</tt>
28
32
  #
29
33
  # == USAGE
30
34
  #
@@ -56,7 +60,7 @@ class Roda
56
60
  # l Time.now, :human #=> "now"
57
61
  # l Time.now, :full #=> "3rd of January, 2010 18:54"
58
62
  #
59
- # Both the <tt>:t</tt> and <tt>:l</tt> methods are available in the route and template (erb) scopes. ie:
63
+ # Both the +:t+ and +:l+ methods are available in the route and template (erb) scopes. ie:
60
64
  #
61
65
  # route do |r|
62
66
  # r.root do
@@ -84,7 +88,7 @@ class Roda
84
88
  #
85
89
  # route do |r|
86
90
  #
87
- # r.locale do
91
+ # r.locale do # or r.i18n_locale
88
92
  # r.is 'posts' do
89
93
  # t.posts.header
90
94
  # end
@@ -95,8 +99,7 @@ class Roda
95
99
  #
96
100
  # === <tt>:i18n_set_locale_from</tt> RequestMethod
97
101
  #
98
- # Obtains the locale from either ENV, HTTP (browser), Params or Session
99
- # values
102
+ # Obtains the locale from either ENV, HTTP (browser), Params or Session values
100
103
  #
101
104
  #
102
105
  # Naturally we can allow browsers to override the default locale within routes, like this:
@@ -123,30 +126,51 @@ class Roda
123
126
  #
124
127
  module RodaI18n
125
128
 
126
- def self.load_dependencies(app, opts={})
127
- # app.plugin :render
128
- # app.plugin :environments
129
- end
129
+ # default options
130
+ OPTS = {
131
+ # set the default locale
132
+ locale: 'en',
133
+ # set the default fallback locale
134
+ default_locale: 'en',
135
+ # set the default translations.
136
+ translations: nil
137
+ }.freeze
130
138
 
131
- def self.configure(app, opts={})
132
- opts = app.opts[:i18n][:orig_opts].merge(opts) if app.opts[:i18n]
133
- app.opts[:i18n] = opts.dup
139
+
140
+ def self.configure(app, opts = OPTS)
141
+ if app.opts[:i18n]
142
+ opts = app.opts[:i18n][:orig_opts].merge(opts)
143
+ else
144
+ opts = OPTS.merge(opts)
145
+ end
146
+
147
+ app.opts[:i18n] = opts.dup
134
148
  app.opts[:i18n][:orig_opts] = opts
135
149
  opts = app.opts[:i18n]
136
150
 
137
- ::R18n.default_places = opts[:translations] || File.expand_path('i18n', app.opts[:root])
151
+ # set the translations path to defaults if nil
152
+ opts[:translations] = File.expand_path('i18n', app.opts[:root]) if opts[:translations].nil?
153
+ ::R18n.default_places = opts[:translations]
138
154
 
139
- opts[:default_locale] = opts[:locale].is_a?(Array) ? opts[:locale].first : opts[:locale] || 'en'
155
+ # default_locale is either 'en' or the set value, so reset :default_locale if
156
+ # it is somehow nil or an empty string ' '
157
+ if opts[:default_locale].nil? || opts[:default_locale] =~ /^\s*$/
158
+ opts[:default_locale] = 'en'
159
+ end
140
160
  ::R18n::I18n.default = opts[:default_locale]
141
161
 
142
162
  ::R18n.clear_cache! if ENV['RACK_ENV'] != 'production'
143
- i18n = R18n::I18n.new(opts[:locale], ::R18n.default_places,
144
- off_filters: :untranslated,
145
- on_filters: :untranslated_html)
163
+ i18n = R18n::I18n.new(
164
+ opts[:locale],
165
+ ::R18n.default_places,
166
+ off_filters: :untranslated,
167
+ on_filters: :untranslated_html
168
+ )
146
169
  ::R18n.set(i18n)
147
170
  end
148
171
 
149
-
172
+ # methods used within Roda's route block
173
+ #
150
174
  module RequestMethods
151
175
 
152
176
  # Obtains the locale from either ENV, HTTP (browser), Params or Session
@@ -155,16 +179,33 @@ class Roda
155
179
  # route do |r|
156
180
  # # A): set from URL params ie: GET /posts?locale=de
157
181
  # r.i18n_set_locale_from(:params)
182
+ #
183
+ # /url?locale=de
184
+ # <%= t.one %> #=> Ein
185
+ # /url?locale=es
186
+ # <%= t.one %> #=> Uno
158
187
  #
159
188
  # # B): set from session[:locale] (if present)
160
189
  # r.i18n_set_locale_from(:session)
190
+ #
191
+ # session[:locale] = 'de'
192
+ # <%= t.one %> #=> Ein
193
+ # session[:locale] = 'es'
194
+ # <%= t.one %> #=> Uno
161
195
  #
162
196
  # # C): set from the browser's HTTP request locale
163
197
  # r.i18n_set_locale_from(:http)
198
+ #
199
+ # HTTP_ACCEPT_LANGUAGE = 'sv-se;q=1,es;q=0.8,en;q=0.6'
200
+ # <%= t.one %> #=> Ett
164
201
  #
165
202
  # # D): set from the server ENV['LANG'] variable
166
203
  # r.i18n_set_locale_from(:ENV)
167
204
  #
205
+ # ENV['LANG'] = 'en_US.UTF8'
206
+ # <%= t.one %> #=> One
207
+ # ENV['LANG'] = 'es'
208
+ # <%= t.one %> #=> Uno
168
209
  #
169
210
  # r.is 'posts' do
170
211
  # t.posts.header # use translations
@@ -174,26 +215,30 @@ class Roda
174
215
  def i18n_set_locale_from(type)
175
216
  case type.to_sym
176
217
  when :http
177
- _locale = ::R18n::I18n.parse_http(scope.request.env['HTTP_ACCEPT_LANGUAGE'])
218
+ loc = ::R18n::I18n.parse_http(scope.request.env['HTTP_ACCEPT_LANGUAGE'])
178
219
  when :session
179
- _locale = session[:locale] if session[:locale]
220
+ loc = session[:locale] if session[:locale]
180
221
  when :params
181
- _locale = scope.request.params['locale'] if scope.request.params['locale']
222
+ loc = scope.request.params['locale'] if scope.request.params['locale']
182
223
  when :ENV
183
- _locale = ENV['LANG'].split('.').first if ENV['LANG']
224
+ # ENV['LANG']="en_US.UTF-8"
225
+ loc = ENV['LANG'].split('.').first if ENV['LANG']
184
226
  else
185
- _locale = nil
227
+ loc = nil
186
228
  end
187
229
  # sanity check: set to default locale if not set above
188
- _locale = ::R18n::I18n.default.to_s if _locale.nil?
230
+ loc = ::R18n::I18n.default.to_s if loc.nil?
189
231
 
190
- _i18n = ::R18n::I18n.new(_locale, ::R18n.default_places,
191
- off_filters: :untranslated,
192
- on_filters: :untranslated_html)
193
- ::R18n.set(_i18n)
232
+ i18n = ::R18n::I18n.new(
233
+ loc,
234
+ ::R18n.default_places,
235
+ off_filters: :untranslated,
236
+ on_filters: :untranslated_html
237
+ )
238
+ ::R18n.set(i18n)
194
239
  end
195
240
 
196
- # Enables setting temporary :locale.
241
+ # Enables setting temporary :locale blocks within the routing block.
197
242
  #
198
243
  # route do |r|
199
244
  #
@@ -210,11 +255,15 @@ class Roda
210
255
  def i18n_set_locale(locale, &blk)
211
256
  locale = ::R18n::I18n.default.to_s if locale.nil?
212
257
 
213
- _i18n = ::R18n::I18n.new(locale, ::R18n.default_places,
214
- off_filters: :untranslated,
215
- on_filters: :untranslated_html)
216
- ::R18n.set(_i18n)
217
- yield
258
+ i18n = ::R18n::I18n.new(
259
+ locale,
260
+ ::R18n.default_places,
261
+ off_filters: :untranslated,
262
+ on_filters: :untranslated_html
263
+ )
264
+ ::R18n.set(i18n)
265
+ yield if block_given?
266
+ # return # NB!! needed to enable routes below to work
218
267
  end
219
268
 
220
269
  # Sets the locale based upon <tt>:locale</tt> prefixed routes
@@ -230,28 +279,31 @@ class Roda
230
279
  # end
231
280
  # end
232
281
  #
233
- def locale(opts={}, &blk)
282
+ def locale(opts = {}, &blk)
234
283
  on(':locale', opts) do |l|
235
- _locale = l || self.class.opts[:locale]
236
- session[:locale] = _locale unless session[:locale]
237
- ::R18n.set(_locale)
238
- yield
284
+ loc = l || self.class.opts[:locale]
285
+ session[:locale] = loc unless session[:locale]
286
+ ::R18n.set(loc)
287
+ yield if block_given?
288
+ return # NB!! needed to enable routes below to work
239
289
  end
240
290
  end
241
291
  alias_method :i18n_locale, :locale
242
292
 
293
+ end # /module RequestMethods
243
294
 
244
- end #/module RequestMethods
245
295
 
246
296
  module ClassMethods
247
297
 
248
- # Return the i18n options for this class.
298
+ # Return the i18n options for this plugin.
249
299
  def i18n_opts
250
300
  opts[:i18n]
251
301
  end
252
302
 
253
- end #/module ClassMethods
303
+ end # /module ClassMethods
304
+
254
305
 
306
+ # defines method available within the views / routing block
255
307
  module InstanceMethods
256
308
  include ::R18n::Helpers
257
309
 
@@ -269,8 +321,8 @@ class Roda
269
321
 
270
322
  end
271
323
 
272
- end #/module RodaI18n
324
+ end # /module RodaI18n
273
325
 
274
326
  register_plugin(:i18n, RodaI18n)
275
- end #/module RodaPlugins
276
- end #/class Roda
327
+ end # /module RodaPlugins
328
+ end # /class Roda
@@ -0,0 +1,50 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'roda/i18n/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'roda-i18n'
8
+ spec.version = ::Roda::I18n::VERSION
9
+ spec.authors = ["Kematzy"]
10
+ spec.email = ["kematzy@gmail.com"]
11
+
12
+ spec.summary = "Roda Internationalisation plugin"
13
+ spec.description = "The Roda-i18n plugin enables easy addition of internationalisation (i18n) and localisation support in Roda apps"
14
+ spec.homepage = "http://github.com/kematzy/roda-i18n"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.platform = Gem::Platform::RUBY
31
+ spec.has_rdoc = true
32
+ spec.extra_rdoc_files = ["README.md", "MIT-LICENSE"]
33
+ spec.rdoc_options += ["--quiet", "--line-numbers", "--inline-source", '--title', 'Roda-i18n: internationalisation plugin', '--main', 'README.md']
34
+
35
+ spec.add_runtime_dependency 'roda', '~> 2.5', '>= 2.5.0'
36
+ spec.add_runtime_dependency 'tilt'
37
+ spec.add_runtime_dependency "r18n-core", '~> 2.0', '>= 2.0.3'
38
+
39
+ spec.add_development_dependency 'bundler', "~> 1.10"
40
+ spec.add_development_dependency 'rake', "~> 10.0"
41
+ spec.add_development_dependency 'erubis'
42
+ spec.add_development_dependency 'kramdown'
43
+ spec.add_development_dependency "minitest", '~> 5.7', '>= 5.7.0'
44
+ spec.add_development_dependency "minitest-hooks", '~> 1.1', '>= 1.1.0'
45
+ spec.add_development_dependency 'minitest-rg'
46
+ spec.add_development_dependency "rack-test", '~> 0.6.3'
47
+ spec.add_development_dependency 'nokogiri'
48
+ spec.add_development_dependency 'simplecov'
49
+
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kematzy
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roda
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 2.5.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: tilt
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: r18n-core
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -50,6 +64,62 @@ dependencies:
50
64
  - - ">="
51
65
  - !ruby/object:Gem::Version
52
66
  version: 2.0.3
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.10'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.10'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '10.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: erubis
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: kramdown
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
53
123
  - !ruby/object:Gem::Dependency
54
124
  name: minitest
55
125
  requirement: !ruby/object:Gem::Requirement
@@ -90,6 +160,20 @@ dependencies:
90
160
  - - ">="
91
161
  - !ruby/object:Gem::Version
92
162
  version: 1.1.0
163
+ - !ruby/object:Gem::Dependency
164
+ name: minitest-rg
165
+ requirement: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ type: :development
171
+ prerelease: false
172
+ version_requirements: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
93
177
  - !ruby/object:Gem::Dependency
94
178
  name: rack-test
95
179
  requirement: !ruby/object:Gem::Requirement
@@ -104,28 +188,53 @@ dependencies:
104
188
  - - "~>"
105
189
  - !ruby/object:Gem::Version
106
190
  version: 0.6.3
191
+ - !ruby/object:Gem::Dependency
192
+ name: nokogiri
193
+ requirement: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ - !ruby/object:Gem::Dependency
206
+ name: simplecov
207
+ requirement: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ type: :development
213
+ prerelease: false
214
+ version_requirements: !ruby/object:Gem::Requirement
215
+ requirements:
216
+ - - ">="
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
107
219
  description: The Roda-i18n plugin enables easy addition of internationalisation (i18n)
108
220
  and localisation support in Roda apps
109
- email: kematzy@gmail.com
221
+ email:
222
+ - kematzy@gmail.com
110
223
  executables: []
111
224
  extensions: []
112
225
  extra_rdoc_files:
113
226
  - README.md
114
227
  - MIT-LICENSE
115
228
  files:
229
+ - ".gitignore"
230
+ - Gemfile
116
231
  - MIT-LICENSE
117
232
  - README.md
118
233
  - Rakefile
234
+ - lib/roda/i18n.rb
235
+ - lib/roda/i18n/version.rb
119
236
  - lib/roda/plugins/i18n.rb
120
- - spec/fixtures/app/i18n/de.yml
121
- - spec/fixtures/app/i18n/en.yml
122
- - spec/fixtures/app/i18n/es.yml
123
- - spec/fixtures/i18n/de.yml
124
- - spec/fixtures/i18n/en.yml
125
- - spec/fixtures/i18n/sv-se.yml
126
- - spec/roda_i18n_coverage.rb
127
- - spec/roda_i18n_spec.rb
128
- - spec/spec_helper.rb
237
+ - roda-i18n.gemspec
129
238
  homepage: http://github.com/kematzy/roda-i18n
130
239
  licenses:
131
240
  - MIT