Permalink
Browse files

Merge pull request #50 from knu/missing_accept_language

Default `Accept-Language` to `I18n.default_locale`
  • Loading branch information...
2 parents 5073e34 + aa66da7 commit 731921552f8a1ac15269cf892f4d810b0c7e21b6 @DouweM DouweM committed Oct 15, 2015
Showing with 36 additions and 3 deletions.
  1. +1 −1 lib/http_accept_language/auto_locale.rb
  2. +35 −2 spec/auto_locale_spec.rb
View
2 lib/http_accept_language/auto_locale.rb
@@ -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
View
37 spec/auto_locale_spec.rb
@@ -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
@@ -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

0 comments on commit 7319215

Please sign in to comment.