rails-translate-routes 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +266 -0
- data/VERSION +1 -1
- data/lib/rails-translate-routes.rb +16 -9
- data/rails-translate-routes.gemspec +5 -5
- metadata +41 -16
- data/README.rdoc +0 -244
data/README.md
ADDED
@@ -0,0 +1,266 @@
|
|
1
|
+
# rails-translate-routes
|
2
|
+
|
3
|
+
**Important change from version 0.0.5 (Feb 2012) to 0.1.0 (June 2012)**: if you're updating from an earlier version take into account that now translations defined in routes.yml are namespaced to avoid conflicts with other translations from app (thanks to cawel for the patch). To upgrade you just have to add the namespace `routes` to your `routes.yml (see example in the below docs).
|
4
|
+
|
5
|
+
Rails >=3.1 routes translations based on Raul's translate_routes (https://github.com/raul/translate_routes).
|
6
|
+
|
7
|
+
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.
|
8
|
+
|
9
|
+
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!
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add it to your Gemfile:
|
14
|
+
|
15
|
+
gem 'rails-translate-routes'
|
16
|
+
|
17
|
+
## Basic usage
|
18
|
+
|
19
|
+
Let's imagine you have a SampleApp with products and a contact page. A typical `routes.rb` file should look like:
|
20
|
+
|
21
|
+
SampleApp::Application.routes.draw do
|
22
|
+
resources :products
|
23
|
+
match 'contact', :to => 'pages#contact'
|
24
|
+
end
|
25
|
+
|
26
|
+
Running `rake routes we have:
|
27
|
+
|
28
|
+
products GET /products(.:format) {:action=>"index", :controller=>"products"}
|
29
|
+
POST /products(.:format) {:action=>"create", :controller=>"products"}
|
30
|
+
new_product GET /products/new(.:format) {:action=>"new", :controller=>"products"}
|
31
|
+
edit_product GET /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
32
|
+
product GET /products/:id(.:format) {:action=>"show", :controller=>"products"}
|
33
|
+
PUT /products/:id(.:format) {:action=>"update", :controller=>"products"}
|
34
|
+
DELETE /products/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
35
|
+
contact /contact(.:format) {:action=>"contact", :controller=>"pages"}
|
36
|
+
|
37
|
+
We want to have them in two languages english and spanish, to accomplish this with rails-translate-routes:
|
38
|
+
|
39
|
+
1) We have to activate the translations appending this line to the end of `routes.rb
|
40
|
+
|
41
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml')
|
42
|
+
|
43
|
+
2) Now we can write translations on a standard YAML file (e.g: in `config/locales/routes.yml`), including all the locales and their translations:
|
44
|
+
|
45
|
+
en:
|
46
|
+
routes:
|
47
|
+
# you can leave empty locales, for example the default one
|
48
|
+
es:
|
49
|
+
routes:
|
50
|
+
products: productos
|
51
|
+
contact: contacto
|
52
|
+
new: crear
|
53
|
+
|
54
|
+
3) Include this filter in your `ApplicationController:
|
55
|
+
|
56
|
+
before_filter :set_locale_from_url
|
57
|
+
|
58
|
+
Also remember to include language detection to your app, a simple example of an `ApplicationController
|
59
|
+
|
60
|
+
class ApplicationController < ActionController::Base
|
61
|
+
protect_from_forgery
|
62
|
+
|
63
|
+
before_filter :set_locale
|
64
|
+
before_filter :set_locale_from_url
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def set_locale
|
69
|
+
I18n.locale = params[:locale] || ((lang = request.env['HTTP_ACCEPT_LANGUAGE']) && lang[/^[a-z]{2}/])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
And that's it! Now if we execute `rake routes`
|
74
|
+
|
75
|
+
products_en GET /products(.:format) {:action=>"index", :controller=>"products"}
|
76
|
+
products_es GET /es/productos(.:format) {:action=>"index", :controller=>"products"}
|
77
|
+
POST /products(.:format) {:action=>"create", :controller=>"products"}
|
78
|
+
POST /es/productos(.:format) {:action=>"create", :controller=>"products"}
|
79
|
+
new_product_en GET /products/new(.:format) {:action=>"new", :controller=>"products"}
|
80
|
+
new_product_es GET /es/productos/new(.:format) {:action=>"new", :controller=>"products"}
|
81
|
+
edit_product_en GET /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
82
|
+
edit_product_es GET /es/productos/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
83
|
+
product_en GET /products/:id(.:format) {:action=>"show", :controller=>"products"}
|
84
|
+
product_es GET /es/productos/:id(.:format) {:action=>"show", :controller=>"products"}
|
85
|
+
PUT /products/:id(.:format) {:action=>"update", :controller=>"products"}
|
86
|
+
PUT /es/productos/:id(.:format) {:action=>"update", :controller=>"products"}
|
87
|
+
DELETE /products/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
88
|
+
DELETE /es/productos/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
89
|
+
contact_en /contact(.:format) {:action=>"contact", :controller=>"pages"}
|
90
|
+
contact_es /es/contacto(.:format) {:action=>"contact", :controller=>"pages"}
|
91
|
+
root_en / {:controller=>"public", :action=>"index"}
|
92
|
+
root_es /es {:controller=>"public", :action=>"index"}
|
93
|
+
|
94
|
+
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.
|
95
|
+
|
96
|
+
## URL structure options
|
97
|
+
|
98
|
+
### Default URL structure
|
99
|
+
|
100
|
+
By default it generates the following url structure:
|
101
|
+
|
102
|
+
/
|
103
|
+
/es
|
104
|
+
/products/
|
105
|
+
/es/productos/
|
106
|
+
/contact/
|
107
|
+
/es/contacto/
|
108
|
+
|
109
|
+
|
110
|
+
### All languages prefixed
|
111
|
+
|
112
|
+
In case you want the default languages to be scoped resulting in the following structure:
|
113
|
+
|
114
|
+
/en
|
115
|
+
/es
|
116
|
+
/en/products/
|
117
|
+
/es/productos/
|
118
|
+
/en/contact/
|
119
|
+
/es/contacto/
|
120
|
+
|
121
|
+
You can specify the following option:
|
122
|
+
|
123
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :prefix_on_default_locale => true })
|
124
|
+
|
125
|
+
If you use the `prefix_on_default_locale` you will have to make the proper redirect on your root controller from http://www.sampleapp.com/ to http://www.sampleapp.com/en or http://www.sampleapp.com/es rails-translate-routes adds an extra unstranslated root path:
|
126
|
+
|
127
|
+
root_en /en {:controller=>"pages", :action=>"index"}
|
128
|
+
root_es /es {:controller=>"pages", :action=>"index"}
|
129
|
+
root / {:controller=>"pages", :action=>"index"}
|
130
|
+
|
131
|
+
A simple example of a redirection to user locale in index method:
|
132
|
+
|
133
|
+
def index
|
134
|
+
unless params[ :locale]
|
135
|
+
# it takes I18n.locale from the previous example set_locale as before_filter in application controller
|
136
|
+
redirect_to eval("root_#{I18n.locale}_path")
|
137
|
+
end
|
138
|
+
|
139
|
+
# rest of the controller logic ...
|
140
|
+
end
|
141
|
+
|
142
|
+
### No prefixed languages
|
143
|
+
|
144
|
+
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:
|
145
|
+
|
146
|
+
/
|
147
|
+
/products/
|
148
|
+
/productos/
|
149
|
+
/contact/
|
150
|
+
/contacto/
|
151
|
+
|
152
|
+
You can specify the following option:
|
153
|
+
|
154
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :no_prefixes => true })
|
155
|
+
|
156
|
+
Note that the `no_prefixes` option will override the `prefix_on_default_locale` option.
|
157
|
+
|
158
|
+
### Keep untranslated routes
|
159
|
+
|
160
|
+
In case you want to keep access to untranslated routes, for easier api or ajax integration for example. Resulting in this structure:
|
161
|
+
|
162
|
+
/en/available-products
|
163
|
+
/fr/produits-disponibles/
|
164
|
+
/products/
|
165
|
+
|
166
|
+
You can specify the following option:
|
167
|
+
|
168
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :keep_untranslated_routes => true })
|
169
|
+
|
170
|
+
This option is not meant to be used with `no_prefixes`.
|
171
|
+
|
172
|
+
### Namespaced backends
|
173
|
+
|
174
|
+
I usually build app backend in namespaced controllers, routes, ... using translated routes will result in duplicated routes or prefixed ones. In most cases you won't want to have the backend in several languages, you can set `routes.rb` this way:
|
175
|
+
|
176
|
+
SampleApp::Application.routes.draw do
|
177
|
+
resources :products
|
178
|
+
match 'contact', :to => 'pages#contact'
|
179
|
+
end
|
180
|
+
|
181
|
+
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :prefix_on_default_locale => true })
|
182
|
+
|
183
|
+
SampleApp::Application.routes.draw do
|
184
|
+
namespace :admin do
|
185
|
+
resources :products
|
186
|
+
root :to => "admin_products#index"
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
## Translation YAML options
|
191
|
+
|
192
|
+
By default translations of routes are specified on a single YAML file (e.g: in `config/locales/routes.yml`), but in some cases you might want to split the routes translations on several files. For example having all application translations files a in folder per language:
|
193
|
+
|
194
|
+
# config/locales/en
|
195
|
+
rails_defaults.yml
|
196
|
+
application.yml
|
197
|
+
routes.yml
|
198
|
+
|
199
|
+
# config/locales/es
|
200
|
+
rails_defaults.yml
|
201
|
+
application.yml
|
202
|
+
routes.yml
|
203
|
+
|
204
|
+
To pass several YAML files to rails-translate-routes you can pass an array of paths:
|
205
|
+
|
206
|
+
ActionDispatch::Routing::Translator.translate_from_file(I18n.available_locales.map { |locale| "config/locales/#{locale}/routes.yml" }, { :prefix_on_default_locale => true })
|
207
|
+
|
208
|
+
## Testing
|
209
|
+
|
210
|
+
Once your app is locale-aware, the routes are dependent on the locale. This means that in functional tests, you need to explicitly include the locale like so:
|
211
|
+
|
212
|
+
get :show, :id => 1, :locale => 'en'
|
213
|
+
|
214
|
+
But in case you would prefer the locale to be implicit or simply because you don't want to add the locale param to all your previous functional tests, you can require the `lib/controller_test_helper` file (typically from your test helper file) and then this will work fine:
|
215
|
+
|
216
|
+
get :show, :id => 1
|
217
|
+
|
218
|
+
## TODO
|
219
|
+
|
220
|
+
Help is welcome ;)
|
221
|
+
|
222
|
+
* make basic testing
|
223
|
+
|
224
|
+
## Credits
|
225
|
+
|
226
|
+
Thanks to:
|
227
|
+
|
228
|
+
Contributors of the current gem:
|
229
|
+
* Martin Carel (https://github.com/cawel)
|
230
|
+
* Johan Gyllenspetz (https://github.com/gyllen)
|
231
|
+
* Nico Ritsche (https://github.com/ncri)
|
232
|
+
* Jean-Loup Fenaux (https://github.com/jlfenaux)
|
233
|
+
* Cyril Mouge (https://github.com/shingara)
|
234
|
+
* Nicolas Arbogast (https://github.com/NicoArbogast)
|
235
|
+
* Stephan van Eijkelenburg (https://github.com/stephanvane)
|
236
|
+
* Víctor Martín (https://github.com/eltercero)
|
237
|
+
|
238
|
+
Main development of forked gem:
|
239
|
+
* Raul Murciano (http://github.com/raul)
|
240
|
+
|
241
|
+
Contributors of forked gem:
|
242
|
+
* Aitor Garay-Romero (http://github.com/aitorgr)
|
243
|
+
* Christian Hølmer (http://github.com/hoelmer)
|
244
|
+
* Nico Ritsche (https://github.com/ncri)
|
245
|
+
* Cedric Darricau (http://github.com/devsigner)
|
246
|
+
* Olivier Gonzalez (http://github.com/gonzoyumo)
|
247
|
+
* Kristian Mandrup (http://github.com/kristianmandrup)
|
248
|
+
* Pieter Visser (http://github.com/pietervisser)
|
249
|
+
* Marian Theisen (http://github.com/cice)
|
250
|
+
* Enric Lluelles (http://github.com/enriclluelles)
|
251
|
+
* Jaime Iniesta (http://github.com/jaimeiniesta)
|
252
|
+
|
253
|
+
## Similar projects
|
254
|
+
|
255
|
+
Another fork of translate_routes, a much cleaner approach with better testing but less flexible than rails-translate-routes:
|
256
|
+
|
257
|
+
* route_translator (https://github.com/enriclluelles/route_translator)
|
258
|
+
|
259
|
+
Also 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 old Rails versions or have different needs.
|
260
|
+
|
261
|
+
* translate_routes (https://github.com/raul/translate_routes)
|
262
|
+
* i18n_routing (https://github.com/kwi/i18n_routing)
|
263
|
+
|
264
|
+
## Other i18n related projects
|
265
|
+
|
266
|
+
If you also need to translate models check out: https://github.com/francesc/rails-translate-models
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
@@ -52,8 +52,8 @@ class RailsTranslateRoutes
|
|
52
52
|
@no_prefixes = no_prefixes
|
53
53
|
end
|
54
54
|
|
55
|
-
# option allowing to keep untranslated routes
|
56
|
-
# *Ex:
|
55
|
+
# option allowing to keep untranslated routes
|
56
|
+
# *Ex:
|
57
57
|
# *resources :users
|
58
58
|
# *translated routes
|
59
59
|
# en/members
|
@@ -66,7 +66,7 @@ class RailsTranslateRoutes
|
|
66
66
|
def keep_untranslated_routes= keep_untranslated_routes
|
67
67
|
@keep_untranslated_routes = keep_untranslated_routes
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
class << self
|
71
71
|
# Default locale suffix generator
|
72
72
|
def locale_suffix locale
|
@@ -128,12 +128,19 @@ class RailsTranslateRoutes
|
|
128
128
|
add_dictionary_from_file file_path
|
129
129
|
end
|
130
130
|
|
131
|
-
# Add translations from another file to the dictionary
|
131
|
+
# Add translations from another file to the dictionary (supports a single file or an array to have translations splited in several folders)
|
132
132
|
def add_dictionary_from_file file_path
|
133
|
-
|
133
|
+
file_path.class == Array ? files = file_path : files = [ file_path ]
|
134
|
+
|
135
|
+
yaml = Hash.new
|
136
|
+
files.each do |file|
|
137
|
+
yaml.merge! YAML.load_file(File.join(Rails.root, file))
|
138
|
+
end
|
139
|
+
|
134
140
|
yaml.each_pair do |locale, translations|
|
135
141
|
merge_translations locale, translations['routes']
|
136
142
|
end
|
143
|
+
|
137
144
|
set_available_locales_from_dictionary
|
138
145
|
end
|
139
146
|
|
@@ -263,7 +270,7 @@ class RailsTranslateRoutes
|
|
263
270
|
available_locales.map do |locale|
|
264
271
|
translated_routes << translate_route(route, locale)
|
265
272
|
end
|
266
|
-
|
273
|
+
|
267
274
|
# add untranslated_route without url helper if we want to keep untranslated routes
|
268
275
|
translated_routes << untranslated_route(route) if @keep_untranslated_routes
|
269
276
|
translated_routes
|
@@ -294,7 +301,7 @@ class RailsTranslateRoutes
|
|
294
301
|
|
295
302
|
# Re-generate untranslated routes (original routes) with name set to nil (which prevents conflict with default untranslated_urls)
|
296
303
|
def untranslated_route route
|
297
|
-
conditions = {}
|
304
|
+
conditions = {}
|
298
305
|
if Rails.version >= '3.2'
|
299
306
|
conditions[:path_info] = route.path.spec.to_s
|
300
307
|
conditions[:request_method] = parse_request_methods route.verb if route.verb != //
|
@@ -308,7 +315,7 @@ class RailsTranslateRoutes
|
|
308
315
|
|
309
316
|
[route.app, conditions, requirements, defaults]
|
310
317
|
end
|
311
|
-
|
318
|
+
|
312
319
|
# Add prefix for all non-default locales
|
313
320
|
def add_prefix? locale
|
314
321
|
if @no_prefixes
|
@@ -383,7 +390,7 @@ module ActionDispatch
|
|
383
390
|
|
384
391
|
def translate_from_file(file_path, options = {})
|
385
392
|
file_path = %w(config locales routes.yml) if file_path.blank?
|
386
|
-
r = RailsTranslateRoutes.init_from_file(
|
393
|
+
r = RailsTranslateRoutes.init_from_file(file_path)
|
387
394
|
r.prefix_on_default_locale = true if options && options[:prefix_on_default_locale] == true
|
388
395
|
r.no_prefixes = true if options && options[:no_prefixes] == true
|
389
396
|
r.keep_untranslated_routes = true if options && options[:keep_untranslated_routes] == true
|
@@ -5,23 +5,23 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "rails-translate-routes"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.3"
|
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-
|
12
|
+
s.date = "2012-08-03"
|
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 = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
"Gemfile",
|
22
22
|
"Gemfile.lock",
|
23
23
|
"LICENSE.txt",
|
24
|
-
"README.
|
24
|
+
"README.md",
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"lib/controller_test_helper.rb",
|
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
s.homepage = "http://github.com/francesc/rails-translate-routes"
|
34
34
|
s.licenses = ["MIT"]
|
35
35
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = "1.8.
|
36
|
+
s.rubygems_version = "1.8.24"
|
37
37
|
s.summary = "Simple gem to translate routes in Rails 3.x"
|
38
38
|
|
39
39
|
if s.respond_to? :specification_version then
|
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.1.
|
4
|
+
version: 0.1.3
|
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-
|
12
|
+
date: 2012-08-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: bundler
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ~>
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: 1.0.0
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: jeweler
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ~>
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: 1.6.4
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.6.4
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: sqlite3
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,10 +69,15 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: minitest
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
83
|
- - ! '>='
|
@@ -65,20 +85,25 @@ dependencies:
|
|
65
85
|
version: '0'
|
66
86
|
type: :development
|
67
87
|
prerelease: false
|
68
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
69
94
|
description: Simple gem to translate routes in Rails 3.x based on translate_routes
|
70
95
|
email: francesc@francesc.net
|
71
96
|
executables: []
|
72
97
|
extensions: []
|
73
98
|
extra_rdoc_files:
|
74
99
|
- LICENSE.txt
|
75
|
-
- README.
|
100
|
+
- README.md
|
76
101
|
files:
|
77
102
|
- .document
|
78
103
|
- Gemfile
|
79
104
|
- Gemfile.lock
|
80
105
|
- LICENSE.txt
|
81
|
-
- README.
|
106
|
+
- README.md
|
82
107
|
- Rakefile
|
83
108
|
- VERSION
|
84
109
|
- lib/controller_test_helper.rb
|
@@ -101,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
126
|
version: '0'
|
102
127
|
segments:
|
103
128
|
- 0
|
104
|
-
hash:
|
129
|
+
hash: -3922825596056835804
|
105
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
131
|
none: false
|
107
132
|
requirements:
|
@@ -110,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
135
|
version: '0'
|
111
136
|
requirements: []
|
112
137
|
rubyforge_project:
|
113
|
-
rubygems_version: 1.8.
|
138
|
+
rubygems_version: 1.8.24
|
114
139
|
signing_key:
|
115
140
|
specification_version: 3
|
116
141
|
summary: Simple gem to translate routes in Rails 3.x
|
data/README.rdoc
DELETED
@@ -1,244 +0,0 @@
|
|
1
|
-
= rails-translate-routes
|
2
|
-
|
3
|
-
Important change from version 0.0.5 (Feb 2012) to 0.1.0 (June 2012): if you're updating from an earlier version take into account that now translations defined in routes.yml are namespaced to avoid conflicts with other translations from app (thanks to cawel for the patch). To upgrade you just have to add the namespace 'routes' to your routes.yml (see example in the below docs).
|
4
|
-
|
5
|
-
Rails >=3.1 routes translations based on Raul's translate_routes (https://github.com/raul/translate_routes).
|
6
|
-
|
7
|
-
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.
|
8
|
-
|
9
|
-
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!
|
10
|
-
|
11
|
-
== Installation
|
12
|
-
|
13
|
-
Add it to your Gemfile:
|
14
|
-
|
15
|
-
gem 'rails-translate-routes'
|
16
|
-
|
17
|
-
== Basic usage
|
18
|
-
|
19
|
-
Let's imagine you have a SampleApp with products and a contact page. A typical 'routes.rb' file should look like:
|
20
|
-
|
21
|
-
SampleApp::Application.routes.draw do
|
22
|
-
resources :products
|
23
|
-
match 'contact', :to => 'pages#contact'
|
24
|
-
end
|
25
|
-
|
26
|
-
Running 'rake routes' we have:
|
27
|
-
|
28
|
-
products GET /products(.:format) {:action=>"index", :controller=>"products"}
|
29
|
-
POST /products(.:format) {:action=>"create", :controller=>"products"}
|
30
|
-
new_product GET /products/new(.:format) {:action=>"new", :controller=>"products"}
|
31
|
-
edit_product GET /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
32
|
-
product GET /products/:id(.:format) {:action=>"show", :controller=>"products"}
|
33
|
-
PUT /products/:id(.:format) {:action=>"update", :controller=>"products"}
|
34
|
-
DELETE /products/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
35
|
-
contact /contact(.:format) {:action=>"contact", :controller=>"pages"}
|
36
|
-
|
37
|
-
We want to have them in two languages english and spanish, to accomplish this with rails-translate-routes:
|
38
|
-
|
39
|
-
1) We have to activate the translations appending this line to the end of 'routes.rb'
|
40
|
-
|
41
|
-
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml')
|
42
|
-
|
43
|
-
2) Now we can write translations on a standard YAML file (e.g: in config/locales/routes.yml), including all the locales and their translations:
|
44
|
-
|
45
|
-
en:
|
46
|
-
routes:
|
47
|
-
# you can leave empty locales, for example the default one
|
48
|
-
es:
|
49
|
-
routes:
|
50
|
-
products: productos
|
51
|
-
contact: contacto
|
52
|
-
new: crear
|
53
|
-
|
54
|
-
3) Include this filter in your ApplicationController:
|
55
|
-
|
56
|
-
before_filter :set_locale_from_url
|
57
|
-
|
58
|
-
Also remember to include language detection to your app, a simple example of an ApplicationController
|
59
|
-
|
60
|
-
class ApplicationController < ActionController::Base
|
61
|
-
protect_from_forgery
|
62
|
-
|
63
|
-
before_filter :set_locale
|
64
|
-
before_filter :set_locale_from_url
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def set_locale
|
69
|
-
I18n.locale = params[:locale] || ((lang = request.env['HTTP_ACCEPT_LANGUAGE']) && lang[/^[a-z]{2}/])
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
And that's it! Now if we execute 'rake routes':
|
74
|
-
|
75
|
-
products_en GET /products(.:format) {:action=>"index", :controller=>"products"}
|
76
|
-
products_es GET /es/productos(.:format) {:action=>"index", :controller=>"products"}
|
77
|
-
POST /products(.:format) {:action=>"create", :controller=>"products"}
|
78
|
-
POST /es/productos(.:format) {:action=>"create", :controller=>"products"}
|
79
|
-
new_product_en GET /products/new(.:format) {:action=>"new", :controller=>"products"}
|
80
|
-
new_product_es GET /es/productos/new(.:format) {:action=>"new", :controller=>"products"}
|
81
|
-
edit_product_en GET /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
82
|
-
edit_product_es GET /es/productos/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
|
83
|
-
product_en GET /products/:id(.:format) {:action=>"show", :controller=>"products"}
|
84
|
-
product_es GET /es/productos/:id(.:format) {:action=>"show", :controller=>"products"}
|
85
|
-
PUT /products/:id(.:format) {:action=>"update", :controller=>"products"}
|
86
|
-
PUT /es/productos/:id(.:format) {:action=>"update", :controller=>"products"}
|
87
|
-
DELETE /products/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
88
|
-
DELETE /es/productos/:id(.:format) {:action=>"destroy", :controller=>"products"}
|
89
|
-
contact_en /contact(.:format) {:action=>"contact", :controller=>"pages"}
|
90
|
-
contact_es /es/contacto(.:format) {:action=>"contact", :controller=>"pages"}
|
91
|
-
root_en / {:controller=>"public", :action=>"index"}
|
92
|
-
root_es /es {:controller=>"public", :action=>"index"}
|
93
|
-
|
94
|
-
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.
|
95
|
-
|
96
|
-
== URL structure options
|
97
|
-
|
98
|
-
<b>Default URL structure</b>
|
99
|
-
|
100
|
-
By default it generates the following url structure:
|
101
|
-
|
102
|
-
/
|
103
|
-
/es
|
104
|
-
/products/
|
105
|
-
/es/productos/
|
106
|
-
/contact/
|
107
|
-
/es/contacto/
|
108
|
-
|
109
|
-
|
110
|
-
<b>All languages prefixed</b>
|
111
|
-
|
112
|
-
In case you want the default languages to be scoped resulting in the following structure:
|
113
|
-
|
114
|
-
/en
|
115
|
-
/es
|
116
|
-
/en/products/
|
117
|
-
/es/productos/
|
118
|
-
/en/contact/
|
119
|
-
/es/contacto/
|
120
|
-
|
121
|
-
You can specify the following option:
|
122
|
-
|
123
|
-
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :prefix_on_default_locale => true })
|
124
|
-
|
125
|
-
If you use the 'prefix_on_default_locale' you will have to make the proper redirect on your root controller from http://www.sampleapp.com/ to http://www.sampleapp.com/en or http://www.sampleapp.com/es rails-translate-routes adds an extra unstranslated root path:
|
126
|
-
|
127
|
-
root_en /en {:controller=>"pages", :action=>"index"}
|
128
|
-
root_es /es {:controller=>"pages", :action=>"index"}
|
129
|
-
root / {:controller=>"pages", :action=>"index"}
|
130
|
-
|
131
|
-
A simple example of a redirection to user locale in index method:
|
132
|
-
|
133
|
-
def index
|
134
|
-
unless params[ :locale]
|
135
|
-
# it takes I18n.locale from the previous example set_locale as before_filter in application controller
|
136
|
-
redirect_to eval("root_#{I18n.locale}_path")
|
137
|
-
end
|
138
|
-
|
139
|
-
# rest of the controller logic ...
|
140
|
-
end
|
141
|
-
|
142
|
-
<b>No prefixed languages</b>
|
143
|
-
|
144
|
-
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:
|
145
|
-
|
146
|
-
/
|
147
|
-
/products/
|
148
|
-
/productos/
|
149
|
-
/contact/
|
150
|
-
/contacto/
|
151
|
-
|
152
|
-
You can specify the following option:
|
153
|
-
|
154
|
-
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :no_prefixes => true })
|
155
|
-
|
156
|
-
Note that the 'no_prefixes' option will override the 'prefix_on_default_locale' option.
|
157
|
-
|
158
|
-
<b>Keep untranslated routes</b>
|
159
|
-
|
160
|
-
In case you want to keep access to untranslated routes, for easier api or ajax integration for example. Resulting in this structure:
|
161
|
-
|
162
|
-
/en/available-products
|
163
|
-
/fr/produits-disponibles/
|
164
|
-
/products/
|
165
|
-
|
166
|
-
You can specify the following option:
|
167
|
-
|
168
|
-
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :keep_untranslated_routes => true })
|
169
|
-
|
170
|
-
This option is not meant to be used with :no_prefixes.
|
171
|
-
|
172
|
-
<b>Namespaced backends</b>
|
173
|
-
|
174
|
-
I usually build app backend in namespaced controllers, routes, ... using translated routes will result in duplicated routes or prefixed ones. In most cases you won't want to have the backend in several languages, you can set 'routes.rb' this way:
|
175
|
-
|
176
|
-
SampleApp::Application.routes.draw do
|
177
|
-
resources :products
|
178
|
-
match 'contact', :to => 'pages#contact'
|
179
|
-
end
|
180
|
-
|
181
|
-
ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :prefix_on_default_locale => true })
|
182
|
-
|
183
|
-
SampleApp::Application.routes.draw do
|
184
|
-
namespace :admin do
|
185
|
-
resources :products
|
186
|
-
root :to => "admin_products#index"
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
== Testing
|
191
|
-
|
192
|
-
Once your app is locale-aware, the routes are dependent on the locale. This means that in functional tests, you need to explicitly include the locale like so:
|
193
|
-
|
194
|
-
get :show, :id => 1, :locale => 'en'
|
195
|
-
|
196
|
-
But in case you would prefer the locale to be implicit or simply because you don't want to add the locale param to all your previous functional tests, you can require the 'lib/controller_test_helper' file (typically from your test helper file) and then this will work fine:
|
197
|
-
|
198
|
-
get :show, :id => 1
|
199
|
-
|
200
|
-
== TODO
|
201
|
-
|
202
|
-
Help is welcome ;)
|
203
|
-
|
204
|
-
* make basic testing
|
205
|
-
|
206
|
-
== Credits
|
207
|
-
|
208
|
-
Thanks to:
|
209
|
-
|
210
|
-
* Contributors of the current gem:
|
211
|
-
* Martin Carel (https://github.com/cawel)
|
212
|
-
* Johan Gyllenspetz (https://github.com/gyllen)
|
213
|
-
* Nico Ritsche (https://github.com/ncri)
|
214
|
-
* Jean-Loup Fenaux (https://github.com/jlfenaux)
|
215
|
-
* Cyril Mouge (https://github.com/shingara)
|
216
|
-
* Nicolas Arbogast (https://github.com/NicoArbogast)
|
217
|
-
* Stephan van Eijkelenburg (https://github.com/stephanvane)
|
218
|
-
* Víctor Martín (https://github.com/eltercero)
|
219
|
-
|
220
|
-
* Main development of forked gem:
|
221
|
-
* Raul Murciano (http://github.com/raul)
|
222
|
-
|
223
|
-
* Contributors of forked gem:
|
224
|
-
* Aitor Garay-Romero (http://github.com/aitorgr)
|
225
|
-
* Christian Hølmer (http://github.com/hoelmer)
|
226
|
-
* Nico Ritsche (https://github.com/ncri)
|
227
|
-
* Cedric Darricau (http://github.com/devsigner)
|
228
|
-
* Olivier Gonzalez (http://github.com/gonzoyumo)
|
229
|
-
* Kristian Mandrup (http://github.com/kristianmandrup)
|
230
|
-
* Pieter Visser (http://github.com/pietervisser)
|
231
|
-
* Marian Theisen (http://github.com/cice)
|
232
|
-
* Enric Lluelles (http://github.com/enriclluelles)
|
233
|
-
* Jaime Iniesta (http://github.com/jaimeiniesta)
|
234
|
-
|
235
|
-
== Similar projects
|
236
|
-
|
237
|
-
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.
|
238
|
-
|
239
|
-
* translate_routes (https://github.com/raul/translate_routes)
|
240
|
-
* i18n_routing (https://github.com/kwi/i18n_routing)
|
241
|
-
|
242
|
-
== Other i18n related projects
|
243
|
-
|
244
|
-
If you also need to translate models check out: https://github.com/francesc/rails-translate-models
|