Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed 'prefix_on_default_locale' config parameter, now it's always …

…disabled
  • Loading branch information...
commit 27735f86ecc8cd5880993580b2e0ca466ce8c1b5 1 parent e7cfdfc
@raul authored
Showing with 59 additions and 187 deletions.
  1. +5 −12 lib/route_translator.rb
  2. +54 −175 test/translate_routes_test.rb
View
17 lib/route_translator.rb
@@ -1,6 +1,5 @@
-# This is my attempt at a restructured TranslateRoutes.
-# I packed everything into a class, which manages the dictionary,
-# and can operate on a specific RouteSet. This class knows nothing
+
+# This class knows nothing
# about Rails.root or Rails.application.routes, and therefor is easier to
# test without an Rails App.
class RouteTranslator
@@ -15,7 +14,7 @@ class RouteTranslator
# Attributes
- attr_accessor :prefix_on_default_locale, :dictionary
+ attr_accessor :dictionary
def available_locales
@available_locales ||= I18n.available_locales.map(&:to_s)
@@ -213,11 +212,9 @@ def translate_route route, locale
[route.app, conditions, requirements, defaults, new_name]
end
- # Check if a prefix for locale should be added. Is true for all
- # non-default locales. A prefix for the default_locale can be forced
- # by setting +prefix_on_default_locale+ to +true+
+ # Add prefix for all non-default locales
def add_prefix? locale
- @prefix_on_default_locale || !default_locale?(locale)
+ !default_locale?(locale)
end
# Translates a path and adds the locale prefix.
@@ -271,10 +268,6 @@ def remove_all_methods_in mod
end
include Translator
- def initialize
- @prefix_on_default_locale = false
- end
-
def locale_suffix locale
self.class.locale_suffix locale
end
View
229 test/translate_routes_test.rb
@@ -14,9 +14,8 @@ class PeopleController < ActionController::Base; end
class TranslateRoutesTest < ActionController::TestCase
include ActionDispatch::Assertions::RoutingAssertions
- def config_default_locale_settings locale, with_prefix
+ def config_default_locale_settings(locale)
I18n.default_locale = locale
- @route_translator.prefix_on_default_locale = with_prefix
end
def translate_routes
@@ -32,178 +31,60 @@ def setup
@route_translator = RouteTranslator.new
end
- # Unnamed routes with prefix on default locale:
-
- def test_unnamed_empty_route_with_prefix
- @routes.draw { root :to => 'people#index' }
- config_default_locale_settings('en', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
- end
-
- def test_unnamed_root_route_with_prefix
- @routes.draw { root :to => 'people#index' }
- config_default_locale_settings('es', true)
- @route_translator.load_dictionary_from_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
-
- assert_routing '/', :controller => 'people', :action => 'index'
- translate_routes
-
- # i think this is the wanted behavior, anyhow, can't get assert_routing to work
- # assert_routing '/', :controller => 'people', :action => 'index'
- assert_recognizes( {:controller => 'people', :action => 'index'}, '/')
- assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
- end
-
- def test_unnamed_untranslated_route_with_prefix
- @routes.draw { match 'foo', :to => 'people#index' }
- config_default_locale_settings('en', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
- end
-
- def test_unnamed_translated_route_on_default_locale_with_prefix
- @routes.draw { match 'people', :to => 'people#index' }
- config_default_locale_settings('es', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
- end
-
- def test_unnamed_translated_route_on_non_default_locale_with_prefix
- @routes.draw { match 'people', :to => 'people#index' }
- config_default_locale_settings('en', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
- end
-
-
- # Unnamed routes without prefix on default locale:
-
- def test_unnamed_empty_route_without_prefix
+ def test_unnamed_root_route
@routes.draw { root :to => 'people#index' }
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
end
-
+
def test_unnamed_root_route_without_prefix
@routes.draw { root :to => 'people#index' }
- config_default_locale_settings('es', false)
+ config_default_locale_settings 'es'
@route_translator.load_dictionary_from_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
translate_routes
-
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
end
-
- def test_unnamed_untranslated_route_without_prefix
+
+ def test_unnamed_untranslated_route
@routes.draw { match 'foo', :to => 'people#index' }
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '/foo', :controller => 'people', :action => 'index', :locale => 'en'
end
-
- def test_unnamed_translated_route_on_default_locale_without_prefix
- @routes.draw { match 'people', :to => 'people#index' } #was named route in original test, despite test title
- config_default_locale_settings('es', false)
+
+ def test_unnamed_translated_route_on_default_locale
+ @routes.draw { match 'people', :to => 'people#index' }
+ config_default_locale_settings 'es'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
- assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es'
+ assert_routing '/gente', :controller => 'people', :action => 'index', :locale => 'es'
end
-
- def test_unnamed_translated_route_on_non_default_locale_without_prefix
- @routes.draw { match 'people', :to => 'people#index' } #was named route in original test, despite test title
- config_default_locale_settings('en', false)
+
+ def test_unnamed_translated_route_on_non_default_locale
+ @routes.draw { match 'people', :to => 'people#index' }
+ config_default_locale_settings 'en'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
end
-
-
- # Named routes with prefix on default locale:
-
- def test_named_empty_route_with_prefix
- @routes.draw { root :to => 'people#index', :as => 'people' }
- config_default_locale_settings('en', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
- assert_helpers_include :people_en, :people_es, :people
- end
-
- def test_named_root_route_with_prefix
- @routes.draw { root :to => 'people#index' }
- config_default_locale_settings('es', true)
- @route_translator.load_dictionary_from_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
- translate_routes
-
- assert_routing '/', :controller => 'people', :action => 'index'
- assert_recognizes( {:controller => 'people', :action => 'index'}, '/')
- assert_routing '/es', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
- end
-
- def test_named_untranslated_route_with_prefix
- @routes.draw { match 'foo', :to => 'people#index', :as => 'people' }
- config_default_locale_settings('en', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es/foo', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
- assert_helpers_include :people_en, :people_es, :people
- end
-
- def test_named_translated_route_on_default_locale_with_prefix
- @routes.draw { match 'people', :to => 'people#index', :as => 'people' }
- config_default_locale_settings('es', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
- assert_helpers_include :people_en, :people_es, :people
- end
-
- def test_named_translated_route_on_non_default_locale_with_prefix
- @routes.draw { match 'people', :to => 'people#index', :as => 'people' }
- config_default_locale_settings('en', true)
- @route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
- translate_routes
-
- assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
- assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
- assert_helpers_include :people_en, :people_es, :people
- end
-
+
def test_named_translated_route_with_prefix_must_have_locale_as_static_segment
@routes.draw { match 'people', :to => 'people#index', :as => 'people' }
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
@@ -212,121 +93,119 @@ def test_named_translated_route_with_prefix_must_have_locale_as_static_segment
people_es = @routes.routes.select{ |r| r.name == 'people_es' }.first
assert_equal "/es/gente", people_es.to_s.split(' ')[1]
end
-
- # Named routes without prefix on default locale:
-
+
def test_named_empty_route_without_prefix
@routes.draw { root :to => 'people#index', :as => 'people' }
- config_default_locale_settings('es', false)
+ config_default_locale_settings 'es'
@route_translator.yield_dictionary { |t| t['es'] = {}; t['en'] = {'people' => 'gente'}; }
translate_routes
-
+
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '', :controller => 'people', :action => 'index', :locale => 'es'
end
-
+
def test_named_root_route_without_prefix
@routes.draw { root :to => 'people#index' }
- config_default_locale_settings('es', false)
+ config_default_locale_settings 'es'
@route_translator.load_dictionary_from_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
translate_routes
-
+
assert_routing '/', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '/en', :controller => 'people', :action => 'index', :locale => 'en'
assert_unrecognized_route '/es', :controller => 'people', :action => 'index', :locale => 'es'
end
-
+
def test_named_untranslated_route_without_prefix
@routes.draw { match 'foo', :to => 'people#index', :as => 'people' }
- config_default_locale_settings('es', false)
+ config_default_locale_settings 'es'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/en/foo', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing 'foo', :controller => 'people', :action => 'index', :locale => 'es'
assert_helpers_include :people_en, :people_es, :people
end
-
+
def test_named_translated_route_on_default_locale_without_prefix
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
- config_default_locale_settings('es', false)
+ config_default_locale_settings 'es'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing 'gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_helpers_include :people_en, :people_es, :people
end
-
+
def test_named_translated_route_on_non_default_locale_without_prefix
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
@route_translator.yield_dictionary { |t| t['en'] = {}; t['es'] = {'people' => 'gente'} }
translate_routes
-
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_helpers_include :people_en, :people_es, :people
end
-
+
def test_languages_load_from_file
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
@route_translator.load_dictionary_from_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
translate_routes
-
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_helpers_include :people_en, :people_es, :people
end
-
+
def test_languages_load_from_file_without_dictionary_for_default_locale
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
- config_default_locale_settings('fr', false)
+ config_default_locale_settings 'fr'
@route_translator.load_dictionary_from_file File.expand_path('locales/routes.yml', File.dirname(__FILE__))
translate_routes
-
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'fr'
assert_routing '/en/people', :controller => 'people', :action => 'index', :locale => 'en'
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_helpers_include :people_fr, :people_en, :people_es, :people
end
-
+
def test_i18n_based_translations_setting_locales
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
I18n.backend = StubbedI18nBackend
@route_translator.init_i18n_dictionary 'es'
translate_routes
-
+
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
assert_helpers_include :people_en, :people_es, :people
end
-
+
def test_i18n_based_translations_taking_i18n_available_locales
@routes.draw { match 'people', :to => 'people#index', :as => 'people'}
- config_default_locale_settings('en', false)
+ config_default_locale_settings 'en'
I18n.stubs(:available_locales).at_least_once.returns StubbedI18nBackend.available_locales
I18n.backend = StubbedI18nBackend
@route_translator.init_i18n_dictionary
translate_routes
-
+
assert_routing '/fr/people', :controller => 'people', :action => 'index', :locale => 'fr'
assert_routing '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
assert_helpers_include :people_fr, :people_en, :people_es, :people
end
-
+
def test_action_controller_gets_locale_setter
ActionController::Base.instance_methods.include?('set_locale_from_url')
end
-
+
def test_action_controller_gets_locale_suffix_helper
ActionController::Base.instance_methods.include?('locale_suffix')
end
-
+
def test_action_view_gets_locale_suffix_helper
ActionView::Base.instance_methods.include?('locale_suffix')
end

2 comments on commit 27735f8

@jsierles

Why was this setting was removed?

@schiza

It was a setting, so why it's removed? Removing functionality isn't a good choice.. I had to monkey-patch it.

Please sign in to comment.
Something went wrong with that request. Please try again.