routing-filter icon indicating copy to clipboard operation
routing-filter copied to clipboard

Support for case insensitive locales

Open kennyadsl opened this issue 9 years ago • 9 comments

For example it will both support zh-CN and zh-cn segments.

Here's the code we used for a project we are working on:

module CaseInsensitiveLocaleSupport
  # Extract the segment and find its supported version using the locales_map hash
  def extract_segment!(*)
    locale = super
    CaseInsensitiveLocaleSupport.locales_map.fetch(locale.downcase, locale).to_s if locale
  end

  # Setup a map for downcased locales
  def self.locales_map
    @locales_map ||= RoutingFilter::Locale.locales.map { |locale| [locale.to_s.downcase, locale]}.to_h
  end
end

class RoutingFilter::Locale
  # Update the regexp to ignore case
  @@locales_pattern = Regexp.new(locales_pattern.source, Regexp::IGNORECASE)

  prepend CaseInsensitiveLocaleSupport
end

As you can see it uses an additional hash that maps downcased locales symbols with their original versions. This will allow to fetch the right locale after extracting the URL segment.

If you think this could be useful we can provide a PR to include this support.

/cc @elia @masterkain

kennyadsl avatar Dec 16 '15 11:12 kennyadsl

Hi, this leads to duplicate content, IMHO - a bad idea. Take this article for example: http://www.hobo-web.co.uk/duplicate-content-problems/

rubyconvict avatar Dec 23 '15 09:12 rubyconvict

@rubyconvict you are totally right! I didn't considered this aspect. Do you have any other idea about how to implement this? Or do you think it's not doable at all?

kennyadsl avatar Dec 28 '15 15:12 kennyadsl

What's the usecase for this?

simi avatar Dec 28 '15 16:12 simi

@simi users could type website paths using both locale segments:

  • website.com/zh-CN
  • website.com/zh-cn

The last one (zh-cn), as it is now (or as I think it is :smile:), will not be accepted as valid because it's not listed into I18n.available_locales.

We are looking for a valid solution to accept both versions setting the current locale to zh-CN, if possible.

kennyadsl avatar Dec 28 '15 16:12 kennyadsl

Just redirect from website.com/zh-cn/* to zh-CN/*.

simi avatar Dec 28 '15 17:12 simi

Well, redirecting was what I was trying to avoid since zh-CN is not the only possible locale with this issue and, being needed on a multidomain platform on which each domain can have multiple locales, it would require more maintenance work per domain/locale. I was trying to figure out if this can be made dynamic. Thanks anyway!

kennyadsl avatar Dec 28 '15 17:12 kennyadsl

@simi p.s sorry for not pointing that out in the too simplified usecase I've presented!

kennyadsl avatar Dec 28 '15 17:12 kennyadsl

301 redirect is the way to go for SEO, if locele is not in case sensitive I18n.available_locales, I would check downcased version against downcased array of locales and redirect based on mapping from downcased match, eg. {"zh-cn" => "zh-CN"}. Indeed, this would be cool opt-in feature for this gem.

rubyconvict avatar Dec 29 '15 07:12 rubyconvict

@rubyconvict yes, that would be great and solve our usecase. Just wondering if and how is possible to make redirects from inside a routing filter. I can't see anything like that in other provided filters.

kennyadsl avatar Dec 29 '15 09:12 kennyadsl