blob: 5b5da6870bb48dfae99c4e49afc772cf2ddd7a95 (
plain)
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
|
module RoutingFilter
class Conditionallyprependlocale < RoutingFilter::Locale
# Override core Locale filter not to prepend locale path segment
# when there's only one locale
def prepend_locale?(locale)
locale && I18n.available_locales.length > 1 && (self.class.include_default_locale? || !default_locale?(locale))
end
# And override the generation logic to use FastGettext.locale
# rather than I18n.locale (the latter is what rails uses
# internally and may look like `en-US`, whereas the latter is
# was FastGettext and other POSIX-based systems use, and will
# look like `en_US`
def around_generate(*args, &block)
params = args.extract_options! # this is because we might get a call like forum_topics_path(forum, topic, :locale => :en)
locale = params.delete(:locale) # extract the passed :locale option
locale = FastGettext.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false)
locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid
args << params
yield.tap do |result|
prepend_segment!(result, locale) if prepend_locale?(locale)
end
end
# Reset the locale pattern when the locales are set.
class << self
def locales=(locales)
@@locales_pattern = nil
@@locales = locales.map(&:to_sym)
end
end
end
end
|