Module: Jekyll::IndicatorName

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

Instance Method Summary collapse

Instance Method Details

#get_indicator_name(inid) ⇒ Object

Takes an indicator ID (dot-delimited or dash-delimited) and returns the translated indicator name (according to the current language). This lookup is as forgiving as possible, to make sure that something is always there.

The order of preference in the lookup is:

  1. “indicator_name_national” in translated metadata - subfolder approach

  2. “indicator_name_national” in translated metadata - translation key approach

  3. If the default language, “indicator_name_national” in non-translated metadata

  4. If a global indicator, translated global indicator name

  5. “indicator_name” in translated metadata - subfolder approach

  6. “indicator_name” in translated metadata - translation key approach

  7. “indicator_name” in non-translated metadata

  8. Finally, fall back to the indicator ID



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
63
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jekyll-open-sdg-plugins/get_indicator_name.rb', line 22

def get_indicator_name(inid)

  # Safety code - abort now if id is nil.
  if inid.nil?
    return ""
  end

  # Also make sure it is a string, and otherwise just return it.
  if not inid.is_a? String
    return inid
  end

  # More safety code - abort now if inid is empty.
  if inid.empty?
    return ""
  end

  # Normalize around dash-delimited inids.
  inid = inid.gsub('.', '-')

  # Some variables to help our lookups later.
  page = @context.environments.first['page']
  language = page['language']
  languages = @context.registers[:site].config['languages']
  data = @context.registers[:site].data
  translations = data['translations']
  meta = data['meta'][inid]

  # The metadata fields that we'll seek, first "override" then "default".
  override_field = 'indicator_name_national'
  default_field = 'indicator_name'

  name = false

  # 1. Is there a subfolder translation of the override field?
  if meta and meta.has_key? language
    if !name and meta[language].has_key? override_field
      name = meta[language][field]
    end
  end

  # 2. Is the override field actually a "translation key"?
  if !name
    if meta and meta.has_key? override_field
      untranslated = meta[override_field]
      translated = opensdg_translate_key(untranslated, translations, language)
      if untranslated != translated
        # If the opensdg_translate_key() function returned something else,
        # that means it was an actual "translation key".
        name = translated
      end
    end
  end

  # 3. If this is the default language, use the non-translated override
  # field, if available.
  if !name
    if language == languages[0]
      if meta and meta.has_key? override_field
        name = meta[override_field]
      end
    end
  end

  # 4. Is this a global indicator with a translation?
  if !name
    # For backwards compatibility, look for both dot and dash-delimited keys.
    inid_dots = inid.gsub('-', '.')
    if translations.has_key? language
      if translations[language].has_key? 'global_indicators'
        if translations[language]['global_indicators'].has_key? inid
          name = translations[language]['global_indicators'][inid]['title']
        elsif translations[language]['global_indicators'].has_key? inid_dots
          name = translations[language]['global_indicators'][inid_dots]['title']
        end
      end
    end
  end

  # 5. Is there a subfolder translation of the default field?
  if !name
    if meta and meta.has_key? language
      if !name and meta[language].has_key? default_field
        name = meta[language][default_field]
      end
    end
  end

  # 6. Is the default field actually a "translation key"?
  if !name
    if meta and meta.has_key? default_field
      untranslated = meta[default_field]
      translated = opensdg_translate_key(untranslated, translations, language)
      if untranslated != translated
        # If the opensdg_translate_key() function returned something else,
        # that means it was an actual "translation key".
        name = translated
      end
    end
  end

  # 7. Use the non-translated default field, if available.
  if !name
    if meta and meta.has_key? default_field
      name = meta[default_field]
    end
  end

  # 8. Still here? Just return the inid.
  if !name
    name = inid
  end

  # Finally return the name with key translation for good measure.
  return opensdg_translate_key(name, translations, language)

end