route_translator 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,65 +3,26 @@ require 'action_controller'
3
3
  require 'action_mailer'
4
4
  require 'action_dispatch'
5
5
 
6
- require 'route_translator/route_set'
7
- require 'route_translator/routes_reloader'
8
- require 'route_translator/railtie' if defined?(Rails::Railtie)
6
+ require File.expand_path('../route_translator/extensions', __FILE__)
7
+ require File.expand_path('../route_translator/translator', __FILE__)
9
8
 
10
9
  module RouteTranslator
11
10
 
12
11
  TRANSLATABLE_SEGMENT = /^([-_a-zA-Z0-9]+)(\()?/.freeze
13
- LOCALE_PARAM_KEY = :locale
14
- ROUTE_HELPER_CONTAINER = [
15
- ActionController::Base,
16
- ActionView::Base,
17
- ActionMailer::Base,
18
- ActionDispatch::Routing::UrlFor
19
- ].freeze
20
12
 
21
- Configuration = Struct.new(:force_locale, :generate_unlocalized_routes, :translation_file)
13
+ Configuration = Struct.new(:force_locale, :generate_unlocalized_routes, :translation_file, :locale_param_key)
22
14
 
23
- def self.locale_suffix locale
24
- locale.to_s.underscore
25
- end
26
-
27
- def self.config
15
+ def self.config(&block)
28
16
  @config ||= Configuration.new
17
+ @config.force_locale ||= false
18
+ @config.generate_unlocalized_routes ||= false
19
+ @config.locale_param_key ||= :locale
20
+ yield @config if block
21
+ @config
29
22
  end
30
23
 
31
- # Attributes
32
-
33
- module Controller
34
- def set_locale_from_url
35
- I18n.locale = params[RouteTranslator::LOCALE_PARAM_KEY]
36
- end
37
- end
38
-
39
- module Mapper
40
- #yield the block and add all the routes created
41
- #in it to the localized_routes array
42
- def localized
43
- routes_before = @set.routes.map(&:to_s)
44
- yield
45
- routes_after = @set.routes.map(&:to_s)
46
- @set.localized_routes ||= []
47
- @set.localized_routes.concat(routes_after - routes_before)
48
- @set.localized_routes.uniq!
49
- end
24
+ def self.locale_param_key
25
+ self.config.locale_param_key
50
26
  end
51
27
 
52
- ActiveSupport.run_load_hooks(:route_translator, self)
53
28
  end
54
-
55
- # Add locale_suffix to controllers, views and mailers
56
- RouteTranslator::ROUTE_HELPER_CONTAINER.each do |klass|
57
- klass.class_eval do
58
- private
59
- def locale_suffix locale
60
- RouteTranslator.locale_suffix locale
61
- end
62
- end
63
- end
64
-
65
- ActionController::Base.send(:include, RouteTranslator::Controller)
66
- ActionDispatch::Routing::Mapper.send(:include, RouteTranslator::Mapper)
67
- ActionDispatch::Routing::RouteSet.send(:include, RouteTranslator::RouteSet)
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../extensions/mapper', __FILE__)
2
+ require File.expand_path('../extensions/route_set', __FILE__)
3
+ require File.expand_path('../extensions/action_controller', __FILE__)
@@ -0,0 +1,11 @@
1
+ require 'action_controller'
2
+
3
+ module ActionController
4
+ class Base
5
+ before_filter :set_locale_from_url
6
+
7
+ def set_locale_from_url
8
+ I18n.locale = params[RouteTranslator.locale_param_key]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ require 'action_dispatch'
2
+
3
+ module ActionDispatch
4
+ module Routing
5
+ class Mapper
6
+ def localized
7
+ @localized = true
8
+ yield
9
+ @localized = false
10
+ end
11
+
12
+ if instance_methods.map(&:to_s).include?('add_route')
13
+ def add_route(action, options) # :nodoc:
14
+ path = path_for_action(action, options.delete(:path))
15
+
16
+ if action.to_s =~ /^[\w\/]+$/
17
+ options[:action] ||= action unless action.to_s.include?("/")
18
+ else
19
+ action = nil
20
+ end
21
+
22
+ if !options.fetch(:as, true)
23
+ options.delete(:as)
24
+ else
25
+ options[:as] = name_for_action(options[:as], action)
26
+ end
27
+
28
+ mapping = Mapping.new(@set, @scope, path, options)
29
+ app, conditions, requirements, defaults, as, anchor = mapping.to_route
30
+ if @localized
31
+ @set.add_localized_route(app, conditions, requirements, defaults, as, anchor)
32
+ else
33
+ @set.add_route(app, conditions, requirements, defaults, as, anchor)
34
+ end
35
+ end
36
+ else
37
+ module Base
38
+ def match(path, options=nil)
39
+ mapping = Mapping.new(@set, @scope, path, options || {})
40
+ app, conditions, requirements, defaults, as, anchor = mapping.to_route
41
+ if @localized
42
+ @set.add_localized_route(app, conditions, requirements, defaults, as, anchor)
43
+ else
44
+ @set.add_route(app, conditions, requirements, defaults, as, anchor)
45
+ end
46
+ self
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ require 'action_dispatch'
2
+
3
+ module ActionDispatch
4
+ module Routing
5
+ class RouteSet
6
+ def add_localized_route(app, conditions = {}, requirements = {}, defaults = {}, as = nil, anchor = true)
7
+ RouteTranslator::Translator.translations_for(app, conditions, requirements, defaults, as, anchor, self) do |*translated_args|
8
+ add_route(*translated_args)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,84 @@
1
+ module RouteTranslator
2
+ module Translator
3
+ # Add standard route helpers for default locale e.g.
4
+ # I18n.locale = :de
5
+ # people_path -> people_de_path
6
+ # I18n.locale = :fr
7
+ # people_path -> people_fr_path
8
+ def self.add_untranslated_helpers_to_controllers_and_views(old_name, helper_container)
9
+ ['path', 'url'].each do |suffix|
10
+ new_helper_name = "#{old_name}_#{suffix}"
11
+
12
+ helper_container.__send__(:define_method, new_helper_name) do |*args|
13
+ locale_suffix = I18n.locale.to_s.underscore
14
+ if respond_to?("#{old_name}_#{locale_suffix}_#{suffix}")
15
+ __send__("#{old_name}_#{locale_suffix}_#{suffix}", *args)
16
+ else
17
+ __send__("#{old_name}_#{I18n.default_locale.to_s.underscore}_#{suffix}", *args)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.translations_for(app, conditions, requirements, defaults, route_name, anchor, route_set, &block)
24
+ add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes.module)
25
+ I18n.available_locales.each do |locale|
26
+ new_conditions = conditions.dup
27
+ new_conditions[:path_info] = translate_path(conditions[:path_info], locale)
28
+ if new_conditions[:required_defaults] && !new_conditions[:required_defaults].include?(RouteTranslator.locale_param_key)
29
+ new_conditions[:required_defaults] << RouteTranslator.locale_param_key if new_conditions[:required_defaults]
30
+ end
31
+ new_defaults = defaults.merge(RouteTranslator.locale_param_key => locale.to_s)
32
+ new_requirements = requirements.merge(RouteTranslator.locale_param_key => locale.to_s)
33
+ new_route_name = translate_name(route_name, locale)
34
+ new_route_name = nil if route_set.named_routes.routes[new_route_name.to_sym] #TODO: Investigate this :(
35
+ block.call(app, new_conditions, new_requirements, new_defaults, new_route_name, anchor)
36
+ end
37
+ block.call(app, conditions, requirements, defaults, route_name, anchor) if RouteTranslator.config.generate_unlocalized_routes
38
+ end
39
+
40
+ # Translates a path and adds the locale prefix.
41
+ def self.translate_path(path, locale)
42
+ new_path = path.dup
43
+ final_optional_segments = new_path.slice!(/(\(.+\))$/)
44
+ new_path = new_path.split("/").map{|seg| translate_path_segment(seg, locale)}.join('/')
45
+
46
+ # Add locale prefix if it's not the default locale,
47
+ # or forcing locale to all routes,
48
+ # or already generating actual unlocalized routes
49
+ if !default_locale?(locale) || RouteTranslator.config.force_locale || RouteTranslator.config.generate_unlocalized_routes
50
+ new_path = "/#{locale.to_s.downcase}#{new_path}"
51
+ end
52
+
53
+ new_path = "/" if new_path.blank?
54
+
55
+ "#{new_path}#{final_optional_segments}"
56
+ end
57
+
58
+ def self.translate_name(n, locale)
59
+ "#{n}_#{locale.to_s.underscore}" if n.present?
60
+ end
61
+
62
+ def self.default_locale?(locale)
63
+ I18n.default_locale.to_sym == locale.to_sym
64
+ end
65
+
66
+ # Tries to translate a single path segment. If the path segment
67
+ # contains sth. like a optional format "people(.:format)", only
68
+ # "people" will be translated, if there is no translation, the path
69
+ # segment is blank or begins with a ":" (param key), the segment
70
+ # is returned untouched
71
+ def self.translate_path_segment segment, locale
72
+ return segment if segment.blank? or segment.starts_with?(":")
73
+
74
+ match = TRANSLATABLE_SEGMENT.match(segment)[1] rescue nil
75
+
76
+ (translate_string(match, locale) || segment)
77
+ end
78
+
79
+ def self.translate_string(str, locale)
80
+ fallback = I18n.translate(str, :locale => locale, :default => str)
81
+ I18n.translate("routes.#{str}", :locale => locale, :default => fallback)
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module RouteTranslator
2
- VERSION = '2.0.1'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -0,0 +1,5 @@
1
+ class DummyController < ActionController::Base
2
+ def dummy
3
+ render :text => I18n.locale
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,13 @@
1
+ require "action_controller/railtie"
2
+ begin
3
+ require "active_resource/railtie"
4
+ rescue LoadError
5
+ end
6
+ require "route_translator"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # config.logger = Logger.new(File.new("/dev/null", 'w'))
11
+ config.active_support.deprecation = :log
12
+ end
13
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = '831d41b30a9e2d662a2d0c6f1f05b5f0b7db5d59446a18686b122e623e10bc5462eb48f9c9245f3e8b00cbe0cf007786482ba7012331de8f1bde93985c4d9686'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,9 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ routes:
6
+ dummy: 'dummy'
7
+ es:
8
+ routes:
9
+ dummy: 'dummy'
@@ -0,0 +1,6 @@
1
+ Dummy::Application.routes.draw do
2
+ localized do
3
+ get 'dummy', :to => 'dummy#dummy'
4
+ end
5
+ mount DummyMountedApp => '/dummy_mounted_app'
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'sinatra'
2
+ class DummyMountedApp < Sinatra::Base
3
+ get "/" do
4
+ "Good"
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+ require 'route_translator'
3
+ require File.expand_path('../dummy/dummy_mounted_app', __FILE__)
4
+ require File.expand_path('../dummy/config/environment', __FILE__)
5
+
6
+ class_to_inherit = defined?(ActionDispatch::IntegrationTest) ? ActionDispatch::IntegrationTest : ActionController::IntegrationTest
7
+ class IntegrationTest < class_to_inherit
8
+ include RouteTranslator::TestHelper
9
+
10
+ def test_set_locale_from_params
11
+ config_default_locale_settings 'en'
12
+
13
+ get '/es/dummy'
14
+ assert_equal 'es', @response.body
15
+ assert_response :success
16
+ end
17
+
18
+ def test_mounted_apps_work_with_correct_path
19
+ get 'dummy_mounted_app'
20
+ assert_equal "Good", @response.body
21
+ assert_response :success
22
+ end
23
+ end
@@ -1,10 +1,6 @@
1
- # es:
2
- # people: gente
3
- #
4
- # en:
5
-
6
1
  es:
