Skip to content

Commit

Permalink
Added the ability to allow users to set a local_aliases option on the…
Browse files Browse the repository at this point in the history
… locale filter that allows a hash of locale aliases to be passed such as :locale_aliases => {'en-UK' => 'UK', 'fr' => 'France'} this would allow URL's such as http://example.com/UK to use the en-UK locale and also generate links to the alias. The aliases will also be case insensitive.

Signed-off-by: Sven Fuchs <svenfuchs@artweb-design.de>
  • Loading branch information
Steve Smith authored and Sven Fuchs committed Aug 5, 2009
1 parent 97bc651 commit 2cde58d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/routing_filter/locale.rb
Expand Up @@ -5,6 +5,9 @@ module RoutingFilter
class Locale < Base
@@include_default_locale = true
cattr_writer :include_default_locale

@@locale_aliases = {}
@@reverse_aliases = nil

class << self
def include_default_locale?
Expand All @@ -20,7 +23,7 @@ def locales=(locales)
end

def locales_pattern
@@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
@@locales_pattern ||= /^\/(#{self.locales.concat(@@locale_aliases.values).map { |l| Regexp.escape(l.to_s) }.join('|')})(?=\/|$)/i
end
end

Expand Down Expand Up @@ -48,7 +51,8 @@ def around_generate(*args, &block)

def extract_locale!(path)
path.sub! self.class.locales_pattern, ''
$1
@@reverse_aliases ||= Hash[*@@locale_aliases.map{|k,v| [v.downcase,k]}.flatten]
@@reverse_aliases[$1.downcase] || $1 if $1
end

def prepend_locale?(locale)
Expand All @@ -64,6 +68,7 @@ def default_locale?(locale)
end

def prepend_locale!(url, locale)
locale = @@locale_aliases[locale] || locale
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{locale}#{$2}" }
end
end
Expand Down

0 comments on commit 2cde58d

Please sign in to comment.