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

(dev/drupal/17) Add Drupal8 support for getUFLocale() #12139

Merged
merged 1 commit into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CRM/Utils/System/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,18 @@ public function postURL($action) {
return $this->url($current_path);
}

/**
* Function to return current language of Drupal8
*
* @return string
*/
public function getCurrentLanguage() {
// Drupal might not be bootstrapped if being called by the REST API.
if (!class_exists('Drupal')) {
return NULL;
}

return \Drupal::languageManager()->getCurrentLanguage()->getId();
}

}
22 changes: 16 additions & 6 deletions CRM/Utils/System/DrupalBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,25 +450,25 @@ public function getUFLocale() {
// return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
// (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
// sometimes for CLI based on order called, this might not be set and/or empty
global $language;
$language = $this->getCurrentLanguage();

if (empty($language)) {
return NULL;
}

if ($language->language == 'zh-hans') {
if ($language == 'zh-hans') {
return 'zh_CN';
}

if ($language->language == 'zh-hant') {
if ($language == 'zh-hant') {
return 'zh_TW';
}

if (preg_match('/^.._..$/', $language->language)) {
return $language->language;
if (preg_match('/^.._..$/', $language)) {
return $language;
}

return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
return CRM_Core_I18n_PseudoConstant::longForShort(substr($language, 0, 2));
}

/**
Expand Down Expand Up @@ -664,4 +664,14 @@ private function parseDrupalSiteNameFromRequest($flagFile = '') {
}
}

/**
* Function to return current language of Drupal
*
* @return string
*/
public function getCurrentLanguage() {
global $language;
return (!empty($language->language)) ? $language->language : $language;
}

}