Class RootDirectoryBasedLocaleContextResolver

java.lang.Object
de.gustavblass.fsu.fmi.roombooking.configuration.RootDirectoryBasedLocaleContextResolver
All Implemented Interfaces:
org.springframework.web.servlet.LocaleResolver

public class RootDirectoryBasedLocaleContextResolver extends Object implements org.springframework.web.servlet.LocaleResolver
Determines the Locale to be used for translation depending on the URL path.
See Also:
  • Field Details

    • supportedLocaleCodes

      @NonNull private final @NonNull Map<String,Locale> supportedLocaleCodes
      All language codes that the application has already been translated to.
  • Constructor Details

    • RootDirectoryBasedLocaleContextResolver

      public RootDirectoryBasedLocaleContextResolver()
  • Method Details

    • getRootDirectory

      @Contract(pure=true) @NonNull private static @NonNull String getRootDirectory(@NonNull @NonNull jakarta.servlet.http.HttpServletRequest request)

      Determines the top-level directory denoted by the URL path of the request. For example,

      • /en/benutzer/@jason returns en
      • /räume returns räume
      • /admin/benutzer returns admin.
      Parameters:
      request - The request whose URL path's root directory shall be determined.
      Returns:
      The top-level directory of the URL path.
    • resolveLocale

      @Contract(pure=true) @NonNull public @NonNull Locale resolveLocale(@NonNull @NonNull jakarta.servlet.http.HttpServletRequest request)

      Determines the appropriate Locale that shall be used to translate the resource requested. The locale is determined based on the presence of a language code as the root directory of the URL path requested.

      If no language code is present or if the language is not supported, the default locale is used instead.

      All headers, cookies, URL parameters etc. are completely ignored.

      Specified by:
      resolveLocale in interface org.springframework.web.servlet.LocaleResolver
      Parameters:
      request - The request for which the correct language shall be determined.
      Returns:
      The language appropriate for the request.
    • setLocale

      @Contract(pure=true) public void setLocale(@NonNull @NonNull jakarta.servlet.http.HttpServletRequest request, @Nullable @Nullable jakarta.servlet.http.HttpServletResponse response, @Nullable @Nullable Locale locale) throws UnsupportedOperationException

      Warning

      Always throws UnsupportedOperationException! This is because the locale appropriate for a request depends only on the URL path and not on anything else.

      See resolveLocale(HttpServletRequest).

      Specified by:
      setLocale in interface org.springframework.web.servlet.LocaleResolver
      Parameters:
      request - Ignored.
      response - Ignored.
      locale - Ignored.
      Throws:
      UnsupportedOperationException - Always!
    • setDefaultLocale

      public void setDefaultLocale(@NonNull @NonNull Locale locale)
      Parameters:
      locale - The new standard language if no other language is determined as appropriate.