jekyll-open-sdg-plugins 1.0.0.rc21 → 1.2.0.pre.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +16 -16
- data/.github/workflows/test-pull-requests.yml +17 -0
- data/.gitignore +6 -4
- data/Makefile +33 -0
- data/README.md +7 -7
- data/jekyll-open-sdg-plugins.gemspec +18 -17
- data/lib/jekyll-open-sdg-plugins.rb +16 -13
- data/lib/jekyll-open-sdg-plugins/create_goals.rb +75 -75
- data/lib/jekyll-open-sdg-plugins/create_indicators.rb +118 -67
- data/lib/jekyll-open-sdg-plugins/create_pages.rb +127 -99
- data/lib/jekyll-open-sdg-plugins/fetch_remote_data.rb +187 -167
- data/lib/jekyll-open-sdg-plugins/helpers.rb +132 -66
- data/lib/jekyll-open-sdg-plugins/schema-indicator-config.json +516 -0
- data/lib/jekyll-open-sdg-plugins/schema-site-config.json +1153 -0
- data/lib/jekyll-open-sdg-plugins/sdg_variables.rb +439 -410
- data/lib/jekyll-open-sdg-plugins/search_index.rb +102 -92
- data/lib/jekyll-open-sdg-plugins/site_configuration.rb +43 -0
- data/lib/jekyll-open-sdg-plugins/translate_date.rb +98 -67
- data/lib/jekyll-open-sdg-plugins/translate_key.rb +20 -20
- data/lib/jekyll-open-sdg-plugins/translate_metadata_field.rb +111 -111
- data/lib/jekyll-open-sdg-plugins/validate_indicator_config.rb +52 -0
- data/lib/jekyll-open-sdg-plugins/validate_site_config.rb +34 -0
- data/lib/jekyll-open-sdg-plugins/version.rb +3 -3
- data/tests/Gemfile +7 -0
- data/tests/_config.yml +147 -0
- metadata +26 -4
@@ -1,92 +1,102 @@
|
|
1
|
-
require "jekyll"
|
2
|
-
require_relative "helpers"
|
3
|
-
|
4
|
-
module JekyllOpenSdgPlugins
|
5
|
-
class SearchIndex < Jekyll::Generator
|
6
|
-
safe true
|
7
|
-
priority :lowest
|
8
|
-
# NOTE: This must be executed **after** the sdg_variables.rb file, since it
|
9
|
-
# relies heavily on the variables created there.
|
10
|
-
|
11
|
-
# Helper function to prepare content for the search index.
|
12
|
-
def prepare_content(site, content)
|
13
|
-
|
14
|
-
# Handle nil content.
|
15
|
-
if !content
|
16
|
-
content = ''
|
17
|
-
end
|
18
|
-
|
19
|
-
#
|
20
|
-
|
21
|
-
|
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
|
-
item['url'] =
|
77
|
-
|
78
|
-
|
79
|
-
item['
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
1
|
+
require "jekyll"
|
2
|
+
require_relative "helpers"
|
3
|
+
|
4
|
+
module JekyllOpenSdgPlugins
|
5
|
+
class SearchIndex < Jekyll::Generator
|
6
|
+
safe true
|
7
|
+
priority :lowest
|
8
|
+
# NOTE: This must be executed **after** the sdg_variables.rb file, since it
|
9
|
+
# relies heavily on the variables created there.
|
10
|
+
|
11
|
+
# Helper function to prepare content for the search index.
|
12
|
+
def prepare_content(site, content, language)
|
13
|
+
|
14
|
+
# Handle nil content.
|
15
|
+
if !content
|
16
|
+
content = ''
|
17
|
+
end
|
18
|
+
|
19
|
+
# Strip whitespace.
|
20
|
+
content = content.strip
|
21
|
+
# Translate if needed.
|
22
|
+
content = opensdg_translate_key(content, site.data['translations'], language)
|
23
|
+
# Next compile any Markdown.
|
24
|
+
converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
|
25
|
+
content = converter.convert(content)
|
26
|
+
# Now strip any HTML.
|
27
|
+
content = content.gsub(/<\/?[^>]*>/, "")
|
28
|
+
return content
|
29
|
+
end
|
30
|
+
|
31
|
+
def generate(site)
|
32
|
+
|
33
|
+
# Generate a hash of items to include in the search index.
|
34
|
+
search_items = {}
|
35
|
+
|
36
|
+
site.collections.keys.each do |collection|
|
37
|
+
site.collections[collection].docs.each do |doc|
|
38
|
+
# Do not index configuration forms.
|
39
|
+
if doc.data.has_key?('layout') && doc.data['layout'] == 'config-builder'
|
40
|
+
next
|
41
|
+
end
|
42
|
+
# We segregate the search items by language.
|
43
|
+
language = doc.data['language']
|
44
|
+
if !search_items.has_key? language
|
45
|
+
search_items[language] = {}
|
46
|
+
end
|
47
|
+
# We'll be adding properties to this basic hash.
|
48
|
+
item = {
|
49
|
+
# The 'type' can be used on the front-end to describe a search result.
|
50
|
+
# It is assumed that all the collection names are translated in the
|
51
|
+
# "general" translation group. Eg: general.indicators, general.goals
|
52
|
+
'type' => opensdg_translate_key('general.' + collection, site.data['translations'], language)
|
53
|
+
}
|
54
|
+
if collection == 'indicators'
|
55
|
+
# For indicators, we assign the following properties for each item.
|
56
|
+
# The URL of the page.
|
57
|
+
item['url'] = doc.data['indicator']['url']
|
58
|
+
# For the title, use the indicator name.
|
59
|
+
indicator_label = opensdg_translate_key('general.indicator', site.data['translations'], language)
|
60
|
+
item['title'] = indicator_label + ' ' + doc.data['indicator']['number'] + ' - ' + doc.data['indicator']['name']
|
61
|
+
# For the content, use the 'page_content' field.
|
62
|
+
item['content'] = prepare_content(site, doc.data['indicator']['page_content'], language)
|
63
|
+
# For the id field, use the ID number.
|
64
|
+
item['id'] = doc.data['indicator']['number']
|
65
|
+
# Also index any additional metadata fields.
|
66
|
+
if site.config['search_index_extra_fields']
|
67
|
+
site.config['search_index_extra_fields'].each do |field|
|
68
|
+
if doc.data['indicator'].has_key? field
|
69
|
+
item[field] = prepare_content(site, doc.data['indicator'][field], language)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
elsif collection == 'goals'
|
74
|
+
# For goals, we assign the following properties for each item.
|
75
|
+
# The URL of the page.
|
76
|
+
item['url'] = doc.data['goal']['url']
|
77
|
+
# For the title we use the goal name.
|
78
|
+
goal_label = opensdg_translate_key('general.goal', site.data['translations'], language)
|
79
|
+
item['title'] = goal_label + ' ' + doc.data['goal']['number'] + ' - ' + doc.data['goal']['name']
|
80
|
+
# For the content, currently nothing here.
|
81
|
+
item['content'] = ''
|
82
|
+
# For the id field, use the ID number.
|
83
|
+
item['id'] = doc.data['goal']['number']
|
84
|
+
else
|
85
|
+
# Otherwise assume it is a normal Jekyll document.
|
86
|
+
item['url'] = File.join(doc.data['baseurl'], doc.url)
|
87
|
+
item['title'] = prepare_content(site, doc.data['title'], language)
|
88
|
+
item['content'] = prepare_content(site, doc.content, language)
|
89
|
+
item['id'] = ''
|
90
|
+
end
|
91
|
+
|
92
|
+
# Save this item in the language-specific search index.
|
93
|
+
search_items[language][item['url']] = item
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Stow the data for later use in Jekyll templates.
|
98
|
+
site.data['search_items'] = search_items
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "jekyll"
|
2
|
+
require_relative "helpers"
|
3
|
+
|
4
|
+
module JekyllOpenSdgPlugins
|
5
|
+
class SiteConfiguration < Jekyll::Generator
|
6
|
+
safe true
|
7
|
+
priority :highest
|
8
|
+
|
9
|
+
# This looks for site configuration in the data directory, and if found, copies it to
|
10
|
+
# the "site" object, as if it had been in _config.yml. It looks in "site_config" for
|
11
|
+
# configuration to move. In addition, if jekyll.environment or site.environment is
|
12
|
+
# specifically "production", then it also moves data from "site_config_prod".
|
13
|
+
#
|
14
|
+
# This allows you to keep all OpenSDG-specific config out of _config.yml, and instead
|
15
|
+
# place it in site_config and/or site_config_prod in your data directory.
|
16
|
+
def generate(site)
|
17
|
+
|
18
|
+
if site.data.has_key?('site_config')
|
19
|
+
hash_to_hash(site.data['site_config'], site.config)
|
20
|
+
end
|
21
|
+
|
22
|
+
production = false
|
23
|
+
if Jekyll.env == 'production'
|
24
|
+
production = true
|
25
|
+
end
|
26
|
+
if site.config.has_key?('environment') && site.config['environment'] == 'production'
|
27
|
+
production = true
|
28
|
+
end
|
29
|
+
|
30
|
+
if production && site.data.has_key?('site_config_prod')
|
31
|
+
hash_to_hash(site.data['site_config_prod'], site.config)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
# Copy properties from a hash onto another hash.
|
37
|
+
def hash_to_hash(hash_from, hash_to)
|
38
|
+
hash_from.each do |key, value|
|
39
|
+
hash_to[key] = value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,67 +1,98 @@
|
|
1
|
-
require "jekyll"
|
2
|
-
require_relative "helpers"
|
3
|
-
|
4
|
-
module Jekyll
|
5
|
-
module TranslateDate
|
6
|
-
|
7
|
-
# Takes a raw Jekyll date and returns a translated string according to the
|
8
|
-
# language of the current page and a date format given.
|
9
|
-
def t_date(date, format_type)
|
10
|
-
|
11
|
-
# Determine the language of the current page.
|
12
|
-
config = @context.registers[:site].config
|
13
|
-
translations = @context.registers[:site].data['translations']
|
14
|
-
language = @context.environments.first['page']['language']
|
15
|
-
|
16
|
-
# Try to find the specified date format in the site config. It needs to be
|
17
|
-
# something like this, assuming the "format_type" param is "standard":
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
1
|
+
require "jekyll"
|
2
|
+
require_relative "helpers"
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
module TranslateDate
|
6
|
+
|
7
|
+
# Takes a raw Jekyll date and returns a translated string according to the
|
8
|
+
# language of the current page and a date format given.
|
9
|
+
def t_date(date, format_type)
|
10
|
+
|
11
|
+
# Determine the language of the current page.
|
12
|
+
config = @context.registers[:site].config
|
13
|
+
translations = @context.registers[:site].data['translations']
|
14
|
+
language = @context.environments.first['page']['language']
|
15
|
+
|
16
|
+
# Try to find the specified date format in the site config. It needs to be
|
17
|
+
# something like this, assuming the "format_type" param is "standard":
|
18
|
+
#
|
19
|
+
# date_formats:
|
20
|
+
# - type: standard
|
21
|
+
# language: en
|
22
|
+
# format: "%b %d, %Y"
|
23
|
+
# - type: standard
|
24
|
+
# language: es
|
25
|
+
# format: "%d de %b de %Y"
|
26
|
+
#
|
27
|
+
# However the following deprecated structure is also supported:
|
28
|
+
#
|
29
|
+
# date_formats:
|
30
|
+
# standard:
|
31
|
+
# en: "%b %d, %Y"
|
32
|
+
# es: "%d de %b de %Y"
|
33
|
+
# etc...
|
34
|
+
date_format = '%b %d, %Y'
|
35
|
+
if config.has_key?('date_formats')
|
36
|
+
|
37
|
+
# @deprecated start
|
38
|
+
# In a deprecated form of date_formats, it was a nested hash keyed first
|
39
|
+
# by the format type and then by the language.
|
40
|
+
if config['date_formats'].is_a?(Hash) && config['date_formats'].has_key?(format_type)
|
41
|
+
if config['date_formats'][format_type].has_key?(language)
|
42
|
+
date_format = config['date_formats'][format_type][language]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
# @deprecated end
|
46
|
+
|
47
|
+
# In the current form of data_formats, it is an array of hashes, each
|
48
|
+
# containing "type", "language", and "format" keys.
|
49
|
+
if config['date_formats'].is_a?(Array)
|
50
|
+
date_format_config = config['date_formats'].find {|d| d['type'] == format_type && d['language'] == language }
|
51
|
+
if date_format_config
|
52
|
+
date_format = date_format_config['format']
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
# Support timestamps.
|
59
|
+
if date.is_a? Integer
|
60
|
+
date = Time.at(date)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Convert the date into English.
|
64
|
+
english = date.strftime(date_format)
|
65
|
+
|
66
|
+
# Now "tokenize" that date by spaces.
|
67
|
+
parts = english.split(' ')
|
68
|
+
|
69
|
+
translated_parts = []
|
70
|
+
parts.each do |part|
|
71
|
+
# Special case: see if we need to remove a comma from the end.
|
72
|
+
removed_comma = false
|
73
|
+
if part.end_with? ','
|
74
|
+
part = part.delete_suffix(',')
|
75
|
+
removed_comma = true
|
76
|
+
end
|
77
|
+
# Look for a translation in the "calendar" translation group.
|
78
|
+
key = 'calendar.' + part
|
79
|
+
translated_part = opensdg_translate_key(key, translations, language)
|
80
|
+
# If it changed from the key, that means it was a working key.
|
81
|
+
if key != translated_part
|
82
|
+
part = translated_part
|
83
|
+
end
|
84
|
+
|
85
|
+
# Add back the comma if needed.
|
86
|
+
if removed_comma
|
87
|
+
part = part + ','
|
88
|
+
end
|
89
|
+
|
90
|
+
translated_parts.push(part)
|
91
|
+
end
|
92
|
+
|
93
|
+
return translated_parts.join(' ')
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
Liquid::Template.register_filter(Jekyll::TranslateDate)
|
@@ -1,20 +1,20 @@
|
|
1
|
-
require "jekyll"
|
2
|
-
require_relative "helpers"
|
3
|
-
|
4
|
-
module Jekyll
|
5
|
-
module TranslateKey
|
6
|
-
# Takes a translation key and returns a translated string according to the
|
7
|
-
# language of the current page. Or if none is found, returns the original
|
8
|
-
# key.
|
9
|
-
def t(key)
|
10
|
-
|
11
|
-
# Determine the language of the current page.
|
12
|
-
translations = @context.registers[:site].data['translations']
|
13
|
-
language = @context.environments.first["page"]['language']
|
14
|
-
|
15
|
-
return opensdg_translate_key(key, translations, language)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
Liquid::Template.register_filter(Jekyll::TranslateKey)
|
1
|
+
require "jekyll"
|
2
|
+
require_relative "helpers"
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
module TranslateKey
|
6
|
+
# Takes a translation key and returns a translated string according to the
|
7
|
+
# language of the current page. Or if none is found, returns the original
|
8
|
+
# key.
|
9
|
+
def t(key)
|
10
|
+
|
11
|
+
# Determine the language of the current page.
|
12
|
+
translations = @context.registers[:site].data['translations']
|
13
|
+
language = @context.environments.first["page"]['language']
|
14
|
+
|
15
|
+
return opensdg_translate_key(key, translations, language)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Liquid::Template.register_filter(Jekyll::TranslateKey)
|
@@ -1,111 +1,111 @@
|
|
1
|
-
require "jekyll"
|
2
|
-
require_relative "helpers"
|
3
|
-
|
4
|
-
module Jekyll
|
5
|
-
module TranslateMetadataField
|
6
|
-
# Takes a metadata field (machine name) and returns a translated string
|
7
|
-
# according to the language of the current page, suitable for displaying to
|
8
|
-
# the public. It gets this string by looking in the site's "schema" for a
|
9
|
-
# "translation_key" property, and running that through the
|
10
|
-
# opensdg_translate_key() helper function.
|
11
|
-
#
|
12
|
-
# Temporary backwards compatibility: If the check fails, it falls back to
|
13
|
-
# checking for a translation in the 'metadata_fields' translation group.
|
14
|
-
#
|
15
|
-
# More backwards compatibility: If all of the above fails, it falls back to
|
16
|
-
# using whatever is in a "label" property in the schema.
|
17
|
-
#
|
18
|
-
# Parameters
|
19
|
-
# ----------
|
20
|
-
# field_name : string
|
21
|
-
# The machine name of a metadata field.
|
22
|
-
def translate_metadata_field(field_name)
|
23
|
-
|
24
|
-
# Determine the language of the current page.
|
25
|
-
t = @context.registers[:site].data['translations']
|
26
|
-
lang = @context.environments.first['page']['language']
|
27
|
-
# Get the schema.
|
28
|
-
schema = @context.registers[:site].data['schema']
|
29
|
-
|
30
|
-
# Find the field.
|
31
|
-
field = schema.select {|x| x['name'] == field_name }
|
32
|
-
if field
|
33
|
-
field = field.first()
|
34
|
-
end
|
35
|
-
|
36
|
-
to_translate = ''
|
37
|
-
# First choice - use the 'translation_key' property from the schema.
|
38
|
-
if field && field['field'].has_key?('translation_key')
|
39
|
-
to_translate = field['field']['translation_key']
|
40
|
-
# Next choice - try the 'metadata_fields' translation group.
|
41
|
-
elsif t[lang].has_key?('metadata_fields') && t[lang]['metadata_fields'].has_key?(field_name)
|
42
|
-
to_translate = 'metadata_fields.' + field_name
|
43
|
-
# Next choice - use the 'label' from the schema.
|
44
|
-
elsif field && field['field'].has_key?('label')
|
45
|
-
to_translate = field['field']['label']
|
46
|
-
# Last choice - just use the field name.
|
47
|
-
else
|
48
|
-
to_translate = field_name
|
49
|
-
end
|
50
|
-
|
51
|
-
return opensdg_translate_key(to_translate, t, lang)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
module TranslateMetadataFieldOption
|
56
|
-
# Takes a metadata field (machine name) and option (value) and returns a
|
57
|
-
# translated string according to the language of the current page, suitable
|
58
|
-
# for displaying to the public.
|
59
|
-
#
|
60
|
-
# By contrast to TranslateMetadataField above, this is for translating the
|
61
|
-
# options of multiple-choice schema fields. But similar to
|
62
|
-
# TranslateMetadataField, this looks for a "translation_key" property on
|
63
|
-
# the option in the schema.
|
64
|
-
#
|
65
|
-
# Temporary backwards compatibility: If the check fails, it falls back to
|
66
|
-
# whatever is in a "name" property in the schema.
|
67
|
-
#
|
68
|
-
# Parameters
|
69
|
-
# ----------
|
70
|
-
# field_name : string
|
71
|
-
# The machine name of a metadata field.
|
72
|
-
# value : string
|
73
|
-
# The 'value' of the option to use.
|
74
|
-
def translate_metadata_field_option(field_name, value)
|
75
|
-
|
76
|
-
# Determine the language of the current page.
|
77
|
-
t = @context.registers[:site].data['translations']
|
78
|
-
lang = @context.environments.first['page']['language']
|
79
|
-
# Get the schema.
|
80
|
-
schema = @context.registers[:site].data['schema']
|
81
|
-
|
82
|
-
# Find the field.
|
83
|
-
field = schema.select {|x| x['name'] == field_name}
|
84
|
-
if field
|
85
|
-
field = field.first()
|
86
|
-
end
|
87
|
-
|
88
|
-
# Fall back to the value itself.
|
89
|
-
to_translate = value
|
90
|
-
|
91
|
-
# Look for the 'translation_key' property from the schema.
|
92
|
-
if field && field['field'].has_key?('options')
|
93
|
-
option = field['field']['options'].select {|x| x['value'] == value}
|
94
|
-
if option
|
95
|
-
option = option.first()
|
96
|
-
if option.has_key?('translation_key')
|
97
|
-
to_translate = option['translation_key']
|
98
|
-
else
|
99
|
-
to_translate = option['name']
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
return opensdg_translate_key(to_translate, t, lang)
|
105
|
-
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
Liquid::Template.register_filter(Jekyll::TranslateMetadataField)
|
111
|
-
Liquid::Template.register_filter(Jekyll::TranslateMetadataFieldOption)
|
1
|
+
require "jekyll"
|
2
|
+
require_relative "helpers"
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
module TranslateMetadataField
|
6
|
+
# Takes a metadata field (machine name) and returns a translated string
|
7
|
+
# according to the language of the current page, suitable for displaying to
|
8
|
+
# the public. It gets this string by looking in the site's "schema" for a
|
9
|
+
# "translation_key" property, and running that through the
|
10
|
+
# opensdg_translate_key() helper function.
|
11
|
+
#
|
12
|
+
# Temporary backwards compatibility: If the check fails, it falls back to
|
13
|
+
# checking for a translation in the 'metadata_fields' translation group.
|
14
|
+
#
|
15
|
+
# More backwards compatibility: If all of the above fails, it falls back to
|
16
|
+
# using whatever is in a "label" property in the schema.
|
17
|
+
#
|
18
|
+
# Parameters
|
19
|
+
# ----------
|
20
|
+
# field_name : string
|
21
|
+
# The machine name of a metadata field.
|
22
|
+
def translate_metadata_field(field_name)
|
23
|
+
|
24
|
+
# Determine the language of the current page.
|
25
|
+
t = @context.registers[:site].data['translations']
|
26
|
+
lang = @context.environments.first['page']['language']
|
27
|
+
# Get the schema.
|
28
|
+
schema = @context.registers[:site].data['schema']
|
29
|
+
|
30
|
+
# Find the field.
|
31
|
+
field = schema.select {|x| x['name'] == field_name }
|
32
|
+
if field
|
33
|
+
field = field.first()
|
34
|
+
end
|
35
|
+
|
36
|
+
to_translate = ''
|
37
|
+
# First choice - use the 'translation_key' property from the schema.
|
38
|
+
if field && field['field'].has_key?('translation_key')
|
39
|
+
to_translate = field['field']['translation_key']
|
40
|
+
# Next choice - try the 'metadata_fields' translation group.
|
41
|
+
elsif t[lang].has_key?('metadata_fields') && t[lang]['metadata_fields'].has_key?(field_name)
|
42
|
+
to_translate = 'metadata_fields.' + field_name
|
43
|
+
# Next choice - use the 'label' from the schema.
|
44
|
+
elsif field && field['field'].has_key?('label')
|
45
|
+
to_translate = field['field']['label']
|
46
|
+
# Last choice - just use the field name.
|
47
|
+
else
|
48
|
+
to_translate = field_name
|
49
|
+
end
|
50
|
+
|
51
|
+
return opensdg_translate_key(to_translate, t, lang)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module TranslateMetadataFieldOption
|
56
|
+
# Takes a metadata field (machine name) and option (value) and returns a
|
57
|
+
# translated string according to the language of the current page, suitable
|
58
|
+
# for displaying to the public.
|
59
|
+
#
|
60
|
+
# By contrast to TranslateMetadataField above, this is for translating the
|
61
|
+
# options of multiple-choice schema fields. But similar to
|
62
|
+
# TranslateMetadataField, this looks for a "translation_key" property on
|
63
|
+
# the option in the schema.
|
64
|
+
#
|
65
|
+
# Temporary backwards compatibility: If the check fails, it falls back to
|
66
|
+
# whatever is in a "name" property in the schema.
|
67
|
+
#
|
68
|
+
# Parameters
|
69
|
+
# ----------
|
70
|
+
# field_name : string
|
71
|
+
# The machine name of a metadata field.
|
72
|
+
# value : string
|
73
|
+
# The 'value' of the option to use.
|
74
|
+
def translate_metadata_field_option(field_name, value)
|
75
|
+
|
76
|
+
# Determine the language of the current page.
|
77
|
+
t = @context.registers[:site].data['translations']
|
78
|
+
lang = @context.environments.first['page']['language']
|
79
|
+
# Get the schema.
|
80
|
+
schema = @context.registers[:site].data['schema']
|
81
|
+
|
82
|
+
# Find the field.
|
83
|
+
field = schema.select {|x| x['name'] == field_name}
|
84
|
+
if field
|
85
|
+
field = field.first()
|
86
|
+
end
|
87
|
+
|
88
|
+
# Fall back to the value itself.
|
89
|
+
to_translate = value
|
90
|
+
|
91
|
+
# Look for the 'translation_key' property from the schema.
|
92
|
+
if field && field['field'].has_key?('options')
|
93
|
+
option = field['field']['options'].select {|x| x['value'] == value}
|
94
|
+
if option
|
95
|
+
option = option.first()
|
96
|
+
if option.has_key?('translation_key')
|
97
|
+
to_translate = option['translation_key']
|
98
|
+
else
|
99
|
+
to_translate = option['name']
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
return opensdg_translate_key(to_translate, t, lang)
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
Liquid::Template.register_filter(Jekyll::TranslateMetadataField)
|
111
|
+
Liquid::Template.register_filter(Jekyll::TranslateMetadataFieldOption)
|