Change language using keycloak

In my project . we use keycloak for login but function change language in login screen is not working . so what i can do to fix it .
image

Hi,

If you select a locale on the Keycloak login form, its value then comes to Jmix the “locale” claim. For example, you may set this claim value into the “locale” user property.

The user class (a slightly modified example from the oidc readme page):

import io.jmix.oidc.user.DefaultJmixOidcUser;

public class MyUser extends DefaultJmixOidcUser {

    private String locale;

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }
}

The user mapper:

@Component
public class MyOidcUserMapper extends BaseOidcUserMapper<MyUser> {

    private ClaimsRolesMapper claimsRolesMapper;

    public MyOidcUserMapper(ClaimsRolesMapper claimsRolesMapper) {
        this.claimsRolesMapper = claimsRolesMapper;
    }

    @Override
    protected MyUser initJmixUser(OidcUser oidcUser) {
        return new MyUser();
    }

    @Override
    protected void populateUserAttributes(OidcUser oidcUser, MyUser jmixUser) {
        //locale selected in the Keycloak login page will be in the "locale" claim
        jmixUser.setLocale((String) oidcUser.getClaims().get("locale"));
    }

    @Override
    protected void populateUserAuthorities(OidcUser oidcUser, MyUser jmixUser) {
        Collection<? extends GrantedAuthority> authorities = claimsRolesMapper.toGrantedAuthorities(oidcUser.getClaims());
        jmixUser.setAuthorities(authorities);
    }
}

After that the locale value will be in the user attribute. Then you need to create an implementation of the AuthenticationLocaleResolver interface that will set a locale based on user attribute:

import com.google.common.base.Strings;
import io.jmix.core.LocaleResolver;
import io.jmix.core.security.AuthenticationLocaleResolver;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * Locale resolver analyzes the "locale" property of the currently authenticated user and if this property is filled,
 * the Locale is evaluated based on its value.
 */
@Component
public class MyOidcLocaleResolver implements AuthenticationLocaleResolver {
    @Override
    public boolean supports(Authentication authentication) {
        return authentication instanceof OAuth2AuthenticationToken;
    }

    @Override
    public Locale getLocale(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof MyUser user &&
                !Strings.isNullOrEmpty(user.getLocale())) {
            String locale = user.getLocale();
            return LocaleResolver.resolve(locale);
        }
        return null;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

See the sample project:
oidc-locale.zip (173.7 KB)