Skip to content

Commit

Permalink
Merge pull request #4 from mathieugagne/master
Browse files Browse the repository at this point in the history
New features
  • Loading branch information
mateomurphy committed Apr 2, 2013
2 parents 56ca92b + 40ed2de commit 6489e74
Show file tree
Hide file tree
Showing 14 changed files with 584 additions and 508 deletions.
1 change: 1 addition & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm use 2.0.0@rails_locale_detection --create
17 changes: 2 additions & 15 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
source "http://rubygems.org"

gem 'activesupport', '~> 3.2.13'
gem 'http_accept_language'

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem 'i18n'
gem 'timecop'
gem 'actionpack', '~> 3.2.13'
gem "rspec", "~> 2.12.0"
gem "bundler", "~> 1.3.4"
gem "jeweler", "~> 1.8.4"
end
source "https://rubygems.org"
gemspec
61 changes: 53 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
PATH
remote: .
specs:
rails_locale_detection (1.3.2)
http_accept_language
i18n
rails (~> 3.2.13)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
actionmailer (3.2.13)
actionpack (= 3.2.13)
mail (~> 2.5.3)
actionpack (3.2.13)
activemodel (= 3.2.13)
activesupport (= 3.2.13)
Expand All @@ -14,9 +25,18 @@ GEM
activemodel (3.2.13)
activesupport (= 3.2.13)
builder (~> 3.0.0)
activerecord (3.2.13)
activemodel (= 3.2.13)
activesupport (= 3.2.13)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.13)
activemodel (= 3.2.13)
activesupport (= 3.2.13)
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.4)
diff-lcs (1.1.3)
erubis (2.7.0)
Expand All @@ -31,14 +51,37 @@ GEM
rdoc
journey (1.0.4)
json (1.7.7)
multi_json (1.7.1)
mail (2.5.3)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.21)
multi_json (1.7.2)
polyglot (0.3.3)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
rake (10.0.3)
rdoc (4.0.0)
rails (3.2.13)
actionmailer (= 3.2.13)
actionpack (= 3.2.13)
activerecord (= 3.2.13)
activeresource (= 3.2.13)
activesupport (= 3.2.13)
bundler (~> 1.0)
railties (= 3.2.13)
railties (3.2.13)
actionpack (= 3.2.13)
activesupport (= 3.2.13)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.0.4)
rdoc (3.12.2)
json (~> 1.4)
rspec (2.12.0)
rspec-core (~> 2.12.0)
Expand All @@ -53,18 +96,20 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.18.0)
tilt (1.3.6)
timecop (0.6.1)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.37)

PLATFORMS
ruby

