route_localize 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +43 -24
- data/lib/route_localize/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3213ab072255f6330c2f26ee072ade47f632bcff
|
4
|
+
data.tar.gz: bc248c471857e5b907d1f0dc17406911306cf8af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ef5b752784039c71ec50e115a04c325ed5783e7ee9816c993f794c13e209c5d7baf54a2c0fdc8f33048539140fc8e5ad7b7597eb30f044b7a36201ecc465606
|
7
|
+
data.tar.gz: f4849ca23b52085b7c28d7d96b84289daa11a8135918ca8a4c29379dea68872d07bbb9ab98d80de71964d119d65d8484443a7cc22180465426bca1e392c92de4
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
# Route Localize
|
2
2
|
|
3
|
-
Rails
|
3
|
+
Rails engine to translate routes using locale files and subdomains.
|
4
4
|
|
5
|
+
**Only works with Rails 4.0**. Check out
|
6
|
+
[route_translator](https://github.com/enriclluelles/route_translator/) for
|
7
|
+
Rails 4.2 and Rails 5 compatibility.
|
5
8
|
|
6
9
|
## Install
|
7
10
|
|
8
11
|
In your Rails application's `Gemfile` add:
|
9
12
|
|
10
13
|
```rb
|
11
|
-
gem "route_localize"
|
14
|
+
gem "route_localize"
|
12
15
|
```
|
13
16
|
|
14
|
-
Install the
|
17
|
+
Install the gem by running:
|
15
18
|
|
16
19
|
```sh
|
17
20
|
$ bundle
|
@@ -19,15 +22,18 @@ $ bundle
|
|
19
22
|
|
20
23
|
## Scopes
|
21
24
|
|
22
|
-
Route Localize adds two
|
25
|
+
Route Localize adds two scopes you can use in your routes:
|
23
26
|
|
24
|
-
- `localize`: your locale is the first
|
25
|
-
|
27
|
+
- `localize`: if your locale is the first parameter in the path.
|
28
|
+
For example `http://example.com/en/foo`.
|
29
|
+
- `localize_subdomain`: if your locale is your subdomain.
|
30
|
+
For example `http://en.example.com/foo`.
|
26
31
|
|
27
32
|
|
28
33
|
## Usage
|
29
34
|
|
30
|
-
In your `config/routes.rb`,
|
35
|
+
In your `config/routes.rb`, add one of the scopes around your routes.
|
36
|
+
For example:
|
31
37
|
|
32
38
|
```rb
|
33
39
|
scope localize: [:en, :fr] do
|
@@ -36,7 +42,8 @@ end
|
|
36
42
|
root 'pages#index'
|
37
43
|
```
|
38
44
|
|
39
|
-
|
45
|
+
Then, create a `config/locales/routes.yml` with translations for each part
|
46
|
+
of your routes under the `routes` key. For example:
|
40
47
|
|
41
48
|
```yml
|
42
49
|
fr:
|
@@ -66,31 +73,32 @@ module ApplicationHelper
|
|
66
73
|
end
|
67
74
|
```
|
68
75
|
|
69
|
-
You can then use the `locale_switch_url` or `locale_switch_subdomain_url`
|
76
|
+
You can then use the `locale_switch_url` or `locale_switch_subdomain_url`
|
77
|
+
helpers in your views like so:
|
70
78
|
|
71
79
|
```erb
|
72
|
-
<%= link_to "
|
73
|
-
<%= link_to "
|
80
|
+
<%= link_to "Version française", locale_switch_url("fr") %>
|
81
|
+
<%= link_to "English version", locale_switch_url("en") %>
|
74
82
|
```
|
75
83
|
|
76
84
|
|
77
85
|
### Change the parameters in your switcher
|
78
86
|
|
79
|
-
If your params are different depending on the language, you can override
|
87
|
+
If some of your params are different depending on the language, you can override
|
80
88
|
the switcher's params by creating a `route_localize_options` method that
|
81
89
|
takes the locale as a parameter.
|
82
90
|
|
83
91
|
For example if you would like to switch from
|
84
92
|
`http://en.example.org/products/keyboard`
|
85
93
|
to `http://fr.example.org/produits/clavier`, where `keyboard` and `clavier`
|
86
|
-
are the `:id` parameter.
|
94
|
+
are the `:id` parameter in your routes.
|
87
95
|
|
88
|
-
In this case you might already have this in controller:
|
96
|
+
In this case you might already have something like this in controller:
|
89
97
|
|
90
98
|
```rb
|
91
99
|
class ProductsController < ApplicationController
|
92
100
|
def show
|
93
|
-
if I18n.locale
|
101
|
+
if I18n.locale == :fr
|
94
102
|
@tree = Product.find_by_name_fr(params[:id])
|
95
103
|
else
|
96
104
|
@tree = Product.find_by_name_en(params[:id])
|
@@ -99,20 +107,24 @@ class ProductsController < ApplicationController
|
|
99
107
|
end
|
100
108
|
```
|
101
109
|
|
102
|
-
|
110
|
+
In this case you would need to add the `route_localize_path_options` method
|
111
|
+
that returns a hash of params to change depending on the locale. Here, `:id`
|
112
|
+
needs to be different because we are looking at two different database fields.
|
103
113
|
|
104
114
|
```
|
105
115
|
helper_method :route_localize_path_options
|
106
116
|
def route_localize_path_options(locale)
|
107
|
-
{
|
117
|
+
{
|
118
|
+
id: (locale == "fr" ? @tree.name_fr : @tree.name_en)
|
119
|
+
}
|
108
120
|
end
|
109
121
|
```
|
110
122
|
|
111
123
|
|
112
|
-
##
|
124
|
+
## Translate a single path
|
113
125
|
|
114
|
-
Rails' `url_for` cannot find the translation url automatically,
|
115
|
-
prefer to use the `_path` and `_url`
|
126
|
+
Because Rails' `url_for` cannot find the translation url automatically,
|
127
|
+
prefer to use the `_path` and `_url` helpers instead.
|
116
128
|
|
117
129
|
If you can't, one way around is to use `RouteLocalize.translate_path`.
|
118
130
|
|
@@ -123,7 +135,8 @@ RouteLocalize.translate_path(url_for(controller: 'trees', action: 'index'),
|
|
123
135
|
I18n.locale)
|
124
136
|
```
|
125
137
|
|
126
|
-
If you are using subdomains you should add `by_subdomain: true` option
|
138
|
+
If you are using subdomains you should add the `by_subdomain: true` option to
|
139
|
+
`translate_path`.
|
127
140
|
|
128
141
|
|
129
142
|
## Development
|
@@ -143,7 +156,13 @@ The following gems could also be a good match for your project:
|
|
143
156
|
|
144
157
|
Route Localize is different from these solutions because it:
|
145
158
|
|
146
|
-
- can add a constraint to the subdomain instead of relying on the locale
|
147
|
-
|
148
|
-
-
|
159
|
+
- can add a constraint to the subdomain instead of relying on the locale
|
160
|
+
beeing in the url (`en/…` `fr/`)
|
161
|
+
- plays well with gems that introduce extra locales, routes you don't want to
|
162
|
+
translate, or reload routes before i18n is loaded (`activeadmin` for example)
|
163
|
+
- includes a language switcher helper that returns the correct url in every
|
164
|
+
other language
|
149
165
|
|
166
|
+
## License
|
167
|
+
|
168
|
+
By Sunny Ripert, Licensed under the MIT.
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: route_localize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sunny Ripert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 4.0.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 4.0.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.4.
|
92
|
+
rubygems_version: 2.4.5
|
93
93
|
signing_key:
|
94
94
|
specification_version: 4
|
95
95
|
summary: Rails 4 engine to translate routes.
|