diff options
author | Seb Bacon <seb.bacon@gmail.com> | 2011-07-15 10:44:38 +0100 |
---|---|---|
committer | Seb Bacon <seb.bacon@gmail.com> | 2011-07-15 10:44:38 +0100 |
commit | 183b5be4f8e2e7396a32c6d15f6fb5c085ba0889 (patch) | |
tree | 8ab399c250799a8c337aa596e8aeebfd08d9a95f | |
parent | 62b006d959caf95870f9215e56fb3594c585a259 (diff) |
Only prepend locales to paths when there's more than one locale to choose from.
Replaces core "locale" filter with customised one. Fixes #82
-rw-r--r-- | config/environment.rb | 4 | ||||
-rw-r--r-- | config/routes.rb | 3 | ||||
-rw-r--r-- | lib/routing_filters.rb | 9 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 23 |
4 files changed, 37 insertions, 2 deletions
diff --git a/config/environment.rb b/config/environment.rb index 86c66ad18..ec6a4096f 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -82,6 +82,10 @@ Rails::Initializer.run do |config| # Make Active Record use UTC-base instead of local time config.active_record.default_timezone = :utc + config.after_initialize do + require 'routing_filters.rb' + end + # See Rails::Configuration for more options ENV['RECAPTCHA_PUBLIC_KEY'] = MySociety::Config::get("RECAPTCHA_PUBLIC_KEY", 'x'); ENV['RECAPTCHA_PRIVATE_KEY'] = MySociety::Config::get("RECAPTCHA_PRIVATE_KEY", 'x'); diff --git a/config/routes.rb b/config/routes.rb index 175a37a82..9765b26b8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -195,8 +195,7 @@ ActionController::Routing::Routes.draw do |map| rule.admin_rule_update '/admin/censor/update', :action => 'update' rule.admin_rule_destroy '/admin/censor/destroy/:censor_rule_id', :action => 'destroy' end - map.filter('locale') - + map.filter('conditionallyprependlocale') # Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' diff --git a/lib/routing_filters.rb b/lib/routing_filters.rb new file mode 100644 index 000000000..cdb58e7c1 --- /dev/null +++ b/lib/routing_filters.rb @@ -0,0 +1,9 @@ +module RoutingFilter + class Conditionallyprependlocale < RoutingFilter::Locale + # Override core Locale filter not to prepend locale path segment + # when there's only one locale + def prepend_locale?(locale) + locale && I18n.available_locales.length > 1 && (self.class.include_default_locale? || !default_locale?(locale)) + end + end +end diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 6a48f142a..7807a5541 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -22,6 +22,29 @@ describe GeneralController, "when searching" do response.should redirect_to(:action => 'search', :combined => "mouse") # URL /search/:query end + describe "when using different locale settings" do + home_link_regex = /href=".*\/en"/ + it "should generate URLs with a locale prepended when there's more than one locale set" do + ActionController::Routing::Routes.add_filters(['conditionallyprependlocale']) + get :frontpage + response.should have_text(home_link_regex) + end + + it "should generate URLs without a locale prepended when there's only one locale set" do + ActionController::Routing::Routes.add_filters(['conditionallyprependlocale']) + old_available_locales = FastGettext.default_available_locales + available_locales = ['en'] + FastGettext.default_available_locales = available_locales + I18n.available_locales = available_locales + + get :frontpage + response.should_not have_text(home_link_regex) + + FastGettext.default_available_locales = old_available_locales + I18n.available_locales = old_available_locales + end + end + describe 'when using xapian search' do # rebuild xapian index after fixtures loaded |