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

Use of an *optional* locale scope leads to missing locale in URLs #208

Open
john-999 opened this issue Mar 27, 2020 · 14 comments
Open

Use of an *optional* locale scope leads to missing locale in URLs #208

john-999 opened this issue Mar 27, 2020 · 14 comments

Comments

@john-999
Copy link

john-999 commented Mar 27, 2020

When I use an optional locale scope - meaning with parenthesis like below - with this gem, then all URLs resulting from routes instructions inside "localized do" will not include the locale any longer:

scope '(:locale)' do
  localized do
    resources :books
  end
end

...will not result in /en/books but only in /books.

The locale is included correctly in the URL only if the local scope is not optional (not in parenthesis):

scope ':locale' do

This means a deviation from Rails' default behavior.

@tagliala
Copy link
Collaborator

Hi!

Thanks for being part of the Route Translator Community

I didn't get the use case, could you please clarify what you are trying to achieve?

@john-999
Copy link
Author

The goal is to use this gem, but together with an optional "locale scope" like in the 1st example. But when I do this, Rails will generate links that don't contain the locale any longer, i.e. /books, whereas Rails would normally generate links like /en/books (when I do not use this gem).

With this gem, it seems I can only use a mandatory "locale scope" like in the 2nd example, in order to have Rails generate links that do contain the locale correctly, i.e.: /en/books

@tagliala
Copy link
Collaborator

Sorry, could you please provide an example of the routes that you want to generate?

@john-999
Copy link
Author

john-999 commented Mar 27, 2020

In this first case below (without this gem), Rails will show <%= link_to 'Books', books_path %> correctly as /en/books:

Rails.application.routes.draw do

  scope '(:locale)' do
    # localized do
      resources :books
    # end
  end

end

But in this second case below (with this gem), Rails will show <%= link_to 'Books', books_path %> incorrectly as /books.

Rails.application.routes.draw do

  scope '(:locale)' do
    localized do
      resources :books
    end
  end

end

(And I would like to have all URLs like the first example, like /en/books, but WITH this gem.)

@tagliala
Copy link
Collaborator

tagliala commented Mar 27, 2020

(And I would like to have all URLs like the first example, like /en/books, but WITH this gem.)

Thanks for the many clarifications 🙏

You are looking for the force_locale option: https://github.com/enriclluelles/route_translator#available-configurations

Let me know if that helps

@john-999 john-999 reopened this Mar 28, 2020
@john-999
Copy link
Author

john-999 commented Mar 28, 2020

Unfortunately, when I follow your instruction while keeping the above mentioned routes configuration...

Rails.application.routes.draw do

  scope '(:locale)' do
    localized do
      resources :books
    end
  end

end

... then the command rails routes will output a strange format for the routes such as...

/en(/:locale)/books(.:format)

... where the correct format would be:

(/:locale)/books(.:format)

@tagliala
Copy link
Collaborator

Hi @john-999 , please do not use the scope, just leave the localized block

@john-999
Copy link
Author

Ok, so if I understand correctly, then the format (note the parenthesis around /:locale)...

(/:locale)/books(.:format)

... is not possible with this gem.

If this is correct, would you consider it worth implementing this option?

@tagliala
Copy link
Collaborator

tagliala commented Mar 28, 2020

If this is correct, would you consider it worth implementing this option?

Thanks for your patience, I need to understand the use case and why this feature is needed.

What you are trying to achieve? What you achieve instead? Look in example at this issue: #205

You can see expected behavior and wanted behavior, I have to understand that

(And I would like to have all URLs like the first example, like /en/books, but WITH this gem.)

If you want all localized routes have its own locale, including the default one, then the option to use is force_locale

Example:

Rails.application.routes.draw do
  localized do
    resources :products, only: %i[index show]
  end
end

Output:

$ rails routes
                   Prefix Verb URI Pattern                                                                              Controller#Action
              products_it GET  /it/prodotti(.:format)                                                                   products#index {:locale=>"it"}
              products_en GET  /products(.:format)                                                                      products#index {:locale=>"en"}
               product_it GET  /it/prodotti/:id(.:format)                                                               products#show {:locale=>"it"}
               product_en GET  /products/:id(.:format)                                                                  products#show {:locale=>"en"}

