Module: Jekyll::TranslateDate
- Defined in:
- lib/jekyll-open-sdg-plugins/translate_date.rb
Instance Method Summary collapse
-
#t_date(date, format_type) ⇒ Object
Takes a raw Jekyll date and returns a translated string according to the language of the current page and a date format given.
Instance Method Details
#t_date(date, format_type) ⇒ Object
Takes a raw Jekyll date and returns a translated string according to the language of the current page and a date format given.
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 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jekyll-open-sdg-plugins/translate_date.rb', line 9 def t_date(date, format_type) # Determine the language of the current page. config = @context.registers[:site].config translations = @context.registers[:site].data['translations'] language = @context.environments.first['page']['language'] # Try to find the specified date format in the site config. It needs to be # something like this, assuming the "format_type" param is "standard": # # date_formats: # - type: standard # language: en # format: "%b %d, %Y" # - type: standard # language: es # format: "%d de %b de %Y" # # However the following deprecated structure is also supported: # # date_formats: # standard: # en: "%b %d, %Y" # es: "%d de %b de %Y" # etc... date_format = '%b %d, %Y' if config.has_key?('date_formats') # @deprecated start # In a deprecated form of date_formats, it was a nested hash keyed first # by the format type and then by the language. if config['date_formats'].is_a?(Hash) && config['date_formats'].has_key?(format_type) if config['date_formats'][format_type].has_key?(language) date_format = config['date_formats'][format_type][language] end end # @deprecated end # In the current form of data_formats, it is an array of hashes, each # containing "type", "language", and "format" keys. if config['date_formats'].is_a?(Array) date_format_config = config['date_formats'].find {|d| d['type'] == format_type && d['language'] == language } if date_format_config date_format = date_format_config['format'] end end end # Support timestamps. if date.is_a? Integer date = Time.at(date) end # Convert the date into English. english = date.strftime(date_format) # Now "tokenize" that date by spaces. parts = english.split(' ') translated_parts = [] parts.each do |part| # Special case: see if we need to remove a comma from the end. removed_comma = false if part.end_with? ',' part = part.delete_suffix(',') removed_comma = true end # Look for a translation in the "calendar" translation group. key = 'calendar.' + part translated_part = opensdg_translate_key(key, translations, language) # If it changed from the key, that means it was a working key. if key != translated_part part = translated_part end # Add back the comma if needed. if removed_comma part = part + ',' end translated_parts.push(part) end return translated_parts.join(' ') end |