Module: FastGettext::Storage

Included in:
FastGettext
Defined in:
lib/fast_gettext/storage.rb

Overview

Responsibility:

- store data threadsave
- 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_available_locales =

cattr_accessor :default_available_locales

nil
@@default_text_domain =

cattr_accessor :default_text_domain

nil
@@translation_repositories =

global, since re-parsing whole folders takes too much time…

{}
@@caches =

used to speedup simple translations, does not work for pluralisation caches[locale]=translation

{}
@@default_locale =
nil

Instance Method Summary collapse

Instance Method Details

#available_localesObject



27
28
29
# File 'lib/fast_gettext/storage.rb', line 27

def available_locales
  Thread.current[:fast_gettext_available_locales] || default_available_locales
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 if not available_locales
    return candidate if available_locales.include?(candidate)
    return candidate[0..1] if available_locales.include?(candidate[0..1])#available locales include a langauge
  end
  return nil#nothing found im sorry :P
end

#cached_find(key) ⇒ Object



92
93
94
95
96
# File 'lib/fast_gettext/storage.rb', line 92

def cached_find(key)
  translation = current_cache[key]
  return translation if translation or translation == false #found or was not found before
  current_cache[key] = current_repository[key] || false
end

#cached_plural_find(*keys) ⇒ Object



98
99
100
101
102
103
# File 'lib/fast_gettext/storage.rb', line 98

def cached_plural_find(*keys)
  key = '||||' + keys * '||||'
  translation = current_cache[key]
  return translation if translation or translation == false #found or was not found before
  current_cache[key] = current_repository.plural(*keys) || false
end

#cachesObject



80
81
82
# File 'lib/fast_gettext/storage.rb', line 80

def caches
  @@caches
end

#current_cacheObject



63
64
65
# File 'lib/fast_gettext/storage.rb', line 63

def current_cache
  Thread.current[:fast_gettext_current_cache] || {}
end

#current_cache=(cache) ⇒ Object



67
68
69
# File 'lib/fast_gettext/storage.rb', line 67

def current_cache=(cache)
  Thread.current[:fast_gettext_current_cache] = cache
end

#current_repositoryObject



84
85
86
# File 'lib/fast_gettext/storage.rb', line 84

def current_repository
  translation_repositories[text_domain] || raise(NoTextDomainConfigured)
end

#default_available_localesObject



38
39
40
# File 'lib/fast_gettext/storage.rb', line 38

def default_available_locales
  @@default_available_locales
end

#default_available_locales=(avail_locales) ⇒ Object



33
34
35
36
# File 'lib/fast_gettext/storage.rb', line 33

def default_available_locales=(avail_locales)
  @@default_available_locales = avail_locales
  update_current_cache
end

#default_localeObject



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)
  update_current_cache
end

#default_text_domainObject



54
55
56
# File 'lib/fast_gettext/storage.rb', line 54

def default_text_domain
  @@default_text_domain
end

#default_text_domain=(domain) ⇒ Object



49
50
51
52
# File 'lib/fast_gettext/storage.rb', line 49

def default_text_domain=(domain)
  @@default_text_domain = domain
  update_current_cache
end

#key_exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/fast_gettext/storage.rb', line 88

def key_exist?(key)
  !!(cached_find key)
end

#localeObject



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
112
# File 'lib/fast_gettext/storage.rb', line 109

def locale=(new_locale)
  new_locale = best_locale_in(new_locale)
  self._locale = new_locale if new_locale
end

#pluralisation_ruleObject



59
60
61
# File 'lib/fast_gettext/storage.rb', line 59

def pluralisation_rule
  Thread.current[:fast_gettext_pluralisation_rule] ||  current_repository.pluralisation_rule || lambda{|i| i!=1}
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



117
118
119
120
# File 'lib/fast_gettext/storage.rb', line 117

def set_locale(new_locale)
  self.locale = new_locale
  locale
end

#silence_errorsObject

turn off translation if none was defined to disable all resulting errors



146
147
148
149
# File 'lib/fast_gettext/storage.rb', line 146

def silence_errors
  require 'fast_gettext/translation_repository/base'
  translation_repositories[text_domain] = TranslationRepository::Base.new('x')
end

#text_domainObject



43
44
45
# File 'lib/fast_gettext/storage.rb', line 43

def text_domain
  Thread.current[:fast_gettext_text_domain] || default_text_domain
end

#translation_repositoriesObject



73
74
75
# File 'lib/fast_gettext/storage.rb', line 73

def translation_repositories
  @@translation_repositories
end