Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Accept-Language to I18n.default_locale #50

Merged
merged 2 commits into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/http_accept_language/auto_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module AutoLocale
private

def set_locale
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) || I18n.default_locale
end
end
end
37 changes: 35 additions & 2 deletions spec/auto_locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
describe HttpAcceptLanguage::AutoLocale do
let(:controller_class) do
Class.new do
def initialize(header = nil)
super()
@header = header
end

def self.before_filter(dummy)
# dummy method
end

def http_accept_language
HttpAcceptLanguage::Parser.new("ja,en-us;q=0.7,en;q=0.3")
@http_accept_language ||= HttpAcceptLanguage::Parser.new(@header)
end

include HttpAcceptLanguage::AutoLocale
end
end

let(:controller) { controller_class.new }
let(:controller) { controller_class.new("ja,en-us;q=0.7,en;q=0.3") }

context "available languages includes accept_languages" do
before do
Expand All @@ -31,4 +36,32 @@ def http_accept_language
expect(I18n.locale).to eq(:ja)
end
end

context "available languages do not include accept_languages" do
before do
I18n.available_locales = [:es]
I18n.default_locale = :es
end

it "set the locale to default" do
no_accept_language_controller.send(:set_locale)

expect(I18n.locale).to eq(:es)
end
end

let(:no_accept_language_controller) { controller_class.new() }

context "default locale is ja" do
before do
I18n.available_locales = [:en, :ja]
I18n.default_locale = :ja
end

it "set the locale to default" do
no_accept_language_controller.send(:set_locale)

expect(I18n.locale).to eq(:ja)
end
end
end