Module: I18n

Defined in:
common/aspace_i18n_enumeration_support.rb,
common/aspace_i18n.rb

Overview

Define a new :t_raw method that knows how to handle ArchivesSpace-specific enumeration translations.

Called by both the ArchivesSpace backend and frontend, so avoid any Rails-isms here.

Constant Summary collapse

LOCALES =
{
  'en' => 'eng',
  'es' => 'spa',
  'fr' => 'fre',
  'ja' => 'jpn',
  'de' => 'ger'
}.sort_by {
TRANSLATE_CACHE_LIMIT =
8192
ENUM_SEPARATOR =
"\0"
@@translate_cache =
initialize_cache

Class Method Summary collapse

Class Method Details

.build_cache_key(args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'common/aspace_i18n_enumeration_support.rb', line 93

def self.build_cache_key(args)
  key = args[0]

  # We'll only cache the trivial case: simple string lookup.  No placeholders.
  cacheable = (args[1] &&
               args[1].class == Hash &&
               args[1].keys.sort == [:default, :raise])

  if !cacheable
    return nil
  end

  if key.is_a?(Hash) && key.has_key?(:enumeration)
    self.build_enumeration_key(key)
  else
    key
  end
end

.build_enumeration_key(key) ⇒ Object



112
113
114
115
# File 'common/aspace_i18n_enumeration_support.rb', line 112

def self.build_enumeration_key(key)
  # Null character to cope with enumeration values containing dots.  Eugh.
  ['enumerations', key[:enumeration], key[:value]].join(ENUM_SEPARATOR)
end

.initialize_cacheObject

can be used to reset the cache



10
11
12
# File 'common/aspace_i18n_enumeration_support.rb', line 10

def self.initialize_cache
  @@translate_cache = java.util.concurrent.ConcurrentHashMap.new(TRANSLATE_CACHE_LIMIT)
end

.prioritize_plugins!Object



40
41
42
# File 'common/aspace_i18n.rb', line 40

def self.prioritize_plugins!
  self.load_path = self.load_path.reject { |p| p.match /plugins\// } + self.load_path.reject { |p| !p.match /plugins\// }
end

.supported_localesObject



36
37
38
# File 'common/aspace_i18n.rb', line 36

def self.supported_locales
  LOCALES
end

.t_raw(*args) ⇒ Object

Caching layer. See #t_raw_uncached for the real action.



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
# File 'common/aspace_i18n_enumeration_support.rb', line 19

def self.t_raw(*args)
  cache_key = build_cache_key(args)

  if !cache_key
    # This entry is uncacheable.  Perhaps because it contains placeholders to
    # be substituted.

    return self.t_raw_uncached(*args)
  end

  # prefix the generated cache key with the locale set per request cycle
  cache_key = "#{config.locale}.#{cache_key}"
  entry = nil
  cache_hit = false

  if (entry = @@translate_cache[cache_key])
    cache_hit = true
  else
    begin
      entry = [:result, self.t_raw_uncached(*args)]
    rescue
      entry = [:error, $!]
    end
  end

  if !cache_hit && @@translate_cache.size < TRANSLATE_CACHE_LIMIT
    # This won't strictly prevent the cache growing a bit larger than the
    # limit since the size check isn't performed under a lock, but should stop
    # unbounded growth due to bugs in code.
    @@translate_cache[cache_key] = entry
  end

  # TEST MODE
  # if cache_hit && entry[0] == :result
  #   raise args.inspect unless entry[1] == self.t_raw_uncached(*args)
  # end

  if entry[0] == :result
    return entry[1]
  else
    # Error
    raise entry[1]
  end
end

.t_raw_uncached(*args) ⇒ Object



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
91
# File 'common/aspace_i18n_enumeration_support.rb', line 64

def self.t_raw_uncached(*args)
  key = args[0]

  default = if args[1].is_a?(String)
              args[1]
            else
              (args[1] || {}).fetch(:default, "")
            end

  # String
  if key && key.is_a?(String) && key.end_with?(".")
    return default
  end

  # Hash / Enumeration Value
  if key && key.is_a?(Hash) && key.has_key?(:enumeration)
    backend  = config.backend
    locale   = config.locale

    translation = backend.send(:lookup, locale, self.build_enumeration_key(key), [], {:separator => ENUM_SEPARATOR}) || default

    if translation && !translation.empty?
      return translation
    end
  end

  self.translate(key, **args[1])
end