-
-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathglobalize.rb
More file actions
90 lines (71 loc) · 2.12 KB
/
globalize.rb
File metadata and controls
90 lines (71 loc) · 2.12 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'request_store'
require 'active_record'
require 'patches/active_record/xml_attribute_serializer'
require 'patches/active_record/query_method'
require 'patches/active_record/relation'
require 'patches/active_record/serialization'
require 'patches/active_record/uniqueness_validator'
require 'patches/active_record/persistence'
module Globalize
autoload :ActiveRecord, 'globalize/active_record'
autoload :Interpolation, 'globalize/interpolation'
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
begin
set_locale(locale)
result = yield(locale)
ensure
set_locale(previous_locale)
end
result
end
def with_locales(*locales, &block)
locales.flatten.map do |locale|
with_locale(locale, &block)
end
end
def fallbacks=(locales)
set_fallbacks(locales)
end
def i18n_fallbacks?
I18n.respond_to?(:fallbacks)
end
def fallbacks(for_locale = self.locale)
read_fallbacks[for_locale] || default_fallbacks(for_locale)
end
def default_fallbacks(for_locale = self.locale)
i18n_fallbacks? ? I18n.fallbacks[for_locale] : [for_locale.to_sym]
end
# Thread-safe global storage
def storage
RequestStore.store
end
protected
def read_locale
storage[:globalize_locale]
end
def set_locale(locale)
storage[:globalize_locale] = locale.try(:to_sym)
end
def read_fallbacks
storage[:globalize_fallbacks] || HashWithIndifferentAccess.new
end
def set_fallbacks(locales)
fallback_hash = HashWithIndifferentAccess.new
locales.each do |key, value|
fallback_hash[key] = value.presence || [key]
end if locales.present?
storage[:globalize_fallbacks] = fallback_hash
end
end
end
ActiveRecord::Base.mattr_accessor :globalize_serialized_attributes, instance_writer: false
ActiveRecord::Base.globalize_serialized_attributes = {}
ActiveRecord::Base.extend(Globalize::ActiveRecord::ActMacro)