forked from globalize/globalize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalize.rb
52 lines (41 loc) · 1.08 KB
/
globalize.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'active_record'
require 'patches/active_record/xml_attribute_serializer'
require 'patches/active_record/query_method'
module Globalize
autoload :ActiveRecord, 'globalize/active_record'
autoload :Versioning, 'globalize/versioning'
class << self
def locale
read_locale || I18n.locale
end
def locale=(locale)
set_locale(locale)
end
def with_locale(locale, &block)
previous_locale = read_locale
set_locale(locale)
result = yield(locale)
set_locale(previous_locale)
result
end
def with_locales(*locales, &block)
locales.flatten.map do |locale|
with_locale(locale, &block)
end
end
def fallbacks?
I18n.respond_to?(:fallbacks)
end
def fallbacks(locale = self.locale)
fallbacks? ? I18n.fallbacks[locale] : [locale.to_sym]
end
protected
def read_locale
Thread.current[:globalize_locale]
end
def set_locale(locale)
Thread.current[:globalize_locale] = locale
end
end
end
ActiveRecord::Base.extend(Globalize::ActiveRecord::ActMacro)