You are here

public function KeycloakService::getI18nMapping in Keycloak OpenID Connect 8

Return the Keycloak i18n locale code mapping.

This mapping is required for some languages, as Drupal uses IETF script codes, while Keycloak may use IETF region codes for its localization.

Parameters

bool $reverse: (optional) Whether to use Drupal language IDs as keys (FALSE), or Keycloak locales (TRUE). Defaults to FALSE.

bool $include_enabled: (optional) Whether to include non-mapped, but in Drupal enabled languages. If no mapping is set for an enabled language, the Drupal language ID will be used as Keycloak locale. (Which most often matches the Keycloak locales by default.) Defaults to TRUE.

Return value

array Associative array with i18n locale mappings with keys as specified with the $reverse parameter and an associative locale map array as value, having the following keys:

  • language_id: Drupal language ID.
  • locale: Keycloak locale.
  • label: Localized human-readable language label.

Overrides KeycloakServiceInterface::getI18nMapping

File

src/Service/KeycloakService.php, line 217

Class

KeycloakService
Keycloak service.

Namespace

Drupal\keycloak\Service

Code

public function getI18nMapping($reverse = FALSE, $include_enabled = TRUE) {
  $mappings = [];
  $languages = $this->languageManager
    ->getLanguages();
  if (empty($languages)) {
    return $mappings;
  }
  $configured = $this->config
    ->get('settings.keycloak_i18n_mapping');

  // The stored mapping is an unkeyed list of associative arrays
  // with 'langcode' and 'target' as keys. Transform it to an assoc
  // array of 'langcode' => 'target'.
  $kc_mappings = [];
  if (!empty($configured)) {
    foreach ($configured as $mapping) {
      $kc_mappings[$mapping['langcode']] = $mapping['target'];
    }
  }

  // Create the i18n locale mapping information.
  foreach ($languages as $langcode => $language) {
    if (empty($kc_mappings[$langcode]) && !$include_enabled) {
      continue;
    }
    $mapping = [
      'language_id' => $langcode,
      'locale' => !empty($kc_mappings[$langcode]) ? $kc_mappings[$langcode] : $langcode,
      'label' => $language
        ->getName(),
    ];
    $mappings[$reverse ? $mapping['locale'] : $langcode] = $mapping;
  }
  return $mappings;
}