First stage of i18n. Some migrations and extra plugins.

This commit is contained in:
Shaun McDonald 2009-05-22 18:36:17 +00:00
parent 6ac7f91734
commit 53b4d645d8
82 changed files with 6876 additions and 18 deletions

View file

@ -0,0 +1,52 @@
module HttpAcceptLanguage
# Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
# Browsers send this HTTP header, so don't think this is holy.
#
# Example:
#
# request.user_preferred_languages
# # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
#
def user_preferred_languages
@user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(',').collect do |l|
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
l.split(';q=')
end.sort do |x,y|
raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/i
y.last.to_f <=> x.last.to_f
end.collect do |l|
l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
end
rescue # Just rescue anything if the browser messed up badly.
[]
end
# Finds the locale specifically requested by the browser.
#
# Example:
#
# request.preferred_language_from I18n.available_locales
# # => 'nl'
#
def preferred_language_from(array)
(user_preferred_languages & array.collect { |i| i.to_s }).first
end
# Returns the first of the user_preferred_languages that is compatible
# with the available locales. Ignores region.
#
# Example:
#
# request.compatible_language_from I18n.available_locales
#
def compatible_language_from(array)
user_preferred_languages.map do |x|
x = x.to_s.split("-")[0]
array.find do |y|
y.to_s.split("-")[0] == x
end
end.compact.first
end
end