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

Avoid duplicate routes in host locales #171

Closed
tagliala opened this issue Jul 23, 2017 · 9 comments
Closed

Avoid duplicate routes in host locales #171

tagliala opened this issue Jul 23, 2017 · 9 comments
Assignees
Milestone

Comments

@tagliala
Copy link
Collaborator

tagliala commented Jul 23, 2017

Hi, please give a try to the new version of the gem, I've just released an alpha version.

Breaking changes are related to the Host-based Locale feature and Engine support:

In other words, you can use host_locales if you don't have a main domain like www.example.com and your subdomains have one language for each one it.example.com, es.example.com.

If you have special needs (eg: main domain with all languages, subdomains with separate languages) please go for a custom solution.

The whole native_path stuff has been removed, each localized route only work for its own domain.

Please use this thread to report issues and feedbacks


Having multiple languages for the same subdomain is out of the Route Translator's scope.

Anyway, there is a simple workaround to restore this feautre: #171 (comment)

@tagliala
Copy link
Collaborator Author

Is anybody using this version?

@josefsj3m
Copy link

Hi tagliala,

I am using verson 5.5 to implement a custom locale identification, first based on url, if it is absent based on cookie, if it is absent based on browser language and based on domain at last.

The rails app is serving two domains, domain.com and domain.com.br, and the locales en, pt-br and es. I've tried to implement the two domains with all languages using host_locales, but I couldn't do it because when I set I18n.default_locale = :'pt-BR' the gem doesn't gererate the route domain.com/pt-BR/something, on the other hand, setting I18n.default_locale = :en there is no route like domain.com.br/en/something. I think it is strange an url like this domain.com/empresas or domain.com.br/companies.

There is no problem with es locale because the host_locales doesn't have an entry *.es, so the gem generates the routes domain.com/es/empresas and dmain.com.br/es/empresas. Is it suppose to be the right behavior ? Or I am doing something wrong.

I can't upgrade to 6.0 because it has a completely different approach and will break my code. Is it possible to maint the current behavior in 6.0 ?

Regards,
Jose Fernando

@tagliala
Copy link
Collaborator Author

Is it possible to maint the current behavior in 6.0 ?

Unfortunately the whole point of the 6.0 is that breaking change

I would like to fix #87 and simplify the host locales feature for the basic use case (one domain per language).

If there is a way to save locales without host, a PR would be appreciated

Anyway, I could decide to not release 6.0 at all 🤷‍♂️

@josefsj3m
Copy link

I guess the main point here is duplicated content and how to avoid it. Reading some google pages, Multi-regional and multilingual sites (https://support.google.com/webmasters/answer/182192?hl=en#3) and Use hreflang for language and regional URLs (https://support.google.com/webmasters/answer/189077) , I think we can have one domain and multiple languages since we provide an hreflang in the head with the prefered alternate language. This approach 5.5 version already has.

So, one domain per language sure avoid content duplication, but forcing it when using host_locales I think limit to one use case. Please correct me if I am wrong, but using verify_host_path_consistency = true in version 5.5 avoid content duplication.

In my use case, two domains and multiple languages, I am writing a custom code right now because a get the locale not only from host, take a look at this and, please, feel free to comment. I am skipping :set_locale_from_url and using do_check_locale instead:

def do_check_locale(&block)
    new_loc = I18n.default_locale
    loc = locale_in_url?
    if loc.nil?
      if valid_language_cookie?
        # Language cookie is valid, use it.
        new_loc = cookies[:eol_language].to_s.to_sym
      else
        # There is no valid language cookie try the browser language
        brw_loc = locale_from_browser
        if !brw_loc.nil?
          # Use the browser locale. 
          new_loc = brw_loc
        else
          # Could not get the browser language use the locale from domain
          loc_domain = current_site.domains_to_locale[request.domain.to_sym]
          unless loc_domain.nil?
            # Use locale assigned to the domain
            new_loc = loc_domain
          end
        end
      end
    else
      # The url contains the locale, use it.
      new_loc = loc
    end
  rescue => e
    # Avoid the request crash, log the error and ensure the I18n.with_locale
    loc_error = ChangeLocaleError.new("Error: Module: ApplicationHelper, Method: do_check_locale, Msg: #{e.message}", e)
    log_and_notify_error(loc_error)
  ensure
     # Locale changes only apply to the current request by saving its original values.
     original_default = I18n.default_locale
     original_locale = I18n.locale
     I18n.default_locale = new_loc
     I18n.locale = new_loc
     I18n.with_locale new_loc, &block
     I18n.default_locale = original_default
     I18n.locale = original_locale
  end

Regards,

@tagliala
Copy link
Collaborator Author

Use hreflang for language and regional URLs

This is a good workaround and that is what I was using to avoid penalties

Please correct me if I am wrong, but using verify_host_path_consistency = true in version 5.5 avoid content duplication.

Not entirely. It does not avoid both /route and /en/route routes on the same domain, and that leads to content duplication

Using the current behavior for 6.0 would be possible by customizing the locale_from_host section.

@tagliala
Copy link
Collaborator Author

tagliala commented Aug 22, 2017

E.g.:

/config/initializers/route_translator.rb

RouteTranslator.config do |config|
  # Do not set config.host_locales here, but  you may want to hide or force locale
end

/config/application.rb

module MyApplication
  class Application < Rails::Application

    # ...

    config.domain_language = {
      'it.lvh.me' => :it,
      'en.lvh.me' => :en,
      'de.lvh.me' => :de
    }

    # ...

  end
end

/app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  # ...

  private

  # Override Route Translator's method.
  # Note that you may want some error management (eg: fallback on a default language)
  def set_locale_from_url
    I18n.with_locale(Rails.application.config.domain_language[request.host]) do
      yield
    end
  end

  # ...

end

This will allow to use multiple languages for each host, with all the duplication.

@josefsj3m
Copy link

Thanks,

I'll use custom solution with hreflang and redirects 301.

@tagliala
Copy link
Collaborator Author

@josefsj3m you're welcome. But it is not sure I'm going to release 6.0 at all. Things could change

@josefsj3m
Copy link

Yes, I know. What I am trying to do is a custom solution that is compliant with your development. I don't want to develop a route_translator from the ground, I have no time to do that.
I am working on that, and when I am acomplish it I will share the code.
Thanks a lot.

@tagliala tagliala added this to the 6.0.0 milestone Dec 3, 2017
@tagliala tagliala changed the title Route Translator 6.0 Avoid duplicate routes in host locales Dec 3, 2017
@tagliala tagliala self-assigned this Dec 3, 2017
tagliala added a commit that referenced this issue Dec 3, 2017
tagliala added a commit that referenced this issue Dec 3, 2017
tagliala added a commit that referenced this issue Dec 3, 2017
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fix: #87

Refers to: #171
tagliala added a commit that referenced this issue Dec 3, 2017
tagliala added a commit that referenced this issue Dec 3, 2017
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fix: #87

Close: #171
tagliala added a commit that referenced this issue Dec 3, 2017
tagliala added a commit that referenced this issue Dec 3, 2017
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fix: #87, #171
tagliala added a commit that referenced this issue Dec 3, 2017
tagliala added a commit that referenced this issue Dec 3, 2017
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Feb 5, 2018
tagliala added a commit that referenced this issue Feb 5, 2018
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Apr 9, 2018
tagliala added a commit that referenced this issue Apr 9, 2018
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Apr 10, 2018
tagliala added a commit that referenced this issue Oct 3, 2018
tagliala added a commit that referenced this issue Oct 3, 2018
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Jan 19, 2019
tagliala added a commit that referenced this issue Jan 19, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Mar 14, 2019
tagliala added a commit that referenced this issue Mar 14, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue May 16, 2019
tagliala added a commit that referenced this issue May 16, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue May 16, 2019
tagliala added a commit that referenced this issue May 16, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
@tagliala tagliala removed this from the 7.0.0 milestone Jul 20, 2019
tagliala added a commit that referenced this issue Dec 26, 2019
tagliala added a commit that referenced this issue Dec 26, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Dec 26, 2019
tagliala added a commit that referenced this issue Dec 26, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Dec 26, 2019
tagliala added a commit that referenced this issue Dec 26, 2019
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
tagliala added a commit that referenced this issue Apr 17, 2020
tagliala added a commit that referenced this issue Apr 17, 2020
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
AsgharRaz pushed a commit to AsgharRaz/route_translator that referenced this issue Nov 3, 2020
AsgharRaz pushed a commit to AsgharRaz/route_translator that referenced this issue Nov 3, 2020
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes enriclluelles#87 and fixes enriclluelles#171
tagliala added a commit that referenced this issue Jan 13, 2021
tagliala added a commit that referenced this issue Jan 13, 2021
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
@tagliala tagliala added this to the 10.0 milestone Jan 15, 2021
tagliala added a commit that referenced this issue Jan 16, 2021
The purpose of this refactor is to avoid duplicate routes.

Previously, route_translator allowed duplicate routes `/cars` and
`/en/cars` under the `en` domain when `host_locales` was set.

Fixes #87 and fixes #171
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