Esempio n. 1
0
 /**
  * Load and merge all phrases from language packs by specified code
  *
  * Takes into account inheritance between language packs
  * Returns associative array where key is phrase in the source code and value is its translation
  *
  * @param string $languageCode
  * @return array
  */
 public function getDictionary($languageCode)
 {
     $languages = [];
     $declarations = $this->dir->search('*/*/language.xml');
     foreach ($declarations as $file) {
         $xmlSource = $this->dir->readFile($file);
         $languageConfig = $this->configFactory->create(['source' => $xmlSource]);
         $this->packList[$languageConfig->getVendor()][$languageConfig->getPackage()] = $languageConfig;
         if ($languageConfig->getCode() === $languageCode) {
             $languages[] = $languageConfig;
         }
     }
     // Collect the inherited packages with meta-information of sorting
     $packs = [];
     foreach ($languages as $languageConfig) {
         $this->collectInheritedPacks($languageConfig, $packs);
     }
     uasort($packs, [$this, 'sortInherited']);
     // Merge all packages of translation to one dictionary
     $result = [];
     foreach ($packs as $packInfo) {
         /** @var Config $languageConfig */
         $languageConfig = $packInfo['language'];
         $dictionary = $this->readPackCsv($languageConfig->getVendor(), $languageConfig->getPackage());
         $result = array_merge($result, $dictionary);
     }
     return $result;
 }
Esempio n. 2
0
 /**
  * Load and merge all phrases from language packs by specified code
  *
  * Takes into account inheritance between language packs
  * Returns associative array where key is phrase in the source code and value is its translation
  *
  * @param string $languageCode
  * @return array
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function getDictionary($languageCode)
 {
     $languages = [];
     $this->paths = $this->componentRegistrar->getPaths(ComponentRegistrar::LANGUAGE);
     foreach ($this->paths as $path) {
         $directoryRead = $this->directoryReadFactory->create($path);
         if ($directoryRead->isExist('language.xml')) {
             $xmlSource = $directoryRead->readFile('language.xml');
             try {
                 $languageConfig = $this->configFactory->create(['source' => $xmlSource]);
             } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
                 throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase("Invalid XML in file %1:\n%2", [$path . '/language.xml', $e->getMessage()]), $e);
             }
             $this->packList[$languageConfig->getVendor()][$languageConfig->getPackage()] = $languageConfig;
             if ($languageConfig->getCode() === $languageCode) {
                 $languages[] = $languageConfig;
             }
         }
     }
     // Collect the inherited packages with meta-information of sorting
     $packs = [];
     foreach ($languages as $languageConfig) {
         $this->collectInheritedPacks($languageConfig, $packs);
     }
     uasort($packs, [$this, 'sortInherited']);
     // Merge all packages of translation to one dictionary
     $result = [];
     foreach ($packs as $packInfo) {
         /** @var Config $languageConfig */
         $languageConfig = $packInfo['language'];
         $dictionary = $this->readPackCsv($languageConfig->getVendor(), $languageConfig->getPackage());
         $result = array_merge($result, $dictionary);
     }
     return $result;
 }