If you want /en/products instead of /products, the initializer should look like

# config/initializers/route_translator.rb

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

Output:

$ rails routes
                   Prefix Verb URI Pattern                                                                              Controller#Action
              products_it GET  /it/prodotti(.:format)                                                                   products#index {:locale=>"it"}
              products_en GET  /en/products(.:format)                                                                   products#index {:locale=>"en"}
               product_it GET  /it/prodotti/:id(.:format)                                                               products#show {:locale=>"it"}
               product_en GET  /en/products/:id(.:format)                                                               products#show {:locale=>"en"}

@john-999
Copy link
Author

The idea would be to have routes where the /locale/ segment is optional (the application would route correctly, with or without the locale segment in the URL):

Route 1 == Route 2 (identical routes):

/it/prodotti(.:format)
/prodotti(.:format)

Route 3 == Route 4 (identical routes):

/en/products(.:format)  
/products(.:format)

I was expecting to achieve this result by the following (but as stated above, it does not seem to work that way):

Rails.application.routes.draw do

  scope '(:locale)' do
    localized do
      resources :products
    end
  end

end

(But maybe this behavior is too much of an edge/perfectionist case...)

@tagliala
Copy link
Collaborator

@john-999 ok, I've got it :)

You are looking for hide_locale :)

hide_locale is useful when you have language subdomains

# config/initializers/route_translator.rb

RouteTranslator.config do |config|
  config.hide_locale = true
end
$ rails routes
                   Prefix Verb URI Pattern                                                                              Controller#Action
              products_it GET  /prodotti(.:format)                                                                      products#index {:locale=>"it"}
              products_en GET  /products(.:format)                                                                      products#index {:locale=>"en"}
               product_it GET  /prodotti/:id(.:format)                                                                  products#show {:locale=>"it"}
               product_en GET  /products/:id(.:format)                                                                  products#show {:locale=>"en"}

Hope it helps

Closing here

@tagliala tagliala reopened this Mar 28, 2020
@tagliala
Copy link
Collaborator

tagliala commented Mar 28, 2020

No, sorry, you want both

@tagliala
Copy link
Collaborator

tagliala commented Mar 28, 2020

Does

# config/initializers/route_translator.rb

RouteTranslator.config do |config|
  config.hide_locale = true
end
# config/routes.rb

Rails.application.routes.draw do
  localized do
    resources :products, only: %i[index show new]
  end

  scope(':locale') do
    localized do
      resources :products, only: %i[index show new]
    end
  end
end
$ rails routes
                   Prefix Verb URI Pattern                                                                              Controller#Action
              products_it GET  /prodotti(.:format)                                                                      products#index {:locale=>"it"}
              products_en GET  /products(.:format)                                                                      products#index {:locale=>"en"}
           new_product_it GET  /prodotti/new(.:format)                                                                  products#new {:locale=>"it"}
           new_product_en GET  /products/new(.:format)                                                                  products#new {:locale=>"en"}
               product_it GET  /prodotti/:id(.:format)                                                                  products#show {:locale=>"it"}
               product_en GET  /products/:id(.:format)                                                                  products#show {:locale=>"en"}
                          GET  /:locale/prodotti(.:format)                                                              products#index {:locale=>"it"}
                          GET  /:locale/products(.:format)                                                              products#index {:locale=>"en"}
                          GET  /:locale/prodotti/new(.:format)                                                          products#new {:locale=>"it"}
                          GET  /:locale/products/new(.:format)                                                          products#new {:locale=>"en"}
                          GET  /:locale/prodotti/:id(.:format)                                                          products#show {:locale=>"it"}
                          GET  /:locale/products/:id(.:format)                                                          products#show {:locale=>"en"}

work for your use case?

In any case, this is something that I would not like to officially support

@john-999
Copy link
Author

Yes, thank you for your help - that result was indeed the idea.

But I do of course understand if this goes beyond the idea of this gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants