Module: FastGettext::Storage
- Included in:
- FastGettext
- Defined in:
- lib/fast_gettext/storage.rb
Overview
Responsibility:
- store data threadsafe
- provide error messages when repositories are unconfigured
- accept/reject locales that are set by the user
Defined Under Namespace
Classes: NoTextDomainConfigured
Constant Summary
collapse
- DEFAULT_PLURALIZATION_RULE =
->(i) { i != 1 }
- @@translation_repositories =
global, since re-parsing whole folders takes too much time…
{}
- @@default_locale =
rubocop:disable Style/ClassVars
nil
Instance Method Summary
collapse
Instance Method Details
#available_locales ⇒ Object
33
34
35
36
37
38
|
# File 'lib/fast_gettext/storage.rb', line 33
def available_locales
locales = Thread.current[:fast_gettext_available_locales] || default_available_locales
return unless locales
locales.map(&:to_s)
end
|
#best_locale_in(locales) ⇒ Object
Opera: de-DE,de;q=0.9,en;q=0.8 Firefox de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 IE6/7 de nil if nothing matches
136
137
138
139
140
141
142
143
|
# File 'lib/fast_gettext/storage.rb', line 136
def best_locale_in(locales)
formatted_sorted_locales(locales).each do |candidate|
return candidate unless available_locales
return candidate if available_locales.include?(candidate)
return candidate[0..1] if available_locales.include?(candidate[0..1]) end
nil end
|
#cache ⇒ Object
69
70
71
|
# File 'lib/fast_gettext/storage.rb', line 69
def cache
Thread.current[:fast_gettext_cache] ||= cache_class.new
end
|
#cached_find(key) ⇒ Object
92
93
94
|
# File 'lib/fast_gettext/storage.rb', line 92
def cached_find(key)
cache.fetch(key) { current_repository[key] }
end
|
#cached_plural_find(*keys) ⇒ Object
96
97
98
99
|
# File 'lib/fast_gettext/storage.rb', line 96
def cached_plural_find(*keys)
key = '||||' + keys * '||||'
cache.fetch(key) { current_repository.plural(*keys) }
end
|
#current_repository ⇒ Object
84
85
86
|
# File 'lib/fast_gettext/storage.rb', line 84
def current_repository
translation_repositories[text_domain] || raise(NoTextDomainConfigured)
end
|
#default_locale ⇒ Object
128
129
130
|
# File 'lib/fast_gettext/storage.rb', line 128
def default_locale
@@default_locale
end
|
#default_locale=(new_locale) ⇒ Object
123
124
125
126
|
# File 'lib/fast_gettext/storage.rb', line 123
def default_locale=(new_locale)
@@default_locale = best_locale_in(new_locale) switch_cache
end
|
#expire_cache_for(key) ⇒ Object
101
102
103
|
# File 'lib/fast_gettext/storage.rb', line 101
def expire_cache_for(key)
cache.delete(key)
end
|
#key_exist?(key) ⇒ Boolean
88
89
90
|
# File 'lib/fast_gettext/storage.rb', line 88
def key_exist?(key)
!!(cached_find key)
end
|
#locale ⇒ Object
105
106
107
|
# File 'lib/fast_gettext/storage.rb', line 105
def locale
_locale || (default_locale || (available_locales || []).first || 'en')
end
|
#locale=(new_locale) ⇒ Object
109
110
111
|
# File 'lib/fast_gettext/storage.rb', line 109
def locale=(new_locale)
set_locale(new_locale)
end
|
#pluralisation_rule ⇒ Object
if overwritten by user( FastGettext.pluralisation_rule = xxx) use it, otherwise fall back to repo or to default lambda
65
66
67
|
# File 'lib/fast_gettext/storage.rb', line 65
def pluralisation_rule
Thread.current[:fast_gettext_pluralisation_rule] || current_repository.pluralisation_rule || DEFAULT_PLURALIZATION_RULE
end
|
#reload! ⇒ Object
73
74
75
76
|
# File 'lib/fast_gettext/storage.rb', line 73
def reload!
cache.reload!
translation_repositories.values.each(&:reload)
end
|
#set_locale(new_locale) ⇒ Object
for chaining: puts set_locale(‘xx’) == ‘xx’ ? ‘applied’ : ‘rejected’ returns the current locale, not the one that was supplied like locale=(), whoes behavior cannot be changed
116
117
118
119
120
|
# File 'lib/fast_gettext/storage.rb', line 116
def set_locale(new_locale) new_locale = best_locale_in(new_locale)
self._locale = new_locale
locale
end
|
#silence_errors ⇒ Object
turn off translation if none was defined to disable all resulting errors
156
157
158
159
|
# File 'lib/fast_gettext/storage.rb', line 156
def silence_errors
require 'fast_gettext/translation_repository/base'
translation_repositories[text_domain] ||= TranslationRepository::Base.new('x', path: 'locale')
end
|
#text_domain ⇒ Object
59
60
61
|
# File 'lib/fast_gettext/storage.rb', line 59
def text_domain
Thread.current[:fast_gettext_text_domain] || default_text_domain
end
|
#translation_repositories ⇒ Object
rubocop:disable Style/ClassVars
80
81
82
|
# File 'lib/fast_gettext/storage.rb', line 80
def translation_repositories
@@translation_repositories
end
|
#with_locale(temp_locale) ⇒ Object
temporarily switch locale for a block FastGettext.with_locale ‘xx’ { _(‘cars’) }
147
148
149
150
151
152
153
|
# File 'lib/fast_gettext/storage.rb', line 147
def with_locale(temp_locale)
current_locale = locale
set_locale temp_locale
yield
ensure
set_locale current_locale
end
|