Module: Jekyll::TranslateKey
- Defined in:
- lib/jekyll-open-sdg-plugins/translate_key.rb
Instance Method Summary collapse
-
#t(key) ⇒ Object
Takes a translation key and returns a translated string according to the language of the current page.
Instance Method Details
#t(key) ⇒ Object
Takes a translation key and returns a translated string according to the language of the current page. Or if none is found, returns the original key.
9 10 11 12 13 14 15 16 17 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 |
# File 'lib/jekyll-open-sdg-plugins/translate_key.rb', line 9 def t(key) # Determine the language of the current page. site = @context.registers[:site] translations = site.data['translations'] language = @context.environments.first["page"]['language'] translated = opensdg_translate_key(key, translations, language) # Stop now if we do not have a string. unless translated.is_a?(String) return translated end # Also look for references to site configuration within the translation, # as "%" parameters. translated = translated.gsub(/%([a-zA-Z0-9_.]+)/) do |match| # Special case for trailing dots. trailing_dot = match.end_with?('.') key_candidate = match.delete_suffix('.').delete_prefix('%') # Check to see if it is a site configuration. translated_word = opensdg_parse_site_config(key_candidate, site) # Check to see if the value of the site config may have been # a translation key itself. But do a safety check to avoid # infinite loops. if translated_word != key translated_word = opensdg_translate_key(translated_word, translations, language) end # Replace the word if something changed. if key_candidate != translated_word match = translated_word # Making sure to add back any trailing dots. if trailing_dot match = match + '.' end end match end return translated end |