DEPENDENCIES
actionpack (~> 3.2.13)
activesupport (~> 3.2.13)
bundler (~> 1.3.4)
http_accept_language
i18n
jeweler (~> 1.8.4)
rails_locale_detection!
rspec (~> 2.12.0)
timecop
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,22 @@
[![Build Status](https://travis-ci.org/mateomurphy/rails_locale_detection.png?branch=master)](https://travis-ci.org/mateomurphy/rails_locale_detection)
[![Dependency Status](https://gemnasium.com/mateomurphy/rails_locale_detection.png)](https://gemnasium.com/mateomurphy/rails_locale_detection)

Sets the current locale of a request using a combination of params, cookies, http headers, and an optional user object.
Sets the current locale of a request using a combination of params, cookies, http headers, and an optional user object.

In turn, it checks the value of params[:locale], cookies[:locale] and HTTP_ACCEPT_LANGUAGE headers to find a locale that
corresponds to the available locales, then stores the set locale in a cookie for future requests. If a user_locale method
is provided, the return value will be used, with preference over the other locale detection methods.

## Usage

Include the gem in your Gemfile
Include the gem in your Gemfile

gem 'rails_locale_detection'

Set your default and available locales

I18n.default_locale = :en
I18n.available_locales = [:en, :fr]

Call `set_locale` as a filter in your controllers

class ApplicationController < ActionController::Base
before_filter :set_locale

end

To support user locales, add a `user_locale` method

Expand All @@ -35,7 +28,7 @@ To support user locales, add a `user_locale` method
def user_locale
current_user.locale if current_user
end

end


Expand All @@ -44,9 +37,9 @@ To support user locales, add a `user_locale` method
Configuration is done via a block that can be added as an initializer:

Rails::LocaleDetection.config do |config|
config.locale_expiry = 3.months
config.locale_expiry = 3.months
config.set_default_url_option = :always # valid values are true, false, :always, :never and :explicity
config.detection_order = [:user, :param, :cookie, :request]
config.detection_order = [:user, :param, :cookie, :request]
end

The configuration options:
Expand All @@ -59,7 +52,7 @@ The configuration options:
* `detection_order` set the order in which locale detection occurs. Omit values to skip those sources

## Contributing to rails_locale_detection

* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
Expand Down
74 changes: 0 additions & 74 deletions lib/rails/locale_detection.rb

This file was deleted.

71 changes: 71 additions & 0 deletions lib/rails/locale_detection/filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Rails
module LocaleDetection
module Filter
extend ActiveSupport::Concern

included do
append_before_filter :set_locale
end

def available_locales
I18n.available_locales
end

def default_locale
I18n.default_locale
end

def current_locale
I18n.locale
end

def user_locale
nil
end

# returns the (symbolized) value passed if it's in the available_locales
def validate_locale(locale)
locale.to_sym if locale && available_locales.include?(locale.to_sym)
end

def locale_from_param
validate_locale(params[:locale])
end

def locale_from_cookie
validate_locale(cookies[:locale])
end

def locale_from_request
validate_locale(request.preferred_language_from(available_locales))
end

def locale_from_user
validate_locale(user_locale)
end

def locale_from(key)
send("locale_from_#{key}")
end

def get_locale
Rails::LocaleDetection.detection_order.inject(nil) { |result, source| result || locale_from(source) } || default_locale
end

# set I18n.locale, default_url_options[:locale] and cookies[:locale] to the value returned by
# get_locale
def set_locale
I18n.locale = get_locale
default_url_options[:locale] = I18n.locale if set_default_url_option_for_request?
cookies[:locale] = { :value => I18n.locale, :expires => Rails::LocaleDetection.locale_expiry.from_now }
end

protected

# returns true if the default url option should be set for this request
def set_default_url_option_for_request?
Rails::LocaleDetection.set_default_url_option === true || Rails::LocaleDetection.set_default_url_option == :always || Rails::LocaleDetection.set_default_url_option == :explicitly && params[:locale].present?
end
end
end
end
38 changes: 38 additions & 0 deletions lib/rails/locale_detection/internationalization_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Rails
module LocaleDetection
module InternationalizationHelper

def current_locale
I18n.locale
end

def available_locales
I18n.available_locales
end

def link_to_locale html_options={}
if available_locales.length == 2
link_to_locale_single html_options
else
link_to_locale_list html_options
end
end

protected

def link_to_locale_list html_options
content_tag :ul, html_options do
available_locales.each do |lang|
concat(content_tag(:li, link_to(t('locale_name', locale: lang), url_for(params.merge(locale: lang)))))
end
end
end

def link_to_locale_single html_options
lang = (available_locales - [current_locale]).first
link_to t('locale_name', locale: lang), url_for(params.merge(locale: lang)), html_options
end

end
end
end
14 changes: 14 additions & 0 deletions lib/rails/locale_detection/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Rails
module LocaleDetection
class Railtie < Rails::Railtie
initializer "rails_locale_detection.append_before_filter" do
ActiveSupport.on_load(:action_controller) do
ActionController::Base.send :include, Rails::LocaleDetection::Filter
end
ActiveSupport.on_load(:action_view) do
ActionView::Base.send :include, Rails::LocaleDetection::InternationalizationHelper
end
end
end
end
end
Loading

0 comments on commit 6489e74

Please sign in to comment.