Skip to content

Support for case insensitive locales #58

@kennyadsl

Description

@kennyadsl

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions