diff options
author | Seb Bacon <seb.bacon@gmail.com> | 2012-06-14 13:47:47 +0100 |
---|---|---|
committer | Seb Bacon <seb.bacon@gmail.com> | 2012-06-14 13:47:47 +0100 |
commit | b30554dc4fcdeeb42d827ead474b1e135bc877e8 (patch) | |
tree | 0d174d7135a102b8b01064b8ebcb20f70a17658a /lib | |
parent | 118bc669365a38d60f696802e1f5d678393ed2f3 (diff) |
Ensure we generate URLs containing the current locale, even if the locale contains an underscore.
Includes a bonus test to ensure that locales with unknown territories fall back to known languages.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/routing_filters.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/routing_filters.rb b/lib/routing_filters.rb index cdb58e7c1..32dafc651 100644 --- a/lib/routing_filters.rb +++ b/lib/routing_filters.rb @@ -5,5 +5,22 @@ module RoutingFilter 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 end end |