Neos\Flow\I18n\LocaleCollection::findBestMatchingLocale PHP Method

findBestMatchingLocale() public method

Returns Locale object which represents one of locales installed and which is most similar to the "template" Locale object given as parameter.
public findBestMatchingLocale ( Locale $locale ) : mixed
$locale Locale The "template" locale to be matched
return mixed Existing Locale instance on success, NULL on failure
    public function findBestMatchingLocale(Locale $locale)
    {
        $localeIdentifier = (string) $locale;
        if (isset($this->localeCollection[$localeIdentifier])) {
            return $this->localeCollection[$localeIdentifier];
        }
        $parentLocaleIdentifier = $localeIdentifier;
        do {
            // Remove the last (most specific) part of the locale tag
            $parentLocaleIdentifier = substr($parentLocaleIdentifier, 0, (int) strrpos($parentLocaleIdentifier, '_'));
            if (isset($this->localeCollection[$parentLocaleIdentifier])) {
                return $this->localeCollection[$parentLocaleIdentifier];
            }
        } while (strrpos($parentLocaleIdentifier, '_') !== false);
        return null;
    }

Usage Example

 /**
  * @test
  */
 public function bestMatchingLocalesAreFoundCorrectly()
 {
     foreach ($this->locales as $locale) {
         $this->localeCollection->addLocale($locale);
     }
     $this->assertEquals($this->locales[1], $this->localeCollection->findBestMatchingLocale($this->locales[1]));
     $this->assertEquals($this->locales[1], $this->localeCollection->findBestMatchingLocale(new I18n\Locale('pl_PL_DVORAK')));
     $this->assertNull($this->localeCollection->findBestMatchingLocale(new I18n\Locale('sv')));
 }
All Usage Examples Of Neos\Flow\I18n\LocaleCollection::findBestMatchingLocale