7
2
  people: gente
8
3
  products: productos
9
4
 
10
- en:
5
+ en:
6
+ m: m
@@ -1,13 +1,5 @@
1
- require 'test/unit'
2
- require 'mocha'
3
-
4
- require "rails"
5
- require "action_controller/railtie"
6
-
7
- require 'route_translator'
8
- require "route_translator/test_request"
9
-
10
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+ require 'route_translator'
11
3
 
12
4
  class PeopleController < ActionController::Base; end
13
5
  class ProductsController < ActionController::Base; end
@@ -16,60 +8,70 @@ class TranslateRoutesTest < ActionController::TestCase
16
8
  include ActionDispatch::Assertions::RoutingAssertions
17
9
  include RouteTranslator::TestHelper
18
10
 
19
- def config_default_locale_settings(locale)
20
- I18n.default_locale = locale
21
- end
22
-
23
- def config_force_locale(boolean)
24
- RouteTranslator.config.force_locale = boolean
25
- end
26
-
27
- def config_generate_unlocalized_routes(boolean)
28
- RouteTranslator.config.generate_unlocalized_routes = boolean
29
- end
30
-
31
- def config_translation_file (file)
32
- RouteTranslator.config.translation_file = file
33
- end
34
-
35
11
  def setup
36
- setup_application
37
-
38
- @controller = ActionController::Base.new
39
- @view = ActionView::Base.new
40
12
  @routes = ActionDispatch::Routing::RouteSet.new
