Class UrlLocaleResolver
- All Implemented Interfaces:
LocaleResolver
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 Summary
ConstructorsConstructorDescriptionCreates a new URL-based locale resolver with no supported locales. -
Method Summary
Modifier and TypeMethodDescriptionaddSupportedLocale(Locale locale) Adds a locale to the set of supported locales.resolveLocale(OorianHttpRequest request) Resolves the locale from the first path segment of the request URL.
-
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
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
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
nullif no supported locales are configured or if the path segment does not match any supported locale.- Specified by:
resolveLocalein interfaceLocaleResolver- Parameters:
request- the current HTTP request- Returns:
- the matched locale, or
nullif the URL does not contain a locale prefix
-