Module: Jekyll::TranslateMetadataFieldOption

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

Instance Method Summary collapse

Instance Method Details

#translate_metadata_field_option(field_name, value) ⇒ Object

Takes a metadata field (machine name) and option (value) and returns a translated string according to the language of the current page, suitable for displaying to the public.

By contrast to TranslateMetadataField above, this is for translating the options of multiple-choice schema fields. But similar to TranslateMetadataField, this looks for a “translation_key” property on the option in the schema.

Temporary backwards compatibility: If the check fails, it falls back to whatever is in a “name” property in the schema.

Parameters


field_name : string

The machine name of a metadata field.

value : string

The 'value' of the option to use.


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

def (field_name, value)

  # 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

  # Fall back to the value itself.
  to_translate = value

  # Look for the 'translation_key' property from the schema.
  if field && field['field'].has_key?('options')
    option = field['field']['options'].select {|x| x['value'] == value}
    if option
      option = option.first()
      if option.has_key?('translation_key')
        to_translate = option['translation_key']
      else
        to_translate = option['name']
      end
    end
  end

  return opensdg_translate_key(to_translate, t, lang)

end