Skip to content

Instantly share code, notes, and snippets.

@genki
Created March 21, 2009 20:38
Show Gist options
  • Save genki/82975 to your computer and use it in GitHub Desktop.
Save genki/82975 to your computer and use it in GitHub Desktop.
def set_locale
I18n.locale = @original_locale = fallback_locale(I18n.locale)
if request.host =~ /^([\w-]+)\.#{HOST.split(':')[0]}/i
I18n.locale = fallback_locale($1)
end
end
private
def fallback_locale(locale)
locale = locale.to_s
@available_locales ||= Set.new(I18n.available_locales)
until locale.empty? || @available_locales.include?(locale.intern)
locale = locale.split(/([-_])/)[0..-3].join
end
locale.present? ? locale.intern : I18n.default_locale
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment