Module: Jekyll::TranslateKey

Defined in:
lib/jekyll-open-sdg-plugins/translate_key.rb

Instance Method Summary collapse

Instance Method Details

#t(key) ⇒ Object

Takes a translation key and returns a translated string according to the language of the current page. Or if none is found, returns the original key.



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
# File 'lib/jekyll-open-sdg-plugins/translate_key.rb', line 8

def t(key)
  translations = @context.registers[:site].data['translations']
  language = @context.environments.first["page"]['language']

  # Keep track of the last thing we drilled to.
  drilled = translations[language]

  # Keep track of how many levels we have drilled.
  levels_drilled = 0
  levels = key.split('.')

  # Loop through each level.
  levels.each do |level|

    # If we have drilled down to a scalar value too soon, abort.
    break if drilled.class != Hash

    if drilled.has_key? level
      # If we find something, continue drilling.
      drilled = drilled[level]
      levels_drilled += 1
    end

  end

  # If we didn't drill the right number of levels, return the
  # original string.
  if levels.length != levels_drilled
    return key
  end

  # Otherwise we must have drilled all they way.
  return drilled
end