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

Support for case insensitive locales #58

Open
kennyadsl opened this issue Dec 16, 2015 · 9 comments
Open

Support for case insensitive locales #58

kennyadsl opened this issue Dec 16, 2015 · 9 comments
Assignees

Comments

@kennyadsl
Copy link

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

@simi simi self-assigned this Dec 16, 2015
@rubyconvict
Copy link

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

@kennyadsl
Copy link
Author

@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?

@simi
Copy link
Collaborator

simi commented Dec 28, 2015

What's the usecase for this?

@kennyadsl
Copy link
Author

@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 😄), 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.

@simi
Copy link
Collaborator

simi commented Dec 28, 2015

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

@kennyadsl
Copy link
Author

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
Copy link
Author

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

@rubyconvict
Copy link

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.

@kennyadsl
Copy link
Author

@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.

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

No branches or pull requests

3 participants