enriclluelles / route_translator

Translate your rails app route to various languages without the hassle
MIT License
876 stars 143 forks source link

Routing spec for Engine #173

Open wafendy opened 7 years ago

wafendy commented 7 years ago

Hi,

I'm using route_translator v5.5.0 gem and running the following issue. I hope someone can clarify why it's behaving this way.

#app/config/routes.rb
App::Engine.routes.draw do

  #This is to hide the extra translation /:locale/engine/:locale/path
  RouteTranslator.config { |config| config.hide_locale = true }

  localized do
    root to: "pages#index"
  end
end
#app/spec/dummy_app/config/routes.rb
Rails.application.routes.draw do
  localized do
    mount App::Engine => "/app"
  end
end
#spec/dummy_app/config/application.rb
module DummyApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    I18n.available_locales = [:en, :de]
    I18n.default_locale = :en
  end
end
           Prefix Verb URI Pattern        Controller#Action
app_de                   /de/app              App::Engine {:locale=>"de"}
app_en                   /app                 App::Engine {:locale=>"en"}

Routes for App::Engine:
           root_de GET    /                   app/pages#landing {:locale=>"de"}
           root_en GET    /                   app/pages#landing {:locale=>"en"}
#spec/routing/app_engine_routes_spec.rb
describe 'routes for App engine' do
  routes { App::Engine.routes }
  it 'routes / to the index page' do
    expect(get: '/').to route_to(
      controller: "app/pages",
      action: "index",
      locale: 'en',
    )
  end
end

Since I set my default_locale, I would expect it returns en, but it returns de as locale instead. Any idea how to do proper spec in this scenario?

tagliala commented 7 years ago

which version of Rails?

wafendy commented 7 years ago

Rails version Rails 5.1.4

tagliala commented 7 years ago

Could you please give a try with 5.1.2?

wafendy commented 7 years ago

Ok, I just did, i'm still getting the same behavior. Just in case if you missed, I did set RouteTranslator.config { |config| config.hide_locale = true } at the engine's route so I won't see the language twice in the URL.

By doing this, I'm getting

Routes for App::Engine:
           root_de GET    /                   app/pages#landing {:locale=>"de"}
           root_en GET    /                   app/pages#landing {:locale=>"en"}

So if I go directly to root path, then it will pick up the first one in the list (I assume).

tagliala commented 7 years ago

Sorry, I'm not using this gem with engines, so I don't know what to expect and what the actual behavior is. I thought it could be related to #172, so I've asked your rails version

Also, I don't know how to fix this issue.

Given that we cannot have two paths with two different languages, could you please provide:

  1. what you are trying to do
  2. what you are expecting from route_translator
  3. what is the actual behaviour of route_translator

?

tagliala commented 7 years ago

/cc @rvasquez-flip4new (author of the engine support feature)

wafendy commented 7 years ago

I want to use route translator on both the main app and my engine. But i'm getting the following path with my route helpers: /en/engine/en/index

The language is being displayed twice

tagliala commented 7 years ago

What does happen if you remove the localized block inside the engine routes?

wafendy commented 7 years ago

If you remove localized block inside the engine routes, then all routes from engine don't have any locale information and are not translated.

tagliala commented 7 years ago

Thanks for the information.

As I mentioned before, I'm not so much into Rails Engines and I don't know how they behave and how route_translator should deal with them.

I would like a summary like this:

Without route translator

routes.rb

# Your routes here

Engine routes

# Your engine routes here

Result of rails routes

              Prefix Verb   URI Pattern                     Controller#Action
                 ... ...    ...

With route translator

routes.rb

# Your routes here

route_translator.rb initialzier

# Your initializer here

Engine routes

# Your engine routes here

Expected result of rails routes

              Prefix Verb   URI Pattern                     Controller#Action
                 ... ...    ...

Actual result of rails routes

              Prefix Verb   URI Pattern                     Controller#Action
                 ... ...    ...
wafendy commented 7 years ago

Without route translator

git repo https://github.com/wafendy/blorgh

routes.yml

en:
  routes:
    articles: articles
    magazines: magazines
id:
  routes:
    articles: artikel
    magazines: majalah

routes.rb

Rails.application.routes.draw do
  localized do
    resources :articles, only: [:index]
    mount Blorgh::Engine => "/blorgh"
  end
end

Engine routes

Blorgh::Engine.routes.draw do
  resources :magazines, only: [:index]
end

Result of rails routes

     Prefix Verb URI Pattern            Controller#Action
articles_id GET  /id/artikel(.:format)  articles#index {:locale=>"id"}
articles_en GET  /en/articles(.:format) articles#index {:locale=>"en"}
  blorgh_id      /id/blorgh             Blorgh::Engine {:locale=>"id"}
  blorgh_en      /en/blorgh             Blorgh::Engine {:locale=>"en"}

Routes for Blorgh::Engine:
magazines GET  /magazines(.:format) blorgh/magazines#index

Would it be possible to automagically include route translator at Engine level when Rails Engine is mounted inside localized block?

Actual Result:

  http://localhost:3000/id/blorgh/magazines

Expected Result:

  http://localhost:3000/id/blorgh/majalah

With route translator

routes.rb

Rails.application.routes.draw do
  localized do
    resources :articles, only: [:index]
    mount Blorgh::Engine => "/blorgh"
  end
end

route_translator.rb initialzier

RouteTranslator.config do |config|
  config.force_locale = true
end

Engine routes

Blorgh::Engine.routes.draw do
  localized do
    resources :magazines, only: [:index]
  end
end

Result of rails routes

     Prefix Verb URI Pattern            Controller#Action
articles_id GET  /id/artikel(.:format)  articles#index {:locale=>"id"}
articles_en GET  /en/articles(.:format) articles#index {:locale=>"en"}
  blorgh_id      /id/blorgh             Blorgh::Engine {:locale=>"id"}
  blorgh_en      /en/blorgh             Blorgh::Engine {:locale=>"en"}

Routes for Blorgh::Engine:
magazines_id GET  /id/majalah(.:format)   blorgh/magazines#index {:locale=>"id"}
magazines_en GET  /en/magazines(.:format) blorgh/magazines#index {:locale=>"en"}
tagliala commented 7 years ago

So, as far as I understand, if you use route_translator inside your engine and your engine is itself inside a localized block, then:

Actual result

     Prefix Verb URI Pattern            Controller#Action
articles_id GET  /id/artikel(.:format)  articles#index {:locale=>"id"}
articles_en GET  /en/articles(.:format) articles#index {:locale=>"en"}
  blorgh_id      /id/blorgh             Blorgh::Engine {:locale=>"id"}
  blorgh_en      /en/blorgh             Blorgh::Engine {:locale=>"en"}

Routes for Blorgh::Engine:
magazines_id GET  /id/majalah(.:format)   blorgh/magazines#index {:locale=>"id"}
magazines_en GET  /en/magazines(.:format) blorgh/magazines#index {:locale=>"en"}

Expected result

     Prefix Verb URI Pattern            Controller#Action
articles_id GET  /id/artikel(.:format)  articles#index {:locale=>"id"}
articles_en GET  /en/articles(.:format) articles#index {:locale=>"en"}
  blorgh_id      /id/blorgh             Blorgh::Engine {:locale=>"id"}
  blorgh_en      /en/blorgh             Blorgh::Engine {:locale=>"en"}

Routes for Blorgh::Engine:
magazines_id GET  /majalah(.:format)   blorgh/magazines#index {:locale=>"id"}
magazines_en GET  /magazines(.:format) blorgh/magazines#index {:locale=>"en"}

Am I right?

wafendy commented 7 years ago

Yup, that's correct.