13
+ I18n.backend = I18n::Backend::Simple.new
14
+ I18n.load_path = [ File.expand_path('../locales/routes.yml', __FILE__) ]
15
+ I18n.reload!
41
16
  end
42
17
 
43
18
  def teardown
44
19
  config_force_locale false
45
20
  config_generate_unlocalized_routes false
46
21
  config_default_locale_settings("en")
47
- routes_file ""
48
- FileUtils.rm_rf "#{tmp_path}/log"
49
22
  end
50
23
 
51
24
  def test_unnamed_root_route
52
- @routes.draw do
25
+ config_default_locale_settings 'en'
26
+ draw_routes do
53
27
  localized do
54
28
  root :to => 'people#index'
55
29
  end
56
30
  end
57
- config_default_locale_settings 'en'
58
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
59
31
 
60
32
  assert_routing '/', :controller => 'people', :action => 'index', :locale => 'en'
61
33
  assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
62
34
  end
63
35
 
64
- def test_unnamed_root_route_without_prefix
65
- @routes.draw do
36
+ def test_resources
37
+ config_default_locale_settings 'es'
38
+
39
+ draw_routes do
66
40
  localized do
67
- root :to => 'people#index'
41
+ resources :products
42
+ end
43
+ end
44
+
45
+
46
+ assert_routing '/en/products', :controller => 'products', :action => 'index', :locale => 'en'
47
+ assert_routing '/productos', :controller => 'products', :action => 'index', :locale => 'es'
48
+ assert_routing({:path => '/productos/1', :method => "GET"}, {:controller => 'products', :action => 'show', :id => '1', :locale => 'es'})
49
+ assert_routing({:path => '/productos/1', :method => "PUT"}, {:controller => 'products', :action => 'update', :id => '1', :locale => 'es'})
50
+ end
51
+
52
+ def test_resources_with_only
53
+ config_default_locale_settings 'es'
54
+
55
+ draw_routes do
56
+ localized do
57
+ resources :products, :only => [:index, :show]
68
58
  end
