Class UrlLocaleResolver

java.lang.Object
com.oorian.i18n.UrlLocaleResolver
All Implemented Interfaces:
LocaleResolver

public class UrlLocaleResolver extends Object implements LocaleResolver
Resolves the locale from the first segment of the URL path.

Supports URL patterns such as /en/dashboard, /fr-FR/products, or /de/settings. The first path segment after the context path is compared against the set of configured supported locales.

This resolver requires at least one supported locale to be configured via addSupportedLocale(Locale). If no supported locales are configured, the resolver always returns null. This prevents false matches where non-locale path segments (like /api or /admin) could be misinterpreted.

Usage:


 UrlLocaleResolver resolver = new UrlLocaleResolver();
 resolver.addSupportedLocale(Locale.US);
 resolver.addSupportedLocale(Locale.GERMANY);
 resolver.addSupportedLocale(Locale.FRANCE);
 resolver.addSupportedLocale(Locale.JAPAN);

 // /en-US/dashboard  → Locale.US
 // /de/settings      → Locale.GERMANY
 // /api/users        → null (not a supported locale)
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • UrlLocaleResolver

      public UrlLocaleResolver()
      Creates a new URL-based locale resolver with no supported locales.

      At least one supported locale must be added via addSupportedLocale(Locale) before this resolver will match any URLs.

  • Method Details

    • addSupportedLocale

      public UrlLocaleResolver addSupportedLocale(Locale locale)
      Adds a locale to the set of supported locales.

      The locale's language tag is used for matching against URL path segments. Both the full tag (e.g., en-us) and the language-only portion (e.g., en) are registered for flexible matching.

      Parameters:
      locale - the locale to support
      Returns:
      this resolver for method chaining
    • resolveLocale

      public Locale resolveLocale(OorianHttpRequest request)
      Resolves the locale from the first path segment of the request URL.

      Extracts the first path segment after the context path and attempts to match it against the configured supported locales. Returns null if no supported locales are configured or if the path segment does not match any supported locale.

      Specified by:
      resolveLocale in interface LocaleResolver
      Parameters:
      request - the current HTTP request
      Returns:
      the matched locale, or null if the URL does not contain a locale prefix