rails-translate-routes 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/README.rdoc +75 -22
- data/VERSION +1 -1
- data/lib/rails-translate-routes.rb +19 -9
- data/rails-translate-routes.gemspec +2 -2
- metadata +13 -13
data/Gemfile.lock
CHANGED
@@ -39,13 +39,13 @@ GEM
|
|
39
39
|
bundler (~> 1.0)
|
40
40
|
git (>= 1.2.5)
|
41
41
|
rake
|
42
|
-
json (1.6.
|
42
|
+
json (1.6.5)
|
43
43
|
mail (2.3.0)
|
44
44
|
i18n (>= 0.4.0)
|
45
45
|
mime-types (~> 1.16)
|
46
46
|
treetop (~> 1.4.8)
|
47
47
|
mime-types (1.17.2)
|
48
|
-
minitest (2.10.
|
48
|
+
minitest (2.10.1)
|
49
49
|
multi_json (1.0.4)
|
50
50
|
polyglot (0.3.3)
|
51
51
|
rack (1.3.6)
|
data/README.rdoc
CHANGED
@@ -4,7 +4,7 @@ Rails 3.x routes translations based on Raul's translate_routes (https://github.c
|
|
4
4
|
|
5
5
|
It's currently a stripped down version of the forked gem, adding some bugfixes for rails 3.1 and features I needed for my project. See doc below to see what it can do.
|
6
6
|
|
7
|
-
translate_routes & i18n_routes seems to be unmaintained projects so I decided to start a fork of the first one for my own projects, I can't promise high dedication but I'll to maintain it for my own use and take care all patches & bugs submitted, help is welcome!
|
7
|
+
translate_routes & i18n_routes seems to be unmaintained projects so I decided to start a fork of the first one for my own projects, I can't promise high dedication but I'll try to maintain it for my own use and take care all patches & bugs submitted, help is welcome!
|
8
8
|
|
9
9
|
== Installation
|
10
10
|
|
@@ -68,29 +68,32 @@ Also remember to include language detection to your app, a simple example of an
|
|
68
68
|
|
69
69
|
And that's it! Now if we execute 'rake routes':
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
edit_product_en GET /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
78
|
-
edit_product_es GET /es/productos/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
71
|
+
products_en GET /products(.:format) {:action=>"index", :controller=>"products"}
|
72
|
+
products_es GET /es/productos(.:format) {:action=>"index", :controller=>"products"}
|
73
|
+
POST /products(.:format) {:action=>"create", :controller=>"products"}
|
74
|
+
POST /es/productos(.:format) {:action=>"create", :controller=>"products"}
|
75
|
+
new_product_en GET /products/new(.:format) {:action=>"new", :controller=>"products"}
|
76
|
+
new_product_es GET /es/productos/new(.:format) {:action=>"new", :controller=>"products"}
|
77
|
+
edit_product_en GET /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
78
|
+
edit_product_es GET /es/productos/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
79
|
+
product_en GET /products/:id(.:format) {:action=>"show", :controller=>"products"}
|
80
|
+
product_es GET /es/productos/:id(.:format) {:action=>"show", :controller=>"products"}
|
81
|
+
PUT /products/:id(.:format) {:action=>"update", :controller=>"products"}
|
82
|
+
PUT /es/productos/:id(.:format) {:action=>"update", :controller=>"products"}
|
83
|
+
DELETE /products/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
84
|
+
DELETE /es/productos/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
85
|
+
contact_en /contact(.:format) {:action=>"contact", :controller=>"pages"}
|
86
|
+
contact_es /es/contacto(.:format) {:action=>"contact", :controller=>"pages"}
|
87
|
+
root_en / {:controller=>"public", :action=>"index"}
|
88
|
+
root_es /es {:controller=>"public", :action=>"index"}
|
88
89
|
|
89
90
|
The application recognizes the different routes and sets the 'I18n.locale' value in controllers, but what about the routes generation? As you can see on the previous rake routes execution, the 'contact_es_path' and 'contact_en_path' routing helpers have been generated and are available in your controllers and views. Additionally, a 'contact_path' helper has been generated, which generates the routes according to the current request's locale. This means that if you use named routes you don't need to modify your application links because the routing helpers are automatically adapted to the current locale.
|
90
91
|
|
91
|
-
|
92
|
+
== URL structure options
|
93
|
+
|
94
|
+
<b>Default URL structure</b>
|
92
95
|
|
93
|
-
|
96
|
+
By default it generates the following url structure:
|
94
97
|
|
95
98
|
/
|
96
99
|
/es
|
@@ -99,6 +102,9 @@ As you can see by default it generates the following url structure:
|
|
99
102
|
/contact/
|
100
103
|
/es/contactos/
|
101
104
|
|
105
|
+
|
106
|
+
<b>All languages prefixed</b>
|
107
|
+
|
102
108
|
In case you want the default languages to be scoped resulting in the following structure:
|
103
109
|
|
104
110
|
/en
|
@@ -123,12 +129,51 @@ A simple example of a redirection to user locale in index method:
|
|
123
129
|
def index
|
124
130
|
unless params[ :locale]
|
125
131
|
# it takes I18n.locale from the previous example set_locale as before_filter in application controller
|
126
|
-
redirect_to eval("root_#{I18n.locale}_path
|
132
|
+
redirect_to eval("root_#{I18n.locale}_path")
|
127
133
|
end
|
128
134
|
|
129
135
|
# rest of the controller logic ...
|
130
136
|
end
|
131
137
|
|
138
|
+
<b>No prefixed languages</b>
|
139
|
+
|
140
|
+
In case you don't want the language prefix in the url path because you have a domain or subdomain per language (or any other reason). Resulting in this structure:
|
141
|
+
|
142
|
+
/
|
143
|
+
/products/
|
144
|
+
/productos/
|
145
|
+
/contact/
|
146
|
+
/contactos/
|
147
|
+
|
148
|
+
You can specify the following option:
|
149
|
+
|
150
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :no_prefixes => true })
|
151
|
+
|
152
|
+
Note that the 'no_prefixes' option will override the 'prefix_on_default_locale' option.
|
153
|
+
|
154
|
+
<b>Namespaced backends</b>
|
155
|
+
|
156
|
+
I usually build app backend in namespaced controllers, routes, ... using 'prefix_on_default_locale' will result in having several routes for backend too, for example:
|
157
|
+
|
158
|
+
/en/admin/products
|
159
|
+
/es/admin/productos
|
160
|
+
|
161
|
+
In most cases you won't want to have the backend in several languages, you can set 'routes.rb' this way:
|
162
|
+
|
163
|
+
SampleApp::Application.routes.draw do
|
164
|
+
resources :products
|
165
|
+
match 'contact', :to => 'pages#contact'
|
166
|
+
end
|
167
|
+
|
168
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :prefix_on_default_locale => true })
|
169
|
+
|
170
|
+
SampleApp::Application.routes.draw do
|
171
|
+
namespace :admin do
|
172
|
+
resources :products
|
173
|
+
root :to => "admin_products#index"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
132
177
|
== TODO
|
133
178
|
|
134
179
|
Help is welcome ;)
|
@@ -140,6 +185,10 @@ Help is welcome ;)
|
|
140
185
|
|
141
186
|
Thanks to:
|
142
187
|
|
188
|
+
* Contributors of the current gem:
|
189
|
+
* Martin Carel (https://github.com/cawel)
|
190
|
+
* Johan Gyllenspetz (https://github.com/gyllen)
|
191
|
+
|
143
192
|
* Main development of forked gem:
|
144
193
|
* Raul Murciano (http://github.com/raul)
|
145
194
|
|
@@ -160,4 +209,8 @@ Thanks to:
|
|
160
209
|
There are other two projects for translating routes in Rails (which I know of), both of them are unfortunately unmaintained but you may want to check them out if you use Rails 2 or have different needs.
|
161
210
|
|
162
211
|
* translate_routes (https://github.com/raul/translate_routes)
|
163
|
-
* i18n_routing (https://github.com/kwi/i18n_routing)
|
212
|
+
* i18n_routing (https://github.com/kwi/i18n_routing)
|
213
|
+
|
214
|
+
== Other i18n related projects
|
215
|
+
|
216
|
+
If you also need to translate models check out: https://github.com/francesc/rails-translate-models
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# coding: UTF-8
|
2
2
|
|
3
|
-
# This class knows nothing
|
4
|
-
#
|
5
|
-
# test without an Rails App.
|
3
|
+
# This class knows nothing about Rails.root or Rails.application.routes,
|
4
|
+
# and therefore is easier to test without a Rails app.
|
6
5
|
class RailsTranslateRoutes
|
7
|
-
TRANSLATABLE_SEGMENT = /^(\w+)(\()?/.freeze
|
6
|
+
TRANSLATABLE_SEGMENT = /^([\w-]+)(\()?/.freeze
|
8
7
|
LOCALE_PARAM_KEY = :locale.freeze
|
9
8
|
ROUTE_HELPER_CONTAINER = [
|
10
9
|
ActionController::Base,
|
@@ -45,6 +44,14 @@ class RailsTranslateRoutes
|
|
45
44
|
@prefix_on_default_locale = locale.to_s
|
46
45
|
end
|
47
46
|
|
47
|
+
def no_prefixes
|
48
|
+
@no_prefixes ||= false
|
49
|
+
end
|
50
|
+
|
51
|
+
def no_prefixes= no_prefixes
|
52
|
+
@no_prefixes = no_prefixes
|
53
|
+
end
|
54
|
+
|
48
55
|
class << self
|
49
56
|
# Default locale suffix generator
|
50
57
|
def locale_suffix locale
|
@@ -228,7 +235,9 @@ class RailsTranslateRoutes
|
|
228
235
|
|
229
236
|
# Add prefix for all non-default locales
|
230
237
|
def add_prefix? locale
|
231
|
-
if
|
238
|
+
if @no_prefixes
|
239
|
+
false
|
240
|
+
elsif !default_locale?(locale) || @prefix_on_default_locale
|
232
241
|
true
|
233
242
|
else
|
234
243
|
false
|
@@ -247,7 +256,7 @@ class RailsTranslateRoutes
|
|
247
256
|
end
|
248
257
|
|
249
258
|
# Tries to translate a single path segment. If the path segment
|
250
|
-
# contains sth. like
|
259
|
+
# contains sth. like an optional format "people(.:format)", only
|
251
260
|
# "people" will be translated, if there is no translation, the path
|
252
261
|
# segment is blank or begins with a ":" (param key), the segment
|
253
262
|
# is returned untouched
|
@@ -256,7 +265,7 @@ class RailsTranslateRoutes
|
|
256
265
|
|
257
266
|
match = TRANSLATABLE_SEGMENT.match(segment)[1] rescue nil
|
258
267
|
|
259
|
-
(translate_string(match, locale) || segment)
|
268
|
+
(translate_string(match, locale) || segment)
|
260
269
|
end
|
261
270
|
|
262
271
|
def translate_string str, locale
|
@@ -287,7 +296,7 @@ class RailsTranslateRoutes
|
|
287
296
|
end
|
288
297
|
end
|
289
298
|
|
290
|
-
# Adapter for Rails 3
|
299
|
+
# Adapter for Rails 3 apps
|
291
300
|
module ActionDispatch
|
292
301
|
module Routing
|
293
302
|
module Translator
|
@@ -300,6 +309,7 @@ module ActionDispatch
|
|
300
309
|
file_path = %w(config locales routes.yml) if file_path.blank?
|
301
310
|
r = RailsTranslateRoutes.init_from_file(File.join(Rails.root, file_path))
|
302
311
|
r.prefix_on_default_locale = true if options && options[:prefix_on_default_locale] == true
|
312
|
+
r.no_prefixes = true if options && options[:no_prefixes] == true
|
303
313
|
r.translate Rails.application.routes
|
304
314
|
end
|
305
315
|
|
@@ -329,4 +339,4 @@ RailsTranslateRoutes::ROUTE_HELPER_CONTAINER.each do |klass|
|
|
329
339
|
RailsTranslateRoutes.locale_suffix locale
|
330
340
|
end
|
331
341
|
end
|
332
|
-
end
|
342
|
+
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "rails-translate-routes"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Francesc Pla"]
|
12
|
-
s.date = "2012-01-
|
12
|
+
s.date = "2012-01-19"
|
13
13
|
s.description = "Simple gem to translate routes in Rails 3.x based on translate_routes"
|
14
14
|
s.email = "francesc@francesc.net"
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-translate-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70239182665940 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70239182665940
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70239182665160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.0.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70239182665160
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70239182664360 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.6.4
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70239182664360
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sqlite3
|
49
|
-
requirement: &
|
49
|
+
requirement: &70239182663440 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70239182663440
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: minitest
|
60
|
-
requirement: &
|
60
|
+
requirement: &70239182662480 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70239182662480
|
69
69
|
description: Simple gem to translate routes in Rails 3.x based on translate_routes
|
70
70
|
email: francesc@francesc.net
|
71
71
|
executables: []
|
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash:
|
103
|
+
hash: 2656952746989576753
|
104
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|