Skip to content

Commit

Permalink
Replace use of ::I18n.locale with context.locale
Browse files Browse the repository at this point in the history
This change makes the locale setting thread-safe.

The right way to use it is to pass "force_locale: context.locale"
or to pass context.locale as third argument to I18n's translate()
and locale() methods.

Also, remove last remaining pieces of dependence on Amber, since the
shard is generic.
  • Loading branch information
docelic committed May 23, 2018
1 parent 8d5e958 commit dad0ce3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions spec/citrine-i18n_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe Citrine::I18n::Handler do
context = create_context(request)
handler = Citrine::I18n::Handler.new
handler.call(context)
I18n.locale.should eq "fr"
context.locale.should eq "fr"
end

it "should set language from complicated header" do
Expand All @@ -22,7 +22,7 @@ describe Citrine::I18n::Handler do
context = create_context(request)
handler = Citrine::I18n::Handler.new
handler.call(context)
I18n.locale.should eq "en"
context.locale.should eq "en"
end

end
12 changes: 8 additions & 4 deletions src/citrine-i18n.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require "amber"
require "http/server/handler"
require "i18n"
require "./citrine-i18n/parser"
require "./citrine-i18n/handler"

class HTTP::Server::Context
property locale : String = "en"
end

module Citrine::I18n
def self.configure
yield ::I18n.config
end
def self.configure
yield ::I18n.config
end
end
2 changes: 1 addition & 1 deletion src/citrine-i18n/handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Citrine
if languages = context.request.headers[HEADER]?
parser = Citrine::I18n::Parser.new languages
compat = parser.compatible_language_from ::I18n.available_locales
::I18n.locale = compat if compat
context.locale = compat if compat
#Amber.logger.debug "Languages available: #{languages.to_s}"
#Amber.logger.debug "Language chosen: #{::I18n.locale}"
end
Expand Down

0 comments on commit dad0ce3

Please sign in to comment.