Class: Jekyll::UJTranslationUrlTag

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Jekyll::UJPowertools::VariableResolver
Defined in:
lib/tags/translation_url.rb

Instance Method Summary collapse

Methods included from Jekyll::UJPowertools::VariableResolver

#is_quoted?, #parse_arguments, #parse_options, #resolve_input, #resolve_variable

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ UJTranslationUrlTag

Returns a new instance of UJTranslationUrlTag.



8
9
10
11
# File 'lib/tags/translation_url.rb', line 8

def initialize(tag_name, markup, tokens)
  super
  @markup = markup.strip
end

Instance Method Details

#render(context) ⇒ Object



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
# File 'lib/tags/translation_url.rb', line 13

def render(context)
  # Parse arguments using helper
  parts = parse_arguments(@markup)
  
  # Return root if no arguments
  return '/' if parts.empty? || parts[0].nil?
  
  # Resolve both arguments using helper (handles literals and variables)
  language_code = resolve_input(context, parts[0])
  url_path = resolve_input(context, parts[1]) || '/'

  # Get site and translation config from context
  site = context.registers[:site]
  return '/' unless site

  translation_config = site.config['translation'] || {}
  default_language = translation_config['default'] || 'en'
  available_languages = translation_config['languages'] || [default_language]
  
  # Validate that the requested language is available
  unless available_languages.include?(language_code)
    # Fall back to default language if requested language is not available
    language_code = default_language
  end
  
  # Normalize the URL path
  normalized_path = normalize_path(url_path)
  
  # Generate the language-specific URL
  generate_language_url(language_code, normalized_path, default_language)
end