Skip to content

Commit

Permalink
Allow disabling i18n fallback to not have unecessary routes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpxc8102 authored and Matt Vleming committed Apr 21, 2015
1 parent e7ab9bb commit 6128554
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/route_translator.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 13 additions & 2 deletions lib/route_translator/translator.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions test/routing_test.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/support/configuration_helper.rb
Expand Up @@ -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
Expand Down

0 comments on commit 6128554

Please sign in to comment.