Is there a built-in mechanism to propagate the selected login language on redirect to the client

GIVEN

  • a realm R with l10n enabled
  • a user successfully authenticates for a specific client (ie with an redirect URL)
  • the user selects a language (or just uses the default)

WHEN
the redirect to the client happens i would like to have that (changed/effective) kc_locale value added to the redirection URL so that the client is made aware of it.

is that possible OOTB with KC 26.0.5 ?
if yes: how ?
if not: how can i do this with custom logic/spi impls ?

I’m just answering this question because it follows the format with the GIVEN-WHEN-THEN syntax :grinning:

The standard profile scope [1] includes a mapper with the user locale attribute (or you can also create a specific scope for that).
Therefore, once the user is authenticated (I’m guessing you are implementing OIDC), the id_token will include the locale claim.

[1] Final: OpenID Connect Core 1.0 incorporating errata set 2

2 Likes

Thanks for the pointer!

As far as i understand it now, the locale switching mechanism works like so:

After a successful login, the selected locale gets persisted in the user’s locale attribute in KC. The default profile mapper then just reads it from the user attributes.

I realize now, that it’s not working for us b/c there is also a very minimal custom User Federation at work which

  1. is read-only and hence does not persist anything
  2. does not map the user’s locale

Given this setup i poked around a little more and came to the following solution:

I write a custom protocol mapper that does simply this:

      var locale = keycloakSession.getContext().resolveLocale(user);
      token.setLocale(locale.getLanguage());

is that the right way to go ?
Or is there a better way (w/o having to store the data in some DB) ?