Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selected locale is not taking into accoun in keycloak.v3 account theme #24082

Closed
2 tasks done
jrpedrianes opened this issue Oct 18, 2023 · 1 comment · Fixed by #24736
Closed
2 tasks done

Selected locale is not taking into accoun in keycloak.v3 account theme #24082

jrpedrianes opened this issue Oct 18, 2023 · 1 comment · Fixed by #24736
Assignees

Comments

@jrpedrianes
Copy link

jrpedrianes commented Oct 18, 2023

Before reporting an issue

Area

account/ui

Describe the bug

When account3 feature and the declarative-user-profile feature is enabled, the keycloak.v3 account theme is configured and a locale attribute is created, if the user changes the "select locale" field, the UI remains in English instead of being rendered in the selected language as it is when using the keycloak.v2 account theme.

Version

22.0.4

Expected behavior

The UI pages must be rendered in the selected user locale.

Actual behavior

Always the page is rendered in English

How to Reproduce?

  1. Start a keycloak server with declarative-user-profile and account3 enabled.
  2. In a Realm, turn on 'User Profile Enabled` setting
  3. Select the keycloak.v3 as Account theme for the selected Realm.
  4. Create the following User attributes in the selected realm.
{
  "attributes": [
    {
      "displayName": "${username}",
      "name": "username",
      "permissions": {
        "edit": [
          "admin"
        ],
        "view": [
          "admin"
        ]
      },
      "validations": {
        "length": {
          "max": "255",
          "min": "3"
        },
        "up-username-not-idn-homograph": {},
        "username-prohibited-characters": {}
      }
    },
    {
      "displayName": "${email}",
      "name": "email",
      "permissions": {
        "edit": [
          "admin",
          "user"
        ],
        "view": [
          "admin",
          "user"
        ]
      },
      "required": {
        "roles": [
          "user"
        ]
      },
      "validations": {
        "email": {},
        "length": {
          "max": "255"
        }
      }
    },
    {
      "displayName": "${firstName}",
      "name": "firstName",
      "permissions": {
        "edit": [
          "admin",
          "user"
        ],
        "view": [
          "admin",
          "user"
        ]
      },
      "required": {
        "roles": [
          "user"
        ]
      },
      "validations": {
        "length": {
          "max": "255"
        },
        "person-name-prohibited-characters": {}
      }
    },
    {
      "displayName": "${lastName}",
      "name": "lastName",
      "permissions": {
        "edit": [
          "admin",
          "user"
        ],
        "view": [
          "admin",
          "user"
        ]
      },
      "required": {
        "roles": [
          "user"
        ]
      },
      "validations": {
        "length": {
          "max": "255"
        },
        "person-name-prohibited-characters": {}
      }
    },
    {
      "displayName": "${locale}",
      "name": "locale",
      "permissions": {
        "edit": [
          "admin",
          "user"
        ],
        "view": [
          "admin",
          "user"
        ]
      }
    }
  ]
}
  1. Go to the realm account, ie: http://localhost:9080/realms/master/account
  2. Try to change the locale. The locale is changed but the UI remains in English.

Anything else?

Reviewing the code a possible solution could be:

  1. Create an 18next language detector to use the document language: https://github.com/keycloak/keycloak/blob/main/js/apps/account-ui/src/i18n.ts#L14
export const keycloakLanguageDetector: LanguageDetectorModule = {
  type: "languageDetector",

  detect(): string | undefined {
    return document.documentElement.lang;
  },
};
  1. Register 'keycloakLanguageDetector' in 18n: https://github.com/keycloak/keycloak/blob/main/js/apps/account-ui/src/i18n.ts#L28
i18n.use(keycloakLanguageDetector);
  1. Reload the UI if the locale is changed: https://github.com/keycloak/keycloak/blob/main/js/apps/account-ui/src/personal-info/PersonalInfo.tsx#L60
      const userLocale = user.attributes["locale"] && user.attributes["locale"][0];
      const currentLocale = keycloakLanguageDetector.detect();
      if (userLocale !== currentLocale) {
        setTimeout(() => window.location.reload(), 100);
      }
  1. Modify the index.ftl to use the user locale: https://github.com/keycloak/keycloak/blob/main/js/apps/account-ui/pom.xml#L67
<replacement>
  <token>lang="en"</token>
  <value>lang="${locale}"</value>
</replacement>

With these changes, when the UI is loaded the HTML language is set as the user locale. Then this value is fetched by the i18next to load the corresponding files. If a user changes the default locale, then the UI is reloaded to detect the changes.

Another option could be use the KEYCLOAK_LOCALE cookie, but it is marked as httpOnly so we can't use it.

@jrpedrianes jrpedrianes added kind/bug Categorizes a PR related to a bug status/triage labels Oct 18, 2023
@ssilvert
Copy link
Contributor

This is closely related to #23699. Assigning to @jonkoops.

@edewit edewit assigned edewit and unassigned jonkoops Nov 14, 2023
@edewit edewit mentioned this issue Nov 14, 2023
edewit added a commit to edewit/keycloak that referenced this issue Nov 14, 2023
fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
edewit added a commit to edewit/keycloak that referenced this issue Nov 21, 2023
fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
jonkoops pushed a commit to edewit/keycloak that referenced this issue Nov 23, 2023
fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
edewit added a commit that referenced this issue Dec 12, 2023
* Switch locale

fixes: #24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

* PR comments

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
srose pushed a commit to srose/keycloak that referenced this issue Dec 20, 2023
* Switch locale

fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

* PR comments

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
kamontat pushed a commit to kamontat/keycloak that referenced this issue Jan 20, 2024
* Switch locale

fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

* PR comments

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
Signed-off-by: Kamontat Chantrachirathumrong <14089557+kamontat@users.noreply.github.com>
ShefeeqPM pushed a commit to ShefeeqPM/keycloak that referenced this issue Jan 27, 2024
* Switch locale

fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

* PR comments

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
Signed-off-by: ShefeeqPM <86718986+ShefeeqPM@users.noreply.github.com>
ahus1 pushed a commit to ahus1/keycloak that referenced this issue Mar 22, 2024
* Switch locale

fixes: keycloak#24082
Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

* PR comments

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants