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,20 @@
Copyright (c) 2008 [name of plugin creator]
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,39 @@
= HttpAcceptLanguage
A small effort in making a plugin which helps you detect the users preferred language, as sent by the HTTP header.
= Features
* Splits the http-header into languages specified by the user
* Returns empty array if header is illformed.
* Corrects case to xx-XX
* Sorted by priority given, as much as possible.
* Gives you the most important language
* Gives compatible languages
See also: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
= Example
class SomeController < ApplicationController
def some_action
request.user_preferred_languages
# => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
available = %w{en en-US nl-BE}
request.preferred_language_from(available)
# => 'nl-BE'
request.user_preferred_language
# => [ 'en-GB']
available = %w{en-US}
request.compatible_language_from(available)
# => 'en-US'
end
end
= Changenotes
2009-03-12: Rails 2.3 compatible
Copyright (c) 2008 Iain Hecker, released under the MIT license

View file

@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
desc 'Test the http_accept_language plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
desc 'Generate documentation for the http_accept_language plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'HttpAcceptLanguage'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

View file

@ -0,0 +1,7 @@
if defined?(ActionController::Request)
ActionController::Request.send :include, HttpAcceptLanguage
elsif defined?(ActionController::AbstractRequest)
ActionController::AbstractRequest.send :include, HttpAcceptLanguage
else
ActionController::CgiRequest.send :include, HttpAcceptLanguage
end

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

View file

@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :http_accept_language do
# # Task goes here
# end

View file

@ -0,0 +1,45 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'http_accept_language'
require 'test/unit'
class MockedCgiRequest
include HttpAcceptLanguage
def env
@env ||= {'HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6'}
end
end
class HttpAcceptLanguageTest < Test::Unit::TestCase
def test_should_return_empty_array
request.env['HTTP_ACCEPT_LANGUAGE'] = nil
assert_equal [], request.user_preferred_languages
end
def test_should_properly_split
assert_equal %w{en-US en-GB en}, request.user_preferred_languages
end
def test_should_ignore_jambled_header
request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
assert_equal [], request.user_preferred_languages
end
def test_should_find_first_available_language
assert_equal 'en-GB', request.preferred_language_from(%w{en en-GB})
end
def test_should_find_first_compatible_language
assert_equal 'en-hk', request.compatible_language_from(%w{en-hk})
assert_equal 'en', request.compatible_language_from(%w{en})
end
def test_should_find_first_compatible_from_user_preferred
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
assert_equal 'en', request.compatible_language_from(%w{de en})
end
private
def request
@request ||= MockedCgiRequest.new
end
end