69
59
  end
60
+
61
+ assert_routing '/en/products', :controller => 'products', :action => 'index', :locale => 'en'
62
+ assert_routing '/productos', :controller => 'products', :action => 'index', :locale => 'es'
63
+ assert_routing({:path => '/productos/1', :method => "GET"}, {:controller => 'products', :action => 'show', :id => '1', :locale => 'es'})
64
+ assert_unrecognized_route({:path => '/productos/1', :method => "PUT"}, {:controller => 'products', :action => 'update', :id => '1', :locale => 'es'})
65
+ end
66
+
67
+ def test_unnamed_root_route_without_prefix
70
68
  config_default_locale_settings 'es'
71
69
 
72
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
70
+ draw_routes do
71
+ localized do
72
+ root :to => 'people#index'
73
+ end
74
+ end
73
75
 
74
76
  assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
75
77
  assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
@@ -77,55 +79,53 @@ class TranslateRoutesTest < ActionController::TestCase
77
79
  end
78
80
 
79
81
  def test_unnamed_untranslated_route
80
- @routes.draw do
82
+ config_default_locale_settings 'en'
83
+
84
+ draw_routes do
81
85
  localized do
82
- match 'foo', :to => 'people#index'
86
+ get 'foo', :to => 'people#index'
83
87
  end
84
88
  end
85
- config_default_locale_settings 'en'
86
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
87
89
 
88
90
  assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
