Contao\CoreBundle\EventListener\LocaleListener::createWithLocales PHP Метод

createWithLocales() публичный статический Метод

Creates a new instance with the installed languages.
public static createWithLocales ( string $defaultLocale, string $rootDir ) : static
$defaultLocale string
$rootDir string
Результат static
    public static function createWithLocales($defaultLocale, $rootDir)
    {
        $dirs = [__DIR__ . '/../Resources/contao/languages'];
        // app/Resources/contao/languages
        if (is_dir($rootDir . '/Resources/contao/languages')) {
            $dirs[] = $rootDir . '/Resources/contao/languages';
        }
        $finder = Finder::create()->directories()->depth(0)->in($dirs);
        $languages = array_values(array_map(function (SplFileInfo $file) {
            return $file->getFilename();
        }, iterator_to_array($finder)));
        // The default locale must be the first supported language (see contao/core#6533)
        array_unshift($languages, $defaultLocale);
        return new static(array_unique($languages));
    }

Usage Example

Пример #1
0
 /**
  * Tests the createWithLocales() method.
  */
 public function testCreateWithLocales()
 {
     $listener = LocaleListener::createWithLocales('de', $this->getRootDir() . '/app');
     $this->assertInstanceOf('Contao\\CoreBundle\\EventListener\\LocaleListener', $listener);
     $reflection = new \ReflectionClass($listener);
     $property = $reflection->getProperty('availableLocales');
     $property->setAccessible(true);
     $locales = $property->getValue($listener);
     $this->assertContains('de', $locales);
     $this->assertContains('en', $locales);
     $this->assertContains('it', $locales);
 }