Module: Jekyll::L10n::UrlFilter
- Defined in:
- lib/jekyll-l10n/jekyll/url_filter.rb
Overview
URL Filter - Liquid filters for locale-aware URL generation
Provides Liquid filters for use in Jekyll templates to generate locale-prefixed URLs. These filters are automatically registered with Liquid during plugin initialization.
This module is designed to be included in Liquid::Template’s filter registry, making the filters available in all Jekyll templates.
Key responsibilities:
-
Generate locale-prefixed URLs for links
-
Switch between locale variants of the same page
-
Validate locales against site configuration
-
Handle edge cases (external URLs, anchors, protocols)
Instance Method Summary collapse
-
#locale_url(url, locale = nil) ⇒ String
Generate a locale-prefixed URL for the given URL and locale.
-
#switch_locale_url(url, target_locale) ⇒ String
Generate a URL for the given locale variant of the current page.
Instance Method Details
#locale_url(url, locale = nil) ⇒ String
Generate a locale-prefixed URL for the given URL and locale
Adds a locale prefix to the given URL, making it suitable for use in locale-specific links. For example, ‘/about/’ becomes ‘/es/about/’ for locale ‘es’. The filter handles various edge cases and only applies the prefix when appropriate.
The method performs several checks to determine if prefixing is appropriate:
-
Invalid locale codes (nil, empty, or ‘en’) are skipped
-
URLs already localized are not prefixed again
-
External URLs, anchors, and relative paths are not modified
52 53 54 55 56 57 |
# File 'lib/jekyll-l10n/jekyll/url_filter.rb', line 52 def locale_url(url, locale = nil) locale ||= current_locale return url if should_skip?(url, locale) "/#{locale}#{url}" end |
#switch_locale_url(url, target_locale) ⇒ String
Generate a URL for the given locale variant of the current page
Switches the current URL to a different locale variant. For example, if the current page is ‘/es/about/’, calling this with ‘fr’ returns ‘/fr/about/’. This is useful for building language switcher menus.
This filter first extracts the base URL (without locale prefix) from the given URL, then applies the target locale using the standard locale_url logic.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-l10n/jekyll/url_filter.rb', line 79 def switch_locale_url(url, target_locale) # Validate target locale is configured return url unless valid_target_locale?(target_locale) # Get base URL without locale prefix base_url_value = base_url(url) # Apply target locale using existing locale_url logic locale_url(base_url_value, target_locale) end |