89
91
  assert_routing '/foo', :controller => 'people', :action => 'index', :locale => 'en'
90
92
  end
91
93
 
92
94
  def test_unnamed_translated_route_on_default_locale
93
- @routes.draw { match 'people', :to => 'people#index' }
94
- @routes.draw do
95
+ config_default_locale_settings 'es'
96
+
97
+ @routes.draw { get 'people', :to => 'people#index' }
98
+ draw_routes do
95
99
  localized do
96
- match 'people', :to => 'people#index'
100
+ get 'people', :to => 'people#index'
97
101
  end
98
102
  end
99
103
 
100
- config_default_locale_settings 'es'
101
-
102
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
103
104
 
104
105
  assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
105
106
  assert_routing '/gente', :controller => 'people', :action => 'index', :locale => 'es'
106
107
  end
107
108
 
108
109
  def test_unnamed_translated_route_on_non_default_locale
109
- @routes.draw do
110
+ config_default_locale_settings 'en'
111
+ draw_routes do
110
112
  localized do
111
- match 'people', :to => 'people#index'
113
+ get 'people', :to => 'people#index'
112
114
  end
113
115
  end
114
- config_default_locale_settings 'en'
115
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
116
116
 
117
117
  assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
118
118
  assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
119
119
  end
120
120
 
121
121
  def test_named_translated_route_with_prefix_must_have_locale_as_static_segment
122
- @routes.draw do
122
+ config_default_locale_settings 'en'
123
+
124
+ draw_routes do
123
125
  localized do
124
- match 'people', :to => 'people#index'
126
+ get 'people', :to => 'people#index'
125
127
  end
126
128
  end
127
- config_default_locale_settings 'en'
128
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
129
129
 
130
130
  # we check the string representation of the route,
131
131
  # if it stores locale as a dynamic segment it would be represented as: "/:locale/gente"
@@ -133,13 +133,12 @@ class TranslateRoutesTest < ActionController::TestCase
133
133
  end
134
134
 
135
135
  def test_named_empty_route_without_prefix
136
- @routes.draw do
136
+ config_default_locale_settings 'es'
137
+ draw_routes do
137
138
  localized do
138
139
  root :to => 'people#index', :as => 'people'
139
140
  end
140
141
  end
141
- config_default_locale_settings 'es'
142
- @routes.translate_with_dictionary{|t| t['es'] = {}; t['en'] = {'people' => 'gente'}; }
143
142
 
144
143
  assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
145
144
  assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
@@ -147,76 +146,73 @@ class TranslateRoutesTest < ActionController::TestCase
147
146
  end
148
147
 
149
148
  def test_named_root_route_without_prefix
150
- @routes.draw do
149
+ config_default_locale_settings 'es'
150
+
151
+ draw_routes do
151
152
  localized do
152
153
  root :to => 'people#index'
153
154
  end
154
155
  end
155
156
 
156
- config_default_locale_settings 'es'
157
-
158
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
159
-
160
157
  assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
161
158
  assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
162
159
  assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
163
160
  end
164
161
 
165
162
  def test_named_untranslated_route_without_prefix
166
- @routes.draw do
163
+ config_default_locale_settings 'es'
164
+
165
+ draw_routes do
167
166
  localized do
168
- match 'foo', :to => 'people#index', :as => 'people'
167
+ get 'foo', :to => 'people#index', :as => 'people'
169
168
  end
170
169
  end
