Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to specify nested translations #124

Closed
wants to merge 14 commits into from
10 changes: 8 additions & 2 deletions lib/route_translator/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def self.add_untranslated_helpers_to_controllers_and_views(old_name, named_route

def self.translations_for(app, conditions, requirements, defaults, route_name, anchor, route_set, &block)
add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes)
@scope = [:routes, :controllers].concat defaults[:controller].split('/').map(&:to_sym)

available_locales.each do |locale|
new_conditions = conditions.dup
Expand Down Expand Up @@ -132,10 +133,15 @@ def self.translate_string(str, locale)
opts = { scope: :routes, locale: locale }
if RouteTranslator.config.disable_fallback && locale.to_s != I18n.default_locale.to_s
opts[:fallback] = true
end
if I18n.exists?([@scope, str].join('.'), opts[:locale]) && !I18n.translate(str, opts.merge(scope: @scope)).is_a?(Hash)
res = I18n.translate(str, opts.merge(scope: @scope))
elsif I18n.exists?(str, opts[:locale])
res = I18n.translate(str, opts)
else
opts[:default] = str
opts[:default] = str unless opts[:fallback]
res = I18n.translate(str, opts)
end
res = I18n.translate(str, opts)
URI.escape(res)
end

Expand Down
10 changes: 10 additions & 0 deletions test/locales/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ es:
people: gente
products: productos
tr_param: tr_parametro
favourites: favoritos
blank: ""
controllers:
people:
products:
favourites: fans

ru:
routes:
people: люди
favourites: избранное
controllers:
people:
products:
favourites: кандидаты

en:
m: m #Adding this here just to have en in I18n.available_locales
20 changes: 20 additions & 0 deletions test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class PeopleController < ActionController::Base
class ProductsController < ActionController::Base
end

module People
class ProductsController < ActionController::Base
end
end

class TranslateRoutesTest < ActionController::TestCase
include ActionDispatch::Assertions::RoutingAssertions
include RouteTranslator::AssertionHelper
Expand Down Expand Up @@ -101,6 +106,21 @@ def test_resources
assert_routing({ path: '/productos/1', method: 'PUT' }, controller: 'products', action: 'update', id: '1', locale: 'es')
end

def test_deep_translation
config_default_locale_settings 'es'

draw_routes do
localized do
get 'people/favourites', to: 'people/products#favourites'
get 'favourites', to: 'products#favourites'
end
end
assert_routing '/gente/fans', controller: 'people/products', action: 'favourites', locale: 'es'
assert_routing '/favoritos', controller: 'products', action: 'favourites', locale: 'es'
assert_routing URI.escape('/ru/люди/кандидаты'), controller: 'people/products', action: 'favourites', locale: 'ru'
assert_routing URI.escape('/ru/избранное'), controller: 'products', action: 'favourites', locale: 'ru'
end

def test_utf8_characters
draw_routes do
localized do
Expand Down