You are here

public static function Carbon::setLocale in Persian Date for Drupal 8 8.4

Set the current translator locale and indicate if the source locale file exists

Parameters

string $locale:

Return value

bool

1 call to Carbon::setLocale()
Carbon::translator in src/Library/Carbon/Carbon.php
Initialize the translator instance if necessary.

File

src/Library/Carbon/Carbon.php, line 1151

Class

Carbon
A simple API extension for DateTime

Namespace

Drupal\persian_date\Library\Carbon

Code

public static function setLocale($locale) {
  $locale = preg_replace_callback('/\\b([a-z]{2})[-_](?:([a-z]{4})[-_])?([a-z]{2})\\b/', function ($matches) {
    return $matches[1] . '_' . (!empty($matches[2]) ? ucfirst($matches[2]) . '_' : '') . strtoupper($matches[3]);
  }, strtolower($locale));
  if (file_exists($filename = __DIR__ . '/Lang/' . $locale . '.php')) {
    $translator = static::translator();
    $translator
      ->setLocale($locale);
    if ($translator instanceof Translator) {

      // Ensure the locale has been loaded.
      $translator
        ->addResource('array', require $filename, $locale);
    }
    return true;
  }
  return false;
}