171
- config_default_locale_settings 'es'
172
- @routes.translate_with_dictionary{ |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
173
170
 
174
171
  assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
175
172
  assert_routing '/foo', :controller => 'people', :action => 'index', :locale => 'es'
173
+
176
174
  assert_helpers_include :people_en, :people_es, :people
177
175
  end
178
176
 
179
177
  def test_named_translated_route_on_default_locale_without_prefix
180
- @routes.draw do
178
+ config_default_locale_settings 'es'
179
+
180
+ draw_routes do
181
181
  localized do
182
- match 'people', :to => 'people#index', :as => 'people'
182
+ get 'people', :to => 'people#index', :as => 'people'
183
183
  end
184
184
  end
185
185
 
186
- config_default_locale_settings 'es'
187
-
188
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
189
-
190
186
  assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
191
187
  assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es'
188
+
189
+
192
190
  assert_helpers_include :people_en, :people_es, :people
193
191
  end
194
192
 
195
193
  def test_named_translated_route_on_non_default_locale_without_prefix
196
- @routes.draw do
194
+ draw_routes do
197
195
  localized do
198
- match 'people', :to => 'people#index', :as => 'people'
196
+ get 'people', :to => 'people#index', :as => 'people'
199
197
  end
200
198
  end
201
199
 
202
200
  config_default_locale_settings 'en'
203
201
 
204
- @routes.translate_with_dictionary{|t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
205
-
206
202
  assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
207
203
  assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
204
+
205
+
208
206
  assert_helpers_include :people_en, :people_es, :people
209
207
  end
210
208
 
211
209
  def test_formatted_root_route
212
- @routes.draw do
210
+ draw_routes do
213
211
  localized do
214
212
  root :to => 'people#index', :as => 'root'
215
213
  end
216
214
  end
217
215
 
218
- @routes.translate_with_dictionary{ |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
219
-
220
216
  if formatted_root_route?
221
217
  assert_equal '/(.:format)', path_string(named_route('root_en'))
222
218
  assert_equal '/es(.:format)', path_string(named_route('root_es'))
@@ -226,112 +222,62 @@ class TranslateRoutesTest < ActionController::TestCase
226
222
  end
227
223
  end
228
224
 
229
- def test_routes_locale_prefixes_are_never_downcased
230
- @routes.draw do
231
- localized do
232
- match 'people', :to => 'people#index', :as => 'people'
233
- end
234
- end
225
+ def test_i18n_based_translations_setting_locales
235
226
  config_default_locale_settings 'en'
236
227
 
237
- @routes.translate_with_dictionary{ |t| t['en'] = {}; t['ES'] = {'people' => 'Gente'} }
238
-
239
- assert_routing '/es/Gente', :controller => 'people', :action => 'index', :locale => 'ES'
240
- end
241
-
242
- def test_languages_load_from_file
243
- @routes.draw do
228
+ draw_routes do
244
229
  localized do
245
- match 'people', :to => 'people#index', :as => 'people'
230
+ get 'people', :to => 'people#index', :as => 'people'
246
231
  end
247
232
  end
248
- config_default_locale_settings 'en'
249
-
250
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
251
233
 
252
- assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
253
234
  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_languages_load_from_file_without_dictionary_for_default_locale
258
- @routes.draw do
259
- localized do
260
- match 'people', :to => 'people#index', :as => 'people'
261
- end
262
- end
263
- config_default_locale_settings 'fr'
264
-
265
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
235
+ assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
266
236
 
267
- assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'fr'
268
- assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
269
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
270
- assert_helpers_include :people_fr, :people_en, :people_es, :people
237
+
238
+ assert_helpers_include :people_en, :people_es, :people
271
239
  end
272
240
 
273
- def test_i18n_based_translations_setting_locales
274
- @routes.draw do
275
- localized do
276
- match 'people', :to => 'people#index', :as => 'people'
277
- end
278
- end
241
+ def test_translations_depend_on_available_locales
279
242
  config_default_locale_settings 'en'
280
243
 
281
- I18n.backend = StubbedI18nBackend
282
-
283
- @routes.translate_with_i18n('es')
284
-
285
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
286
- assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
287
- assert_helpers_include :people_en, :people_es, :people
288
- end
289
-
290
- def test_i18n_based_translations_taking_i18n_available_locales
291
- @routes.draw do
292
- localized do
293
- match 'people', :to => 'people#index', :as => 'people'
244
+ I18n.stub(:available_locales, [:es, :en, :fr]) do
245
+ draw_routes do
246
+ localized do
247
+ get 'people', :to => 'people#index', :as => 'people'
248
+ end
294
249
  end
295
250
  end
296
- config_default_locale_settings 'en'
297
- I18n.stubs(:available_locales).at_least_once.returns StubbedI18nBackend.available_locales
298
- I18n.backend = StubbedI18nBackend
299
-
300
- @routes.translate_with_i18n
301
251
 
302
252
  assert_routing '/fr/people', :controller => 'people', :action => 'index', :locale => 'fr'
303
253
  assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
304
254
  assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
255
+
305
256
  assert_helpers_include :people_fr, :people_en, :people_es, :people
306
257
  end
307
258
 
308
259
  def test_2_localized_blocks
309
- @routes.draw do
260
+ draw_routes do
310
261
  localized do
311
- match 'people', :to => 'people#index', :as => 'people'
262
+ get 'people', :to => 'people#index', :as => 'people'
312
263
  end
313
264
  localized do
314
- match 'products', :to => 'products#index', :as => 'products'
265
+ get 'products', :to => 'products#index', :as => 'products'
315
266
  end
316
267
  end
317
- config_default_locale_settings 'en'
318
-
319
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
320
268
 
321
269
  assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
322
270
  assert_routing '/es/productos', :controller => 'products', :action => 'index', :locale => 'es'
323
271
  end
324
272
 
325
273
  def test_not_localizing_routes_outside_blocks
326
- @routes.draw do
274
+ config_default_locale_settings 'en'
275
+ draw_routes do
327
276
  localized do
328
- match 'people', :to => 'people#index', :as => 'people'
277
+ get 'people', :to => 'people#index', :as => 'people'
329
278
  end
330
- match 'products', :to => 'products#index', :as => 'products'
279
+ get 'products', :to => 'products#index', :as => 'products'
331
280
  end
332
- config_default_locale_settings 'en'
333
-
334
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
335
281
 
336
282
  assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
337
283
  assert_routing '/products', :controller => 'products', :action => 'index'
@@ -339,75 +285,47 @@ class TranslateRoutesTest < ActionController::TestCase
339
285
  end
340
286
 
341
287
  def test_force_locale
342
- @routes.draw do
343
- localized do
344
- match 'people', :to => 'people#index', :as => 'people'
345
- end
346
- end
347
-
348
288
  config_default_locale_settings 'en'
349
289
  config_force_locale true
350
290
 
351
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
291
+ draw_routes do
292
+ localized do
293
+ get 'people', :to => 'people#index', :as => 'people'
294
+ end
295
+ end
352
296
 
353
297
  assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
354
298
  assert_unrecognized_route '/people', :controller => 'people', :action => 'index'
355
299
  end
356
300
 
357
301
  def test_generate_unlocalized_routes
358
- @routes.draw do
359
- localized do
360
- match 'people', :to => 'people#index', :as => 'people'
361
- end
362
- end
363
-
364
302
  config_default_locale_settings 'en'
365
303
  config_generate_unlocalized_routes true
366
304
 
367
- @routes.translate_from_file(File.expand_path('locales/routes.yml', File.dirname(__FILE__)))
305
+ draw_routes do
306
+ localized do
307
+ get 'people', :to => 'people#index', :as => 'people'
308
+ end
309
+ end
368
310
 
369
311
  assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
370
312
  assert_routing '/people', :controller => 'people', :action => 'index'
371
313
  end
372
314
 
373
315
  def test_config_translation_file
374
- @routes.draw do
316
+ config_default_locale_settings 'es'
317
+
318
+ draw_routes do
375
319
  localized do
376
320
  root :to => 'people#index'
377
321
  end
378
322
  end
379
323
 
380
- config_default_locale_settings 'es'
381
- config_translation_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
382
-
383
- @routes.translate_from_file
384
-
385
324
  assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
386
325
  assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
387
326
  assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
388
327
  end
389
328
 
390
- def test_auto_translate
391
-
392
- routes_file <<-RUBY
393
- Rails.application.routes.draw do
394
- localized do
395
- root :to => 'people#index'
396
- end
397
- end
398
- RUBY
399
-
400
- config_default_locale_settings 'es'
401
- config_translation_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
402
-
403
- app.reload_routes!
404
-
405
- @routes = app.routes
406
-
407
- assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
408
- assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
409
- assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
410
- end
411
329
 
412
330
  def test_action_controller_gets_locale_setter
413
331
  ActionController::Base.instance_methods.include?('set_locale_from_url')
@@ -420,5 +338,4 @@ class TranslateRoutesTest < ActionController::TestCase
420
338
  def test_action_view_gets_locale_suffix_helper
421
339
  ActionView::Base.instance_methods.include?('locale_suffix')
422
340
  end
423
-
424
341
  end