Class: Jekyll::L10n::LocaleNameResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-l10n/utils/locale_name_resolver.rb

Overview

Resolves a canonical locale code into a human-readable display name using ISO 639-1 language names and ISO 3166-1 country names from the locale gem.

Supported input forms:

  • Language-only: "es""Spanish"
  • Language + country subtag (POSIX underscore form): "es_ES""Spanish (Spain)", "pt_BR""Portuguese (Brazil)"

Falls back to the raw code segment when the locale gem cannot resolve a language or region code. This is a defensive-only path — every locale that reaches this class from plugin-managed configuration has already passed Constants::LOCALE_PATTERN validation, so unknown codes are not expected in normal operation.

Examples:

Language-only codes

Jekyll::L10n::LocaleNameResolver.resolve('es')  # => "Spanish"
Jekyll::L10n::LocaleNameResolver.resolve('en')  # => "English"
Jekyll::L10n::LocaleNameResolver.resolve('pt')  # => "Portuguese"

Country-subtag codes

Jekyll::L10n::LocaleNameResolver.resolve('es_ES')  # => "Spanish (Spain)"
Jekyll::L10n::LocaleNameResolver.resolve('pt_BR')  # => "Portuguese (Brazil)"

Fallback for unresolvable codes (defensive)

Jekyll::L10n::LocaleNameResolver.resolve('xx')     # => "xx"
Jekyll::L10n::LocaleNameResolver.resolve('xx_YY')  # => "xx (YY)"

Class Method Summary collapse

Class Method Details

.resolve(locale) ⇒ String

Resolve a canonical locale code to a human-readable display name.

Parameters:

  • locale (String, nil)

    The locale code to resolve (e.g. "es", "es_ES", "pt_BR"). nil or empty string returns an empty string without raising.

Returns:

  • (String)

    The resolved display name, or the raw code segment(s) if the locale gem cannot resolve either part.



41
42
43
44
45
46
47
48
# File 'lib/jekyll-l10n/utils/locale_name_resolver.rb', line 41

def self.resolve(locale)
  language_code, country_code = locale.to_s.split('_', 2)
  language_name = Locale::Info.get_language(language_code)&.name || language_code
  return language_name unless country_code

  country_name = Locale::Info.get_region(country_code)&.name || country_code
  "#{language_name} (#{country_name})"
end