From 61285542c35aae4c0a2b65951ff0b30a53c78036 Mon Sep 17 00:00:00 2001 From: Matt vleming Date: Mon, 20 Apr 2015 01:01:01 +0000 Subject: [PATCH] Allow disabling i18n fallback to not have unecessary routes --- README.md | 5 +++++ lib/route_translator.rb | 3 ++- lib/route_translator/translator.rb | 15 +++++++++++++-- test/routing_test.rb | 16 ++++++++++++++++ test/support/configuration_helper.rb | 4 ++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b2493a67..05742b6e 100755 --- a/README.md +++ b/README.md @@ -170,6 +170,11 @@ end Optional hash to set `I18n.default_locale` based on `request.host`. Useful for apps accepting requests from more than one domain. See below for more details. +* **disable_fallback** + Set this option to `true` to create only the routes for each locale that have translations. + For example if we have `/examples` and a translation is not provided for ES, a route helper of `examples_es` will not be created. + Defaults to `false`. + Useful when one uses this with a locale route constraint, so non-ES routes can 404 on a Spanish website. ### Host-based Locale diff --git a/lib/route_translator.rb b/lib/route_translator.rb index f64232f7..bb0da664 100644 --- a/lib/route_translator.rb +++ b/lib/route_translator.rb @@ -12,7 +12,7 @@ module RouteTranslator Configuration = Struct.new(:force_locale, :hide_locale, :generate_unlocalized_routes, :locale_param_key, :generate_unnamed_unlocalized_routes, :available_locales, - :host_locales) + :host_locales, :disable_fallback) def self.config(&block) @config ||= Configuration.new @@ -23,6 +23,7 @@ def self.config(&block) @config.generate_unnamed_unlocalized_routes ||= false @config.host_locales ||= ActiveSupport::OrderedHash.new @config.available_locales ||= nil + @config.disable_fallback ||= false yield @config if block resolve_config_conflicts @config diff --git a/lib/route_translator/translator.rb b/lib/route_translator/translator.rb index 5853a1fc..41758615 100644 --- a/lib/route_translator/translator.rb +++ b/lib/route_translator/translator.rb @@ -37,7 +37,12 @@ def self.translations_for(app, conditions, requirements, defaults, route_name, a available_locales.each do |locale| new_conditions = conditions.dup - new_conditions[:path_info] = translate_path(conditions[:path_info], locale) + begin + new_conditions[:path_info] = translate_path(conditions[:path_info], locale) + rescue I18n::MissingTranslationData => e + raise e unless RouteTranslator.config.disable_fallback + next + end new_conditions[:parsed_path_info] = ActionDispatch::Journey::Parser.new.parse(new_conditions[:path_info]) if conditions[:parsed_path_info] if new_conditions[:required_defaults] && !new_conditions[:required_defaults].include?(RouteTranslator.locale_param_key) new_conditions[:required_defaults] << RouteTranslator.locale_param_key @@ -121,7 +126,13 @@ def self.translate_path_segment(segment, locale) def self.translate_string(str, locale) locale = "#{locale}".gsub('native_', '') - res = I18n.translate(str, :scope => :routes, :locale => locale, :default => str) + opts = {:scope => :routes, :locale => locale} + if RouteTranslator.config.disable_fallback && locale.to_s != I18n.default_locale.to_s + opts[:fallback] = true + else + opts[:default] = str + end + res = I18n.translate(str, opts) URI.escape(res) end diff --git a/test/routing_test.rb b/test/routing_test.rb index caff5d85..9a3ad3e4 100755 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -570,6 +570,22 @@ def test_config_available_locales_handles_strings config_available_locales nil end + def test_disable_fallback_does_not_draw_non_default_routes + config_disable_fallback(true) + + draw_routes do + localized do + get 'tr_param', :to => 'people#index', :as => 'people' + end + end + + config_disable_fallback(false) + + assert_routing '/tr_param', :controller => 'people', :action => 'index', :locale => 'en' + assert_routing '/es/tr_parametro', :controller => 'people', :action => 'index', :locale => 'es' + assert_unrecognized_route '/ru/tr_param', :controller => 'people', :action => 'index', :locale => 'ru' + end + end class ProductsControllerTest < ActionController::TestCase diff --git a/test/support/configuration_helper.rb b/test/support/configuration_helper.rb index 1c3e22f8..035d3898 100644 --- a/test/support/configuration_helper.rb +++ b/test/support/configuration_helper.rb @@ -43,6 +43,10 @@ def config_available_locales(arr) RouteTranslator.config.available_locales = arr end + def config_disable_fallback(boolean) + RouteTranslator.config.disable_fallback = boolean + end + def host_locales_config_hash if RUBY_VERSION < '1.9' ::ActiveSupport::OrderedHash.new