-
Notifications
You must be signed in to change notification settings - Fork 84
/
locale.rb
35 lines (31 loc) · 1 KB
/
locale.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'i18n'
require 'routing_filter/base'
module RoutingFilter
class Locale < Base
@@default_locale = :en
cattr_reader :default_locale
class << self
def default_locale=(locale)
@@default_locale = locale.to_sym
end
end
# remove the locale from the beginning of the path, pass the path
# to the given block and set it to the resulting params hash
def around_recognize(path, env, &block)
locale = nil
path.sub! %r(^/([a-zA-Z]{2}|[a-zA-Z]{2}\-[a-zA-Z]{2})(?=/|$)) do locale = $1; '' end
returning yield do |params|
params[:locale] = locale if locale
end
end
def around_generate(*args, &block)
locale = args.extract_options!.delete(:locale) || I18n.locale
returning yield do |result|
if locale.to_sym != @@default_locale
target = result.is_a?(Array) ? result.first : result
target.sub!(%r(^(http.?://[^/]*)?(.*))){ "#{$1}/#{locale}#{$2}" }
< end
end
end
end
end