Module: Jekyll::TranslateMetadataField

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

Instance Method Summary collapse

Instance Method Details

#translate_metadata_field(field_name) ⇒ Object

Takes a metadata field (machine name) and returns a translated string according to the language of the current page, suitable for displaying to the public. It gets this string by looking in the site’s “schema” for a “translation_key” property, and running that through the opensdg_translate_key() helper function.

Temporary backwards compatibility: If the check fails, it falls back to checking for a translation in the ‘metadata_fields’ translation group.

More backwards compatibility: If all of the above fails, it falls back to using whatever is in a “label” property in the schema.

Parameters


field_name : string

The machine name of a metadata field.


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

def (field_name)

  # Determine the language of the current page.
  t = @context.registers[:site].data['translations']
  lang = @context.environments.first['page']['language']
  # Get the schema.
  schema = @context.registers[:site].data['schema']

  # Find the field.
  field = schema.select {|x| x['name'] == field_name }
  if field
    field = field.first()
  end

  to_translate = ''
  # First choice - use the 'translation_key' property from the schema.
  if field && field['field'].has_key?('translation_key')
    to_translate = field['field']['translation_key']
  # Next choice - try the 'metadata_fields' translation group.
  elsif t[lang].has_key?('metadata_fields') && t[lang]['metadata_fields'].has_key?(field_name)
    to_translate = 'metadata_fields.' + field_name
  # Next choice - use the 'label' from the schema.
  elsif field && field['field'].has_key?('label')
    to_translate = field['field']['label']
  # Last choice - just use the field name.
  else
    to_translate = field_name
  end

  return opensdg_translate_key(to_translate, t, lang)
end