You are here

public static function LingotekLocale::generateLingotek2Drupal in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  2. 4.0.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  3. 3.0.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  4. 3.1.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  5. 3.2.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  6. 3.3.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  7. 3.4.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  8. 3.5.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  9. 3.6.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  10. 3.7.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
  11. 3.8.x src/LingotekLocale.php \Drupal\lingotek\LingotekLocale::generateLingotek2Drupal()
2 calls to LingotekLocale::generateLingotek2Drupal()
LingotekDashboardController::endpoint in src/Controller/LingotekDashboardController.php
LingotekLocaleTest::testGenerateLingotek2Drupal in src/Tests/LingotekLocaleTest.php

File

src/LingotekLocale.php, line 283
Defines LingotekLocale class.

Class

LingotekLocale
A utility class for Lingotek translation.

Namespace

Drupal\lingotek

Code

public static function generateLingotek2Drupal($lingotek_locale) {
  $drupal_language_code = strtolower(str_replace("_", "-", $lingotek_locale));

  // standard conversion
  if ($hyphen_index = strpos($drupal_language_code, '-') > 0) {
    $drupal_general_code = substr($drupal_language_code, 0, strpos($drupal_language_code, '-'));
  }
  else {

    // We try with the given language, if we didn't have an hyphen (e.g. ar).
    $drupal_general_code = $drupal_language_code;
  }

  // check enabled list
  $enabled_languages = \Drupal::languageManager()
    ->getLanguages();
  $enabled_codes = array_keys($enabled_languages);
  if (!in_array($drupal_general_code, $enabled_codes)) {
    return $drupal_general_code;
  }
  else {
    if (!in_array($drupal_language_code, $enabled_codes)) {
      return $drupal_language_code;
    }
    else {
      return $drupal_language_code . rand(10, 99);
    }
  }
}