translate_routes_rails_2_3 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +22 -0
- data/README.markdown +103 -0
- data/lib/translate_routes.rb +219 -0
- data/lib/translate_routes_i18n_available_locales.rb +23 -0
- data/lib/translate_routes_test_helper.rb +33 -0
- data/test/translate_routes_test.rb +323 -0
- metadata +73 -0
data/ChangeLog
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
-- 0.98
|
2
|
+
Accepted patch from hoelmer: Updated rake task to use I18n yaml format.
|
3
|
+
-- 0.97
|
4
|
+
Accepted patch from Aitor Garay-Romero: root routes with prefix now doesn't set the locale parameter.
|
5
|
+
|
6
|
+
-- rails2.2 branch -> master
|
7
|
+
|
8
|
+
-- branch rails2.2 v0.9 (Oct 27th 2008)
|
9
|
+
* Developed after Rails2.2rc1 release, with i18n support. Beta, not backward compatible with Rails < 2.2
|
10
|
+
|
11
|
+
-- 0.96.1 (Aug 5th 2008)
|
12
|
+
* Fixed by Mathieu Fosse: helpers didn't worked as expected when locale_param_key is undefined
|
13
|
+
|
14
|
+
-- 0.96 (Jun 10th 2008)
|
15
|
+
* Added update_yaml task, suggested by Francesc Esplugas
|
16
|
+
|
17
|
+
-- 0.95 (Jan 21st 2008)
|
18
|
+
* Still beta version
|
19
|
+
* Added yaml files support for dictionaries
|
20
|
+
|
21
|
+
-- 0.9 (Dec 27th 2007)
|
22
|
+
* Beta version
|
data/README.markdown
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
TranslateRoutes
|
2
|
+
===============
|
3
|
+
|
4
|
+
This branch works with Rails 2.3.x, you can find branches for Rails [2.1.x](http://github.com/raul/translate_routes/tree/rails2.1) and [2.2.x](http://github.com/raul/translate_routes/tree/rails2.2)
|
5
|
+
|
6
|
+
This Rails plugin provides a simple way to translate your URLs to any number of languages, even on a fully working application.
|
7
|
+
|
8
|
+
It works fine with all kind of routing definitions, including RESTful and named routes.
|
9
|
+
**Your current code will remain untouched**: your current routing code, helpers and links will be translated transparently - even in your tests.
|
10
|
+
(Un)installing it is a very clean and simple process, so why don't you give it a chance? ;)
|
11
|
+
|
12
|
+
This version works only with Rails 2.3.x. You can find all available versions in [the wiki](http://wiki.github.com/raul/translate_routes).
|
13
|
+
|
14
|
+
Sample application
|
15
|
+
------------------
|
16
|
+
There is a [sample application](http://github.com/raul/translate_routes_demo/tree/master) which can be very useful to see how to integrate this plugin on your Rails application. The application itself includes all the required steps: 3 lines, an optional filter and a yaml translations file were used.
|
17
|
+
|
18
|
+
|
19
|
+
Quick start
|
20
|
+
-----------
|
21
|
+
|
22
|
+
Let's start with a tiny example. Of course you need to define your routes first, e.g:
|
23
|
+
|
24
|
+
ActionController::Routing::Routes.draw do |map|
|
25
|
+
map.contact 'contact', :controller => 'contact', :action => 'index'
|
26
|
+
end
|
27
|
+
|
28
|
+
1) Download the plugin to your app's `/vendor/plugins` directory.
|
29
|
+
|
30
|
+
2) Write your translations on a standard YAML file (e.g: i18n-routes.yml), including the locales and it translations pairs:
|
31
|
+
|
32
|
+
es:
|
33
|
+
contact: contacto
|
34
|
+
|
35
|
+
|
36
|
+
3) Append a line to your routes.rb file to activate the translations. If you loaded the translations file with
|
37
|
+
your other I18n translations files, the line will be:
|
38
|
+
|
39
|
+
ActionController::Routing::Translator.i18n('es')
|
40
|
+
|
41
|
+
and if you want to keep the file separated (e.g: config/i18n-routes.yml), the line to append is:
|
42
|
+
|
43
|
+
ActionController::Routing::Translator.translate_from_file('config','i18n-routes.yml')
|
44
|
+
|
45
|
+
You can see it working by executing `rake routes` on the shell:
|
46
|
+
|
47
|
+
|
48
|
+
contact_es_es_path /es-ES/contacto {:locale=>"es", :controller=>"contact", :action=>"index"}
|
49
|
+
contact_en_us_path /contact {:locale=>"'en'", :controller=>"contact", :action=>"index"}
|
50
|
+
|
51
|
+
|
52
|
+
As we can see, a new spanish route has been setted up and a `locale` parameter has been added to the routes.
|
53
|
+
|
54
|
+
4) Include this filter in your ApplicationController:
|
55
|
+
|
56
|
+
before_filter :set_locale_from_url
|
57
|
+
|
58
|
+
Now your application recognizes the different routes and sets the `I18n.locale` value on your controllers,
|
59
|
+
but what about the routes generation? As you can see on the previous `rake routes` execution, the
|
60
|
+
`contact_es_es_path` and `contact_en_us_path` routing helpers have been generated and are
|
61
|
+
available in your controllers and views. Additionally, a `contact_path` helper has been generated, which
|
62
|
+
generates the routes according to the current request's locale. This way your link
|
63
|
+
|
64
|
+
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.
|
65
|
+
|
66
|
+
5) Hey, but what about my tests?
|
67
|
+
|
68
|
+
Of course, your functional and integration testing involves some requests.
|
69
|
+
The plugin includes some code to add a default locale parameter so they can remain untouched.
|
70
|
+
Append it to your `test_helper` and it will be applied.
|
71
|
+
|
72
|
+
Documentation
|
73
|
+
-------------
|
74
|
+
You can find additional information in [the translate_routes' wiki](http://wiki.github.com/raul/translate_routes).
|
75
|
+
|
76
|
+
Questions, suggestions, bug reports...
|
77
|
+
--------------------------------------
|
78
|
+
Feedback, questions and comments will be always welcome at raul@murciano.net
|
79
|
+
|
80
|
+
Credits
|
81
|
+
-------
|
82
|
+
* Main development:
|
83
|
+
* Raul Murciano <http://raul.murciano.net> - code
|
84
|
+
* Domestika INTERNET S.L <http://domestika.org> - incredible support, really nice people to work with!
|
85
|
+
|
86
|
+
* Contributors:
|
87
|
+
* [Aitor Garay-Romero](http://github.com/aitorgr)
|
88
|
+
* [Christian Hølmer](http://github.com/hoelmer)
|
89
|
+
* Nico Ritsche
|
90
|
+
* [Cedric Darricau](http://github.com/devsigner)
|
91
|
+
|
92
|
+
Rails routing resources
|
93
|
+
-----------------------
|
94
|
+
* David Black's 'Rails Routing' ebook rocks! - 'Ruby for Rails' too, BTW.
|
95
|
+
* Obie Fernandez's 'The Rails Way' - the definitive RoR reference, great work Obie!
|
96
|
+
* As a part of the impressive Rails Guides set there is an [awesome document about rails routing](http://guides.rails.info/routing_outside_in.html) by Mike Gunderloy:
|
97
|
+
|
98
|
+
|
99
|
+
License
|
100
|
+
-------
|
101
|
+
Copyright (c) 2007 Released under the MIT license (see MIT-LICENSE)
|
102
|
+
Raul Murciano <http://raul.murciano.net>
|
103
|
+
Domestika INTERNET S.L. <http://domestika.org>
|
@@ -0,0 +1,219 @@
|
|
1
|
+
# Author: Raul Murciano [http://raul.murciano.net] for Domestika [http://domestika.org]
|
2
|
+
# Copyright (c) 2007, Released under the MIT license (see MIT-LICENSE)
|
3
|
+
|
4
|
+
module ActionController
|
5
|
+
|
6
|
+
module Routing
|
7
|
+
|
8
|
+
module Translator
|
9
|
+
|
10
|
+
mattr_accessor :prefix_on_default_locale
|
11
|
+
@@prefix_on_default_locale = false
|
12
|
+
|
13
|
+
mattr_accessor :locale_param_key
|
14
|
+
@@locale_param_key = :locale # set to :locale for params[:locale]
|
15
|
+
|
16
|
+
mattr_accessor :original_routes, :original_named_routes, :original_names, :dictionaries
|
17
|
+
|
18
|
+
def self.translate
|
19
|
+
init_dictionaries
|
20
|
+
yield @@dictionaries
|
21
|
+
@using_i18n = false
|
22
|
+
Translator.translate_current_routes
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.translate_from_file(*path)
|
26
|
+
init_dictionaries
|
27
|
+
path = %w(locales routes.yml) if path.blank?
|
28
|
+
file_path = File.join(RAILS_ROOT, path)
|
29
|
+
yaml = YAML.load_file(file_path)
|
30
|
+
yaml.each_pair{ |k,v| @@dictionaries[k.to_s] = v || {} }
|
31
|
+
@using_i18n = false
|
32
|
+
Translator.translate_current_routes
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.i18n(*locales)
|
36
|
+
init_dictionaries
|
37
|
+
locales = I18n.available_locales if locales.blank? && I18n.respond_to?(:available_locales)
|
38
|
+
locales.each{ |locale| @@dictionaries[locale] = {} }
|
39
|
+
@using_i18n = true
|
40
|
+
Translator.translate_current_routes
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def self.default_locale
|
46
|
+
I18n.default_locale.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.init_dictionaries
|
50
|
+
@@dictionaries = { default_locale => {} }
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.available_locales
|
54
|
+
@@dictionaries.keys.map(&:to_s).uniq
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.original_static_segments
|
58
|
+
static_segments = []
|
59
|
+
(@@original_routes || Routes.routes).each do |r|
|
60
|
+
r.segments.select do |s|
|
61
|
+
static_segments << s.value if s.instance_of?(ActionController::Routing::StaticSegment)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
static_segments.uniq.sort
|
65
|
+
end
|
66
|
+
|
67
|
+
# code shared by translation and application helpers,
|
68
|
+
# it generates a suffix code for a given locale: en-US -> en_us
|
69
|
+
def self.locale_suffix_code
|
70
|
+
'locale.to_s.underscore'
|
71
|
+
end
|
72
|
+
|
73
|
+
class_eval <<-FOO
|
74
|
+
def self.locale_suffix(locale)
|
75
|
+
#{self.locale_suffix_code}
|
76
|
+
end
|
77
|
+
FOO
|
78
|
+
def self.translate_current_routes
|
79
|
+
|
80
|
+
RAILS_DEFAULT_LOGGER.info "Translating routes (default locale: #{default_locale})" if defined? RAILS_DEFAULT_LOGGER
|
81
|
+
|
82
|
+
@@original_routes = Routes.routes.dup # Array [routeA, routeB, ...]
|
83
|
+
@@original_named_routes = Routes.named_routes.routes.dup # Hash {:name => :route}
|
84
|
+
@@original_names = @@original_named_routes.keys
|
85
|
+
|
86
|
+
Routes.clear!
|
87
|
+
new_routes = []
|
88
|
+
new_named_routes = {}
|
89
|
+
|
90
|
+
@@original_routes.each do |old_route|
|
91
|
+
|
92
|
+
old_name = @@original_named_routes.index(old_route)
|
93
|
+
# process and add the translated ones
|
94
|
+
trans_routes, trans_named_routes = translate_route(old_route, old_name)
|
95
|
+
|
96
|
+
if old_name
|
97
|
+
new_named_routes.merge! trans_named_routes
|
98
|
+
end
|
99
|
+
|
100
|
+
new_routes.concat(trans_routes)
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
Routes.routes = new_routes
|
105
|
+
new_named_routes.each { |name, r| Routes.named_routes.add name, r }
|
106
|
+
|
107
|
+
@@original_names.each{ |old_name| add_untranslated_helpers_to_controllers_and_views(old_name) }
|
108
|
+
end
|
109
|
+
|
110
|
+
# The untranslated helper (root_path instead root_en_path) redirects according to the current locale
|
111
|
+
def self.add_untranslated_helpers_to_controllers_and_views(old_name)
|
112
|
+
|
113
|
+
['path', 'url'].each do |suffix|
|
114
|
+
new_helper_name = "#{old_name}_#{suffix}"
|
115
|
+
def_new_helper = <<-DEF_NEW_HELPER
|
116
|
+
def #{new_helper_name}(*args)
|
117
|
+
send("#{old_name}_\#{locale_suffix(I18n.locale)}_#{suffix}", *args)
|
118
|
+
end
|
119
|
+
DEF_NEW_HELPER
|
120
|
+
|
121
|
+
[ActionController::Base, ActionView::Base, ActionMailer::Base].each { |d| d.module_eval(def_new_helper) }
|
122
|
+
ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.add_prefix?(lang)
|
127
|
+
@@prefix_on_default_locale || lang != default_locale
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.translate_static_segment(segment, locale)
|
131
|
+
if @using_i18n
|
132
|
+
tmp = I18n.locale
|
133
|
+
I18n.locale = locale
|
134
|
+
value = I18n.t segment.value, :default => segment.value.dup
|
135
|
+
I18n.locale = tmp
|
136
|
+
else
|
137
|
+
value = @@dictionaries[locale][segment.value] || segment.value.dup
|
138
|
+
end
|
139
|
+
StaticSegment.new(value, :raw => segment.raw, :optional => segment.optional?)
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.locale_segments(orig, locale)
|
143
|
+
segments = []
|
144
|
+
|
145
|
+
if add_prefix?(locale) # initial prefix i.e: /en-US
|
146
|
+
divider = DividerSegment.new(orig.segments.first.value, :optional => false) # divider ('/')
|
147
|
+
static = StaticSegment.new(locale, :optional => false) # static ('en-US')
|
148
|
+
segments += [divider, static]
|
149
|
+
end
|
150
|
+
|
151
|
+
orig.segments.each do |s|
|
152
|
+
if s.instance_of?(StaticSegment)
|
153
|
+
new_segment = translate_static_segment(s, locale)
|
154
|
+
else
|
155
|
+
new_segment = s.dup # just reference the original
|
156
|
+
end
|
157
|
+
segments << new_segment
|
158
|
+
end
|
159
|
+
segments
|
160
|
+
end
|
161
|
+
|
162
|
+
def self.locale_requirements(orig, locale)
|
163
|
+
orig.requirements.merge(@@locale_param_key => locale)
|
164
|
+
end
|
165
|
+
|
166
|
+
def self.translate_route_by_locale(orig, locale, orig_name=nil)
|
167
|
+
segments = locale_segments(orig, locale)
|
168
|
+
requirements = locale_requirements(orig, locale)
|
169
|
+
conditions = orig.conditions
|
170
|
+
|
171
|
+
Route.new(segments, requirements, conditions).freeze
|
172
|
+
end
|
173
|
+
|
174
|
+
def self.root_route?(route)
|
175
|
+
route.segments.length == 1
|
176
|
+
end
|
177
|
+
|
178
|
+
def self.translate_route(route, route_name = nil)
|
179
|
+
new_routes = []
|
180
|
+
new_named_routes = {}
|
181
|
+
|
182
|
+
if root_route?(route) && prefix_on_default_locale
|
183
|
+
# add the root route "as is" in addition to the translated versions
|
184
|
+
new_routes << route
|
185
|
+
new_named_routes[route_name] = route
|
186
|
+
end
|
187
|
+
|
188
|
+
available_locales.each do |locale|
|
189
|
+
translated = translate_route_by_locale(route, locale, route_name)
|
190
|
+
new_routes << translated
|
191
|
+
locale_suffix = locale_suffix(locale)
|
192
|
+
new_named_routes["#{route_name}_#{locale_suffix}".to_sym] = translated if route_name
|
193
|
+
end
|
194
|
+
[new_routes, new_named_routes]
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
# Add set_locale_from_url to controllers
|
203
|
+
ActionController::Base.class_eval do
|
204
|
+
private
|
205
|
+
def set_locale_from_url
|
206
|
+
I18n.locale = params[ActionController::Routing::Translator.locale_param_key]
|
207
|
+
default_url_options({ActionController::Routing::Translator => I18n.locale })
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Add locale_suffix to controllers, views and mailers
|
212
|
+
[ActionController::Base, ActionView::Base, ActionMailer::Base].map do |klass|
|
213
|
+
klass.class_eval do
|
214
|
+
private
|
215
|
+
def locale_suffix(locale)
|
216
|
+
eval ActionController::Routing::Translator.locale_suffix_code
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# monkeypatch I18n to get the available locales
|
2
|
+
# (not strictly needed to use translate_routes, but recommended anyway)
|
3
|
+
module I18n
|
4
|
+
class << self
|
5
|
+
def available_locales
|
6
|
+
backend.available_locales
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Backend
|
11
|
+
class Simple
|
12
|
+
def available_locales
|
13
|
+
init_translations unless initialized?
|
14
|
+
translations.keys
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# load translation files from RAILS_ROOT/locales
|
21
|
+
[:rb, :yml].each do |format|
|
22
|
+
I18n.load_path = Dir[File.join(RAILS_ROOT, 'locales', '*.{rb,yml}') ]
|
23
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Author: Raul Murciano [http://raul.murciano.net] for Domestika [http://domestika.org]
|
2
|
+
# Copyright (c) 2007, Released under the MIT license (see MIT-LICENSE)
|
3
|
+
|
4
|
+
require 'test_help'
|
5
|
+
|
6
|
+
# Include default lang on your test requests (test requests doesn't support default_url_options):
|
7
|
+
ActionController::TestProcess.class_eval do
|
8
|
+
unless method_defined?(:process_without_default_language)
|
9
|
+
def process_with_default_language(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
|
10
|
+
lang_pair = {:locale, I18n.default_locale.to_s}
|
11
|
+
parameters = lang_pair.merge(parameters) rescue lang_pair
|
12
|
+
process_without_default_language(action, parameters, session, flash, http_method)
|
13
|
+
end
|
14
|
+
|
15
|
+
alias :process_without_default_language :process
|
16
|
+
alias :process :process_with_default_language
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Add untranslated helper for named routes to integration tests
|
21
|
+
ActionController::Integration::Session.class_eval do
|
22
|
+
['path', 'url'].each do |suffix|
|
23
|
+
ActionController::Routing::Translator.original_names.each do |old_name|
|
24
|
+
new_helper_name = "#{old_name}_#{suffix}"
|
25
|
+
def_new_helper = <<-DEF_NEW_HELPER
|
26
|
+
def #{new_helper_name}(*args)
|
27
|
+
send("#{old_name}_#{ActionController::Routing::Translator.locale_suffix(I18n.default_locale)}_#{suffix}", *args)
|
28
|
+
end
|
29
|
+
DEF_NEW_HELPER
|
30
|
+
eval def_new_helper
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,323 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
%w(actionpack activesupport actionmailer).each{ |gem_lib| gem gem_lib, '2.3.10' }
|
5
|
+
%w(activesupport actionpack actionmailer action_controller).each{ |lib| require lib }
|
6
|
+
|
7
|
+
plugin_root = File.join(File.dirname(__FILE__), '..')
|
8
|
+
require "#{plugin_root}/lib/translate_routes"
|
9
|
+
RAILS_ROOT = plugin_root
|
10
|
+
|
11
|
+
class PeopleController < ActionController::Base; end
|
12
|
+
|
13
|
+
class TranslateRoutesTest < ActionController::TestCase
|
14
|
+
|
15
|
+
include ActionController::Assertions::RoutingAssertions
|
16
|
+
|
17
|
+
def setup
|
18
|
+
@controller = ActionController::Base.new
|
19
|
+
@view = ActionView::Base.new
|
20
|
+
ActionController::Routing::Routes.clear!
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# Unnamed routes with prefix on default locale:
|
25
|
+
|
26
|
+
def test_unnamed_empty_route_with_prefix
|
27
|
+
ActionController::Routing::Routes.draw { |map| map.connect '', :controller => 'people', :action => 'index' }
|
28
|
+
config_default_locale_settings('en', true)
|
29
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
30
|
+
|
31
|
+
assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
32
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_unnamed_root_route_with_prefix
|
36
|
+
ActionController::Routing::Routes.draw { |map| map.connect '/', :controller => 'people', :action => 'index'}
|
37
|
+
config_default_locale_settings('es', true)
|
38
|
+
ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml'
|
39
|
+
|
40
|
+
assert_routing '/', :controller => 'people', :action => 'index'
|
41
|
+
assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
42
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_unnamed_untranslated_route_with_prefix
|
46
|
+
ActionController::Routing::Routes.draw { |map| map.connect 'foo', :controller => 'people', :action => 'index' }
|
47
|
+
config_default_locale_settings('en', true)
|
48
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
49
|
+
|
50
|
+
assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
|
51
|
+
assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_unnamed_translated_route_on_default_locale_with_prefix
|
55
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
56
|
+
config_default_locale_settings('es', true)
|
57
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
58
|
+
|
59
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_unnamed_translated_route_on_non_default_locale_with_prefix
|
63
|
+
ActionController::Routing::Routes.draw { |map| map.connect 'people', :controller => 'people', :action => 'index' }
|
64
|
+
config_default_locale_settings('en', true)
|
65
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
66
|
+
|
67
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
68
|
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# Unnamed routes without prefix on default locale:
|
73
|
+
|
74
|
+
def test_unnamed_empty_route_without_prefix
|
75
|
+
ActionController::Routing::Routes.draw { |map| map.connect '', :controller => 'people', :action => 'index' }
|
76
|
+
config_default_locale_settings('en', false)
|
77
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
78
|
+
|
79
|
+
assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
80
|
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'en'
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_unnamed_root_route_without_prefix
|
84
|
+
ActionController::Routing::Routes.draw { |map| map.connect '/', :controller => 'people', :action => 'index'}
|
85
|
+
config_default_locale_settings('es', false)
|
86
|
+
ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml'
|
87
|
+
|
88
|
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
|
89
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
90
|
+
assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_unnamed_untranslated_route_without_prefix
|
94
|
+
ActionController::Routing::Routes.draw { |map| map.connect 'foo', :controller => 'people', :action => 'index'}
|
95
|
+
config_default_locale_settings('en', false)
|
96
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
97
|
+
|
98
|
+
assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
|
99
|
+
assert_routing '/foo', :controller => 'people', :action => 'index', :locale => 'en'
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_unnamed_translated_route_on_default_locale_without_prefix
|
103
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
104
|
+
config_default_locale_settings('es', false)
|
105
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
106
|
+
|
107
|
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
|
108
|
+
assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es'
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_unnamed_translated_route_on_non_default_locale_without_prefix
|
112
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
113
|
+
config_default_locale_settings('en', false)
|
114
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
115
|
+
|
116
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
117
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
# Named routes with prefix on default locale:
|
122
|
+
|
123
|
+
def test_named_empty_route_with_prefix
|
124
|
+
ActionController::Routing::Routes.draw { |map| map.people '', :controller => 'people', :action => 'index' }
|
125
|
+
config_default_locale_settings('en', true)
|
126
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
127
|
+
|
128
|
+
assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
129
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
130
|
+
assert_helpers_include :people_en, :people_es, :people
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_named_root_route_with_prefix
|
134
|
+
ActionController::Routing::Routes.draw { |map| map.root :controller => 'people', :action => 'index'}
|
135
|
+
config_default_locale_settings('es', true)
|
136
|
+
ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml'
|
137
|
+
|
138
|
+
assert_routing '/', :controller => 'people', :action => 'index'
|
139
|
+
assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
140
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_named_untranslated_route_with_prefix
|
144
|
+
ActionController::Routing::Routes.draw { |map| map.people 'foo', :controller => 'people', :action => 'index'}
|
145
|
+
config_default_locale_settings('en', true)
|
146
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
147
|
+
|
148
|
+
assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
|
149
|
+
assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
|
150
|
+
assert_helpers_include :people_en, :people_es, :people
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_named_translated_route_on_default_locale_with_prefix
|
154
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
155
|
+
config_default_locale_settings('es', true)
|
156
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
157
|
+
|
158
|
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
|
159
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
160
|
+
assert_helpers_include :people_en, :people_es, :people
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_named_translated_route_on_non_default_locale_with_prefix
|
164
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index' }
|
165
|
+
config_default_locale_settings('en', true)
|
166
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
167
|
+
|
168
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
169
|
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
|
170
|
+
assert_helpers_include :people_en, :people_es, :people
|
171
|
+
end
|
172
|
+
|
173
|
+
# Named routes without prefix on default locale:
|
174
|
+
|
175
|
+
def test_named_empty_route_without_prefix
|
176
|
+
ActionController::Routing::Routes.draw { |map| map.people '', :controller => 'people', :action => 'index'}
|
177
|
+
config_default_locale_settings('es', false)
|
178
|
+
ActionController::Routing::Translator.translate { |t| t['es'] = {}; t['en'] = {'people' => 'gente'}; }
|
179
|
+
|
180
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
181
|
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
|
182
|
+
assert_routing '', :controller => 'people', :action => 'index', :locale => 'es'
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_named_root_route_without_prefix
|
186
|
+
ActionController::Routing::Routes.draw { |map| map.root :controller => 'people', :action => 'index'}
|
187
|
+
config_default_locale_settings('es', false)
|
188
|
+
ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml'
|
189
|
+
|
190
|
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
|
191
|
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
|
192
|
+
assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_named_untranslated_route_without_prefix
|
196
|
+
ActionController::Routing::Routes.draw { |map| map.people 'foo', :controller => 'people', :action => 'index'}
|
197
|
+
config_default_locale_settings('es', false)
|
198
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
199
|
+
|
200
|
+
assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
|
201
|
+
assert_routing 'foo', :controller => 'people', :action => 'index', :locale => 'es'
|
202
|
+
assert_helpers_include :people_en, :people_es, :people
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_named_translated_route_on_default_locale_without_prefix
|
206
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
207
|
+
config_default_locale_settings('es', false)
|
208
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
209
|
+
|
210
|
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
|
211
|
+
assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es'
|
212
|
+
assert_helpers_include :people_en, :people_es, :people
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_named_translated_route_on_non_default_locale_without_prefix
|
216
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
217
|
+
config_default_locale_settings('en', false)
|
218
|
+
ActionController::Routing::Translator.translate { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
|
219
|
+
|
220
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
221
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
222
|
+
assert_helpers_include :people_en, :people_es, :people
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_languages_load_from_file
|
226
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
227
|
+
config_default_locale_settings('en', false)
|
228
|
+
ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml'
|
229
|
+
|
230
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
231
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
232
|
+
assert_helpers_include :people_en, :people_es, :people
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_languages_load_from_file_without_dictionary_for_default_locale
|
236
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
237
|
+
config_default_locale_settings('fr', false)
|
238
|
+
ActionController::Routing::Translator.translate_from_file 'test', 'locales', 'routes.yml'
|
239
|
+
|
240
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'fr'
|
241
|
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
|
242
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
243
|
+
assert_helpers_include :people_fr, :people_en, :people_es, :people
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_i18n_based_translations_setting_locales
|
247
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
248
|
+
config_default_locale_settings('en', false)
|
249
|
+
I18n.backend = StubbedI18nBackend
|
250
|
+
ActionController::Routing::Translator.i18n('es')
|
251
|
+
|
252
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
253
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
254
|
+
assert_helpers_include :people_en, :people_es, :people
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_i18n_based_translations_taking_i18n_available_locales
|
258
|
+
ActionController::Routing::Routes.draw { |map| map.people 'people', :controller => 'people', :action => 'index'}
|
259
|
+
config_default_locale_settings('en', false)
|
260
|
+
I18n.stubs(:available_locales).at_least_once.returns StubbedI18nBackend.available_locales
|
261
|
+
I18n.backend = StubbedI18nBackend
|
262
|
+
ActionController::Routing::Translator.i18n
|
263
|
+
|
264
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
265
|
+
assert_routing '/fr/people', :controller => 'people', :action => 'index', :locale => 'fr'
|
266
|
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
267
|
+
assert_helpers_include :people_fr, :people_en, :people_es, :people
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_action_controller_gets_locale_setter
|
271
|
+
ActionController::Base.instance_methods.include?('set_locale_from_url')
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_action_controller_gets_locale_suffix_helper
|
275
|
+
ActionController::Base.instance_methods.include?('locale_suffix')
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_action_view_gets_locale_suffix_helper
|
279
|
+
ActionView::Base.instance_methods.include?('locale_suffix')
|
280
|
+
end
|
281
|
+
|
282
|
+
private
|
283
|
+
|
284
|
+
def assert_helpers_include(*helpers)
|
285
|
+
helpers.each do |helper|
|
286
|
+
['_url', '_path'].each do |suffix|
|
287
|
+
[@controller, @view].each { |obj| assert_respond_to obj, "#{helper}#{suffix}".to_sym }
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
def assert_unrecognized_route(route_path, options)
|
293
|
+
assert_raise ActionController::RoutingError do
|
294
|
+
assert_routing route_path, options
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
def config_default_locale_settings(locale, with_prefix)
|
299
|
+
I18n.default_locale = locale
|
300
|
+
ActionController::Routing::Translator.prefix_on_default_locale = with_prefix
|
301
|
+
end
|
302
|
+
|
303
|
+
class StubbedI18nBackend
|
304
|
+
|
305
|
+
|
306
|
+
@@translations = {
|
307
|
+
'es' => { 'people' => 'gente'},
|
308
|
+
'fr' => {} # empty on purpose to test behaviour on incompleteness scenarios
|
309
|
+
}
|
310
|
+
|
311
|
+
def self.translate(locale, key, options)
|
312
|
+
@@translations[locale.to_s][key] || options[:default]
|
313
|
+
rescue
|
314
|
+
options[:default]
|
315
|
+
end
|
316
|
+
|
317
|
+
def self.available_locales
|
318
|
+
@@translations.keys
|
319
|
+
end
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: translate_routes_rails_2_3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Raul Murciano
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-14 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Translates the Rails routes of your application into the languages defined in your locale files
|
23
|
+
email: raul@murciano.net
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- ChangeLog
|
30
|
+
- README.markdown
|
31
|
+
files:
|
32
|
+
- lib/translate_routes.rb
|
33
|
+
- lib/translate_routes_i18n_available_locales.rb
|
34
|
+
- lib/translate_routes_test_helper.rb
|
35
|
+
- ChangeLog
|
36
|
+
- README.markdown
|
37
|
+
- test/translate_routes_test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/raul/translate_routes
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.4.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Translate your Rails routes in a simple manner (Rails2.3 branch)
|
72
|
+
test_files:
|
73
|
+
- test/translate_routes_test.rb
|