Neos\Flow\I18n\Service::getLocalizedFilename PHP Méthode

getLocalizedFilename() public méthode

Searching is done for the current locale if no $locale parameter is provided. The search is done according to the configured fallback rule. If parameter $strict is provided, searching is done only for the provided / current locale (without searching of files localized for more generic locales). If no localized version of file is found, $filepath is returned without any change.
See also: Configuration::setFallbackRule()
public getLocalizedFilename ( string $pathAndFilename, Locale $locale = null, boolean $strict = false ) : array
$pathAndFilename string Path to the file
$locale Locale Desired locale of localized file
$strict boolean Whether to match only provided locale (TRUE) or search for best-matching locale (FALSE)
Résultat array Path to the localized file (or $filename when no localized file was found) and the matched locale
    public function getLocalizedFilename($pathAndFilename, Locale $locale = null, $strict = false)
    {
        if ($locale === null) {
            $locale = $this->configuration->getCurrentLocale();
        }
        $filename = basename($pathAndFilename);
        if (strpos($filename, '.') !== false) {
            $dotPosition = strrpos($pathAndFilename, '.');
            $pathAndFilenameWithoutExtension = substr($pathAndFilename, 0, $dotPosition);
            $extension = substr($pathAndFilename, $dotPosition);
        } else {
            $pathAndFilenameWithoutExtension = $pathAndFilename;
            $extension = '';
        }
        if ($strict === true) {
            $possibleLocalizedFilename = $pathAndFilenameWithoutExtension . '.' . (string) $locale . $extension;
            if (file_exists($possibleLocalizedFilename)) {
                return [$possibleLocalizedFilename, $locale];
            }
        } else {
            foreach ($this->getLocaleChain($locale) as $localeIdentifier => $locale) {
                $possibleLocalizedFilename = $pathAndFilenameWithoutExtension . '.' . $localeIdentifier . $extension;
                if (file_exists($possibleLocalizedFilename)) {
                    return [$possibleLocalizedFilename, $locale];
                }
            }
        }
        return [$pathAndFilename, $locale];
    }

Usage Example

 /**
  * Render the URI to the resource. The filename is used from child content.
  *
  * @param string $path The location of the resource, can be either a path relative to the Public resource directory of the package or a resource://... URI
  * @param string $package Target package key. If not set, the current package key will be used
  * @param PersistentResource $resource If specified, this resource object is used instead of the path and package information
  * @param boolean $localize Whether resource localization should be attempted or not
  * @return string The absolute URI to the resource
  * @throws InvalidVariableException
  * @api
  */
 public function render($path = null, $package = null, PersistentResource $resource = null, $localize = true)
 {
     if ($resource !== null) {
         $uri = $this->resourceManager->getPublicPersistentResourceUri($resource);
         if ($uri === false) {
             $uri = '404-Resource-Not-Found';
         }
     } else {
         if ($path === null) {
             throw new InvalidVariableException('The ResourceViewHelper did neither contain a valuable "resource" nor "path" argument.', 1353512742);
         }
         if ($package === null) {
             $package = $this->controllerContext->getRequest()->getControllerPackageKey();
         }
         if (strpos($path, 'resource://') === 0) {
             try {
                 list($package, $path) = $this->resourceManager->getPackageAndPathByPublicPath($path);
             } catch (Exception $exception) {
                 throw new InvalidVariableException(sprintf('The specified path "%s" does not point to a public resource.', $path), 1386458851);
             }
         }
         if ($localize === true) {
             $resourcePath = 'resource://' . $package . '/Public/' . $path;
             $localizedResourcePathData = $this->i18nService->getLocalizedFilename($resourcePath);
             $matches = array();
             if (preg_match('#resource://([^/]+)/Public/(.*)#', current($localizedResourcePathData), $matches) === 1) {
                 $package = $matches[1];
                 $path = $matches[2];
             }
         }
         $uri = $this->resourceManager->getPublicPackageResourceUri($package, $path);
     }
     return $uri;
 }
All Usage Examples Of Neos\Flow\I18n\Service::getLocalizedFilename