module Jekyll::TranslateMetadataFieldOption

Public Instance Methods

translate_metadata_field_option(field_name, value) click to toggle source

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