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 in BCP 47 hyphenated form
- Switch between locale variants of the same page
- Build structured locale-switcher menu data
- Validate locales against site configuration (with form normalization)
- Handle edge cases (external URLs, anchors, protocols)
Instance Method Summary collapse
-
#locale_menu(url) ⇒ Array<Hash>
Build the full locale-switcher menu for the current page.
-
#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_menu(url) ⇒ Array<Hash>
Build the full locale-switcher menu for the current page.
Returns one entry per locale available to the current page (via the existing #configured_locales resolution — site-wide with_locales_data.locales config, or a page-level front-matter override — always including English even if not explicitly configured). Each entry carries everything a theme needs to render a menu item with zero locale-specific logic of its own: a display name, a switch-target URL, and whether it's the page's current locale.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/jekyll-l10n/jekyll/url_filter.rb', line 120 def (url) .map do |locale| { 'code' => locale, 'name' => LocaleNameResolver.resolve(locale), 'url' => switch_locale_url(url, locale), 'current' => locale == (current_locale || 'en') } end end |
#locale_url(url, locale = nil) ⇒ String
Generate a locale-prefixed URL for the given URL and locale
Adds a locale prefix in BCP 47 hyphenated form to the given URL. For example, '/about/' becomes '/es-ES/about/' for locale 'es_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
62 63 64 65 66 67 68 |
# File 'lib/jekyll-l10n/jekyll/url_filter.rb', line 62 def locale_url(url, locale = nil) locale ||= current_locale return url if should_skip?(url, locale) url_segment = LocaleUrlFormatter.to_url_segment(locale) "/#{url_segment}#{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-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.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/jekyll-l10n/jekyll/url_filter.rb', line 89 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 |