-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mathieugagne/master
New features
- Loading branch information
Showing
14 changed files
with
584 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rvm use 2.0.0@rails_locale_detection --create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.