Responsibility:
- store data threadsave - provide error messages when repositories are unconfigured - accept/reject locales that are set by the user
# File lib/fast_gettext/storage.rb, line 27 def available_locales locales = Thread.current[:fast_gettext_available_locales] || default_available_locales return unless locales locales.map{|s|s.to_s} end
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
# File lib/fast_gettext/storage.rb, line 155 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
# File lib/fast_gettext/storage.rb, line 101 def cached_find(key) translation = current_cache[key] if translation.nil? # uncached current_cache[key] = current_repository[key] || false else translation end end
# File lib/fast_gettext/storage.rb, line 110 def cached_plural_find(*keys) key = '||||' + keys * '||||' translation = current_cache[key] if translation.nil? # uncached current_cache[key] = current_repository.plural(*keys) || false else translation end end
# File lib/fast_gettext/storage.rb, line 89 def caches @@caches end
# File lib/fast_gettext/storage.rb, line 67 def current_cache Thread.current[:fast_gettext_current_cache] || {} end
# File lib/fast_gettext/storage.rb, line 71 def current_cache=(cache) Thread.current[:fast_gettext_current_cache] = cache end
# File lib/fast_gettext/storage.rb, line 93 def current_repository translation_repositories[text_domain] || raise(NoTextDomainConfigured) end
# File lib/fast_gettext/storage.rb, line 40 def default_available_locales @@default_available_locales end
# File lib/fast_gettext/storage.rb, line 35 def default_available_locales=(avail_locales) @@default_available_locales = avail_locales update_current_cache end
# File lib/fast_gettext/storage.rb, line 147 def default_locale @@default_locale end
# File lib/fast_gettext/storage.rb, line 142 def default_locale=(new_locale) @@default_locale = best_locale_in(new_locale) update_current_cache end
# File lib/fast_gettext/storage.rb, line 56 def default_text_domain @@default_text_domain end
# File lib/fast_gettext/storage.rb, line 51 def default_text_domain=(domain) @@default_text_domain = domain update_current_cache end
# File lib/fast_gettext/storage.rb, line 120 def expire_cache_for(key) current_cache.delete(key) end
# File lib/fast_gettext/storage.rb, line 97 def key_exist?(key) !!(cached_find key) end
# File lib/fast_gettext/storage.rb, line 124 def locale _locale || ( default_locale || (available_locales||[]).first || 'en' ) end
# File lib/fast_gettext/storage.rb, line 128 def locale=(new_locale) set_locale(new_locale) end
if overwritten by user( #pluralisation_rule = xxx) use it, otherwise fall back to repo or to default lambda
# File lib/fast_gettext/storage.rb, line 63 def pluralisation_rule Thread.current[:fast_gettext_pluralisation_rule] || current_repository.pluralisation_rule || lambda{|i| i!=1} end
# File lib/fast_gettext/storage.rb, line 75 def reload! self.current_cache = {} translation_repositories.values.each(&:reload) end
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
# File lib/fast_gettext/storage.rb, line 135 def set_locale(new_locale) new_locale = best_locale_in(new_locale) self._locale = new_locale locale end
turn off translation if none was defined to disable all resulting errors
# File lib/fast_gettext/storage.rb, line 165 def silence_errors require 'fast_gettext/translation_repository/base' translation_repositories[text_domain] ||= TranslationRepository::Base.new('x', :path => 'locale') end
# File lib/fast_gettext/storage.rb, line 45 def text_domain Thread.current[:fast_gettext_text_domain] || default_text_domain end
# File lib/fast_gettext/storage.rb, line 82 def translation_repositories @@translation_repositories end
# File lib/fast_gettext/storage.rb, line 21 def _locale Thread.current[:fast_gettext__locale] end
de-de -> de_DE
# File lib/fast_gettext/storage.rb, line 196 def format_locale(locale) locale.sub(/^([a-zA-Z]{2,3})[-_]([a-zA-Z]{2,3})$/){$1.downcase+'_'+$2.upcase} end
de-de,DE-CH;q=0.9 -> ['de_DE','de_CH']
# File lib/fast_gettext/storage.rb, line 173 def formatted_sorted_locales(locales) found = weighted_locales(locales).reject{|x|x.empty?}.sort_by{|l|l.last}.reverse #sort them by weight which is the last entry found.flatten.map{|l| format_locale(l)} end
# File lib/fast_gettext/storage.rb, line 200 def update_current_cache caches[text_domain] ||= {} caches[text_domain][locale] ||= {} caches[text_domain][locale][""] = false #ignore gettext meta key when translating self.current_cache = caches[text_domain][locale] end
split the locale and seperate it into different languages de-de,de;q=0.9,en;q=0.8 => [['de-de','de','0.5'], ['en','0.8']]
# File lib/fast_gettext/storage.rb, line 180 def weighted_locales(locales) locales = locales.to_s.gsub(/\s/,'') found = [[]] locales.split(',').each do |part| if part =~ /;q=/ #contains language and weight ? found.last << part.split(/;q=/) found.last.flatten! found << [] else found.last << part end end found end