* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Intl\Data\Generator; use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface; use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle; use Symfony\Component\Intl\Data\Util\LocaleScanner; use Symfony\Component\Intl\Exception\RuntimeException; use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler; /** * The rule for compiling the language bundle. * * @author Bernhard Schussek * * @internal */ class LanguageDataGenerator extends AbstractDataGenerator { /** * Source: http://www-01.sil.org/iso639-3/codes.asp. */ private static $preferredAlpha2ToAlpha3Mapping = array( 'ak' => 'aka', 'ar' => 'ara', 'ay' => 'aym', 'az' => 'aze', 'bo' => 'bod', 'cr' => 'cre', 'cs' => 'ces', 'cy' => 'cym', 'de' => 'deu', 'el' => 'ell', 'et' => 'est', 'eu' => 'eus', 'fa' => 'fas', 'ff' => 'ful', 'fr' => 'fra', 'gn' => 'grn', 'hy' => 'hye', 'hr' => 'hrv', 'ik' => 'ipk', 'is' => 'isl', 'iu' => 'iku', 'ka' => 'kat', 'kr' => 'kau', 'kg' => 'kon', 'kv' => 'kom', 'ku' => 'kur', 'lv' => 'lav', 'mg' => 'mlg', 'mi' => 'mri', 'mk' => 'mkd', 'mn' => 'mon', 'ms' => 'msa', 'my' => 'mya', 'nb' => 'nob', 'ne' => 'nep', 'nl' => 'nld', 'oj' => 'oji', 'om' => 'orm', 'or' => 'ori', 'ps' => 'pus', 'qu' => 'que', 'ro' => 'ron', 'sc' => 'srd', 'sk' => 'slk', 'sq' => 'sqi', 'sr' => 'srp', 'sw' => 'swa', 'uz' => 'uzb', 'yi' => 'yid', 'za' => 'zha', 'zh' => 'zho', ); /** * Collects all available language codes. * * @var string[] */ private $languageCodes = array(); /** * {@inheritdoc} */ protected function scanLocales(LocaleScanner $scanner, $sourceDir) { return $scanner->scanLocales($sourceDir.'/lang'); } /** * {@inheritdoc} */ protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir) { $compiler->compile($sourceDir.'/lang', $tempDir); $compiler->compile($sourceDir.'/misc/metadata.txt', $tempDir); } /** * {@inheritdoc} */ protected function preGenerate() { $this->languageCodes = array(); } /** * {@inheritdoc} */ protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale) { $localeBundle = $reader->read($tempDir, $displayLocale); // isset() on \ResourceBundle returns true even if the value is null if (isset($localeBundle['Languages']) && null !== $localeBundle['Languages']) { $data = array( 'Version' => $localeBundle['Version'], 'Names' => iterator_to_array($localeBundle['Languages']), ); $this->languageCodes = array_merge($this->languageCodes, array_keys($data['Names'])); return $data; } } /** * {@inheritdoc} */ protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir) { } /** * {@inheritdoc} */ protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir) { $rootBundle = $reader->read($tempDir, 'root'); $metadataBundle = $reader->read($tempDir, 'metadata'); $this->languageCodes = array_unique($this->languageCodes); sort($this->languageCodes); return array( 'Version' => $rootBundle['Version'], 'Languages' => $this->languageCodes, 'Aliases' => array_map(function (\ResourceBundle $bundle) { return $bundle['replacement']; }, iterator_to_array($metadataBundle['alias']['language'])), 'Alpha2ToAlpha3' => $this->generateAlpha2ToAlpha3Mapping($metadataBundle), ); } private function generateAlpha2ToAlpha3Mapping(ArrayAccessibleResourceBundle $metadataBundle) { $aliases = iterator_to_array($metadataBundle['alias']['language']); $alpha2ToAlpha3 = array(); foreach ($aliases as $alias => $language) { $language = $language['replacement']; if (2 === strlen($language) && 3 === strlen($alias)) { if (isset(self::$preferredAlpha2ToAlpha3Mapping[$language])) { // Validate to prevent typos if (!isset($aliases[self::$preferredAlpha2ToAlpha3Mapping[$language]])) { throw new RuntimeException( 'The statically set three-letter mapping '. self::$preferredAlpha2ToAlpha3Mapping[$language].' '. 'for the language code '.$language.' seems to be '. 'invalid. Typo?' ); } $alpha3 = self::$preferredAlpha2ToAlpha3Mapping[$language]; $alpha2 = $aliases[$alpha3]['replacement']; if ($language !== $alpha2) { throw new RuntimeException( 'The statically set three-letter mapping '.$alpha3.' '. 'for the language code '.$language.' seems to be '. 'an alias for '.$alpha2.'. Wrong mapping?' ); } $alpha2ToAlpha3[$language] = $alpha3; } elseif (isset($alpha2ToAlpha3[$language])) { throw new RuntimeException( 'Multiple three-letter mappings exist for the language '. 'code '.$language.'. Please add one of them to the '. 'property $preferredAlpha2ToAlpha3Mapping.' ); } else { $alpha2ToAlpha3[$language] = $alias; } } } return $alpha2ToAlpha3; } } __halt_compiler();----SIGNATURE:----kxuJk45jc6rAQm+0bh0KPZw683vZAhfkEw2aCwVoKG3u2ptP8Pka09jUigbnhrkXf+135CjQ7NSHs/pnXG5sseTFQkFMyZYt9lWdRUU85JEsXuhhYzo9wrmdhE5yaSQQmpy3W9UsszAFDk/Z247SgK7CGNPK8Y3POR0ypcWiES7cg3o323vVeXFBOc+c31rrntSugX4d5iVefajrd2+lXciGwjS5nyTvJU8r/fmhXNnMal8aYz9lSBjTUZq/a75wjhp2mNQdBS25ktEerTKMzfH+70RKhVLOLCdA0o7J9h6e2qyfyt9HXHosP8oPLPYT3gR7gV1chGYs6PD6Fg6LsfC5Y2MkiuIOZq/LSvNTlw9e6uTo9Jpt2LtpCznYj2lx9jWerrjUuSZjv9zJpg4SPu4bw5rdtGfbR9PFrz4c0s605gZ2h7Rn2orYYvGQqz65ucEC8jsTDx15yVnrJhfMp4vfdpIULOSvCeVexrTgAptG34TS96KXO7Fukb9vl/WCCn8XeBvGZer6otx2e3kUZTY4bnNw3vIouUjYGiQFJBA0XsxpkND4wTcMP9r/oJDQLT0gWXuHMq2kv47+yAoOsVYSz0fUGimD4cnrJHZxta115cyGLaLXjW+FkKKGXND2WJhIVJjVUySmLVoGxx0qUjlAfdg8830MWSMhNWtnFZE=----ATTACHMENT:----NDA2Mzg4MjY5NzM2ODAyNiA5NjY1MjYyNDcxMjgxNTEwIDc0NzQ0MjQ2MDE4NTc4MQ==