translate_routes 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +28 -23
- data/lib/route_translator.rb +2 -2
- data/test/translate_routes_test.rb +15 -0
- metadata +6 -6
data/README.markdown
CHANGED
@@ -9,46 +9,51 @@ It works fine with all kind of routing definitions, including RESTful and named
|
|
9
9
|
**Your current code will remain untouched**: your current routing code, helpers and links will be translated transparently - even in your tests.
|
10
10
|
(Un)installing it is a very clean and simple process, so why don't you give it a chance? ;)
|
11
11
|
|
12
|
-
|
12
|
+
Installation
|
13
|
+
------------
|
14
|
+
Add translate_routes to your `Gemfile`:
|
15
|
+
|
16
|
+
gem 'translate_routes'
|
17
|
+
|
18
|
+
And let bundle do the rest:
|
19
|
+
|
20
|
+
bundle install
|
21
|
+
|
22
|
+
Quick Start
|
13
23
|
-----------
|
14
24
|
|
15
|
-
Let's
|
25
|
+
1) Let's imagine you have this `routes.rb` file:
|
16
26
|
|
17
|
-
|
18
|
-
match 'contact'
|
27
|
+
TranslateRoutesApp::Application.routes.draw do
|
28
|
+
match 'contact' => 'contact#index'
|
19
29
|
end
|
20
30
|
|
21
|
-
|
22
|
-
|
23
|
-
2) Write your translations on a standard YAML file (e.g: i18n-routes.yml), including the locales and it translations pairs:
|
31
|
+
You can see the available routes with the `rake routes` task:
|
24
32
|
|
25
|
-
es:
|
26
|
-
contact: contacto
|
27
33
|
|
34
|
+
contact /contact(.:format) {:controller=>"contact", :action=>"index"}
|
28
35
|
|
29
|
-
|
30
|
-
your other I18n translations files, the line will be:
|
36
|
+
2) Now write your translations on a standard YAML file (e.g: in `/config/i18n-routes.yml`), including all the locales and their translations pairs:
|
31
37
|
|
32
|
-
|
38
|
+
en:
|
39
|
+
# you can leave empty locales, for example the default one
|
40
|
+
es:
|
41
|
+
contact: contacto
|
33
42
|
|
34
|
-
|
43
|
+
3) Append this line in your `routes.rb` file to activate the translations specifying the path of the translations file:
|
35
44
|
|
36
45
|
ActionDispatch::Routing::Translator.translate_from_file('config','i18n-routes.yml')
|
37
46
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
contact_es_es_path /es-ES/contacto {:locale=>"es", :controller=>"contact", :action=>"index"}
|
42
|
-
contact_en_us_path /contact {:locale=>"'en'", :controller=>"contact", :action=>"index"}
|
43
|
-
|
47
|
+
4) Execute `rake routes` to see the new routes:
|
44
48
|
|
45
|
-
|
49
|
+
contact_es /es/contacto(.:format) {:controller=>"contact", :action=>"index"}
|
50
|
+
contact_en /contact(.:format) {:controller=>"contact", :action=>"index"}
|
46
51
|
|
47
|
-
|
52
|
+
5) Include this filter in your ApplicationController:
|
48
53
|
|
49
54
|
before_filter :set_locale_from_url
|
50
55
|
|
51
|
-
Now your application recognizes the different routes and sets the `I18n.locale` value
|
56
|
+
Now your application recognizes the different routes and sets the `I18n.locale` value in your controllers,
|
52
57
|
but what about the routes generation? As you can see on the previous `rake routes` execution, the
|
53
58
|
`contact_es_es_path` and `contact_en_us_path` routing helpers have been generated and are
|
54
59
|
available in your controllers and views. Additionally, a `contact_path` helper has been generated, which
|
@@ -56,7 +61,7 @@ generates the routes according to the current request's locale. This way your li
|
|
56
61
|
|
57
62
|
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.
|
58
63
|
|
59
|
-
|
64
|
+
6) Hey, but what about my tests?
|
60
65
|
|
61
66
|
Of course, your functional and integration testing involves some requests.
|
62
67
|
The plugin includes some code to add a default locale parameter so they can remain untouched.
|
data/lib/route_translator.rb
CHANGED
@@ -222,7 +222,7 @@ class RouteTranslator
|
|
222
222
|
final_optional_segments = path.match(/(\(.+\))$/)[1] rescue nil # i.e: (.:format)
|
223
223
|
path_segments = path.gsub(final_optional_segments,'').split("/")
|
224
224
|
new_path = path_segments.map{ |seg| translate_path_segment(seg, locale) }.join('/')
|
225
|
-
new_path = "/#{locale}#{new_path}" if add_prefix? locale
|
225
|
+
new_path = "/#{locale.downcase}#{new_path}" if add_prefix? locale
|
226
226
|
new_path = '/' if new_path.blank?
|
227
227
|
final_optional_segments ? new_path + final_optional_segments : new_path
|
228
228
|
end
|
@@ -237,7 +237,7 @@ class RouteTranslator
|
|
237
237
|
|
238
238
|
match = TRANSLATABLE_SEGMENT.match(segment)[1] rescue nil
|
239
239
|
|
240
|
-
translate_string(match, locale) || segment
|
240
|
+
(translate_string(match, locale) || segment).downcase
|
241
241
|
end
|
242
242
|
|
243
243
|
def translate_string str, locale
|
@@ -157,6 +157,21 @@ class TranslateRoutesTest < ActionController::TestCase
|
|
157
157
|
assert_equal '/es(.:format)', path_string(named_route('root_es'))
|
158
158
|
end
|
159
159
|
|
160
|
+
def test_routes_translations_are_always_downcased
|
161
|
+
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
|
162
|
+
config_default_locale_settings 'en'
|
163
|
+
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'Gente'} }
|
164
|
+
translate_routes
|
165
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_routes_locale_prefixes_are_always_downcased
|
169
|
+
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
|
170
|
+
config_default_locale_settings 'en'
|
171
|
+
@route_translator.yield_dictionary { |t| t['en'] = {}; t['ES'] = {'people' => 'Gente'} }
|
172
|
+
translate_routes
|
173
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'ES'
|
174
|
+
end
|
160
175
|
|
161
176
|
def test_languages_load_from_file
|
162
177
|
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: translate_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 3.0.
|
9
|
+
- 3
|
10
|
+
version: 3.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Raul Murciano
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-28 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -38,8 +38,8 @@ homepage: http://github.com/raul/translate_routes
|
|
38
38
|
licenses: []
|
39
39
|
|
40
40
|
post_install_message:
|
41
|
-
rdoc_options:
|
42
|
-
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
43
|
require_paths:
|
44
44
|
- lib
|
45
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|