Classes List

Symfony\Component\Intl\Data\Util\LocaleScanner

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
<?php

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\Intl\Data\Util;

/** * Scans a directory with data files for locales. * * The name of each file with the extension ".txt" is considered, if it "looks" * like a locale: * * - the name must start with two letters; * - the two letters may optionally be followed by an underscore and any * sequence of other symbols. * * For example, "de" and "de_DE" are considered to be locales. "root" and "meta" * are not. * * @author Bernhard Schussek <bschussek@gmail.com> * * @internal */
class LocaleScanner
{
    /** * Returns all locales found in the given directory. * * @param string $sourceDir The directory with ICU files * * @return array An array of locales. The result also contains locales that * are in fact just aliases for other locales. Use * {@link scanAliases()} to determine which of the locales * are aliases */
    public function scanLocales($sourceDir)
    {
        $locales = glob($sourceDir.'/*.txt');

        // Remove file extension and sort
        array_walk($localesfunction (&$locale) { $locale = basename($locale'.txt'); });

        // Remove non-locales
        $locales = array_filter($localesfunction ($locale) {
            return preg_match('/^[a-z]{2}(_.+)?$/'$locale);
        });

        sort($locales);

        return $locales;
    }

    /** * Returns all locale aliases found in the given directory. * * @param string $sourceDir The directory with ICU files * * @return array An array with the locale aliases as keys and the aliased * locales as values */
    public function scanAliases($sourceDir)
    {
        $locales = $this->scanLocales($sourceDir);
        $aliases = array();

        // Delete locales that are no aliases
        foreach ($locales as $locale) {
            $content = file_get_contents($sourceDir.'/'.$locale.'.txt');

            // Aliases contain the text "%%ALIAS" followed by the aliased locale
            if (preg_match('/"%%ALIAS"\{"([^"]+)"\}/'$content$matches)) {
                $aliases[$locale] = $matches[1];
            }
        }

        return $aliases;
    }
}