Translation/Importer/FileImporter.php
public function import(\Symfony\Component\Finder\SplFileInfo $file, $forceUpdate = false, $merge = false)
    {
        $this->skippedKeys = array();
        $imported = 0;
        list($domain, $locale, $extention) = explode('.', $file->getFilename());

        if (!isset($this->loaders[$extention])) {
            throw new \RuntimeException(sprintf('No load found for "%s" format.', $extention));
        }

        $messageCatalogue = $this->loaders[$extention]->load($file->getPathname(), $locale, $domain);

        $translationFile = $this->fileManager->getFor($file->getFilename(), $file->getPath());

        $keys = array();

        foreach ($messageCatalogue->all($domain) as $key => $content) {
            if (!isset($content)) {
                continue; // skip empty translation values
            }

            $normalizedKey = $this->caseInsensitiveInsert ? strtolower($key) : $key;

            if (in_array($normalizedKey, $keys, true)) {
                $this->skippedKeys[] = $key;
                continue; // skip duplicate keys
            }

            $transUnit = $this->storage->getTransUnitByKeyAndDomain($key, $domain);

            if (!($transUnit instanceof TransUnitInterface)) {
                $transUnit = $this->transUnitManager->create($key, $domain);
            }

                $translation = $this->transUnitManager->addTranslation($transUnit, $locale, $content, $translationFile);
                if ($translation instanceof TranslationInterface) {
                    $imported++;
                } else if($forceUpdate) {
                    $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content);
                    if ($translation instanceof Translation) {
                        $translation->setModifiedManually(false);
                    }
                    $imported++;
                } else if($merge) {
                    $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content, false, true);
                    if ($translation instanceof TranslationInterface) {
                        $imported++;
                    }
                }

            $keys[] = $normalizedKey;

            // convert MongoTimestamp objects to time to don't get an error in:
            // Doctrine\ODM\MongoDB\Mapping\Types\TimestampType::convertToDatabaseValue()
            if ($transUnit instanceof TransUnitDocument) {
                $transUnit->convertMongoTimestamp();
            }
        }

        $this->storage->flush();

        // clear only Lexik entities
        foreach (array('file', 'trans_unit', 'translation') as $name) {
            $this->storage->clear($this->storage->getModelClass($name));
        }

        return $imported;
    }
public function import(\Symfony\Component\Finder\SplFileInfo $file, $forceUpdate = false, $merge = false)
    {
        $this->skippedKeys = array();
        $imported = 0;
        list($domain, $locale, $extention) = explode('.', $file->getFilename());

        if (!isset($this->loaders[$extention])) {
            throw new \RuntimeException(sprintf('No load found for "%s" format.', $extention));
        }

        $messageCatalogue = $this->loaders[$extention]->load($file->getPathname(), $locale, $domain);

        $translationFile = $this->fileManager->getFor($file->getFilename(), $file->getPath());

        $keys = array();

        foreach ($messageCatalogue->all($domain) as $key => $content) {
            if (!isset($content)) {
                continue; // skip empty translation values
            }

            $normalizedKey = $this->caseInsensitiveInsert ? strtolower($key) : $key;

            if (in_array($normalizedKey, $keys, true)) {
                $this->skippedKeys[] = $key;
                continue; // skip duplicate keys
            }

            $transUnit = $this->storage->getTransUnitByKeyAndDomain($key, $domain);

            if (!($transUnit instanceof TransUnitInterface)) {
                $transUnit = $this->transUnitManager->create($key, $domain);
            }

                $translation = $this->transUnitManager->addTranslation($transUnit, $locale, $content, $translationFile);
                if ($translation instanceof TranslationInterface) {
                    $imported++;
                } else if($forceUpdate) {
                    $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content);
                    if ($translation instanceof Translation) {
                        $translation->setModifiedManually(false);
                    }
                    $imported++;
                } else if($merge) {
                    $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content, false, true);
                    if ($translation instanceof TranslationInterface) {
                        $imported++;
                    }
                }

            $keys[] = $normalizedKey;

            // convert MongoTimestamp objects to time to don't get an error in:
            // Doctrine\ODM\MongoDB\Mapping\Types\TimestampType::convertToDatabaseValue()
            if ($transUnit instanceof TransUnitDocument) {
                $transUnit->convertMongoTimestamp();
            }
        }

        $this->storage->flush();

        // clear only Lexik entities
        foreach (array('file', 'trans_unit', 'translation') as $name) {
            $this->storage->clear($this->storage->getModelClass($name));
        }

        return $imported;
    }
public function import ( \ Symfony \ Component \ Finder \ SplFileInfo $ file , $ forceUpdate = false , $ merge = false ) { $ this -> skippedKeys = array ( ) ; $ imported = 0 ; list ( $ domain , $ locale , $ extention ) = explode ( '.' , $ file -> getFilename ( ) ) ; if ( ! isset ( $ this -> loaders [ $ extention ] ) ) { throw new \ RuntimeException ( sprintf ( 'No load found for "%s" format.' , $ extention ) ) ; } $ messageCatalogue = $ this -> loaders [ $ extention ] -> load ( $ file -> getPathname ( ) , $ locale , $ domain ) ; $ translationFile = $ this -> fileManager -> getFor ( $ file -> getFilename ( ) , $ file -> getPath ( ) ) ; $ keys = array ( ) ; foreach ( $ messageCatalogue -> all ( $ domain ) as $ key => $ content ) { if ( ! isset ( $ content ) ) { continue ; // skip empty translation values } $ normalizedKey = $ this -> caseInsensitiveInsert ? strtolower ( $ key ) : $ key ; if ( in_array ( $ normalizedKey , $ keys , true ) ) { $ this -> skippedKeys [ ] = $ key ; continue ; // skip duplicate keys } $ transUnit = $ this -> storage -> getTransUnitByKeyAndDomain ( $ key , $ domain ) ; if ( ! ( $ transUnit instanceof TransUnitInterface ) ) { $ transUnit = $ this -> transUnitManager -> create ( $ key , $ domain ) ; } $ translation = $ this -> transUnitManager -> addTranslation ( $ transUnit , $ locale , $ content , $ translationFile ) ; if ( $ translation instanceof TranslationInterface ) { $ imported ++ ; } else if ( $ forceUpdate ) { $ translation = $ this -> transUnitManager -> updateTranslation ( $ transUnit , $ locale , $ content ) ; if ( $ translation instanceof Translation ) { $ translation -> setModifiedManually ( false ) ; } $ imported ++ ; } else if ( $ merge ) { $ translation = $ this -> transUnitManager -> updateTranslation ( $ transUnit , $ locale , $ content , false , true ) ; if ( $ translation instanceof TranslationInterface ) { $ imported ++ ; } } $ keys [ ] = $ normalizedKey ; // convert MongoTimestamp objects to time to don't get an error in: // Doctrine\ODM\MongoDB\Mapping\Types\TimestampType::convertToDatabaseValue() if ( $ transUnit instanceof TransUnitDocument ) { $ transUnit -> convertMongoTimestamp ( ) ; } } $ this -> storage -> flush ( ) ; // clear only Lexik entities foreach ( array ( 'file' , 'trans_unit' , 'translation' ) as $ name ) { $ this -> storage -> clear ( $ this -> storage -> getModelClass ( $ name ) ) ; } return $ imported ; }
Import the given file and return the number of inserted translations.

@param \Symfony\Component\Finder\SplFileInfo $file
@param boolean                               $forceUpdate  force update of the translations
@param boolean                               $merge        merge translations
@return int
Import the given file and return the number of inserted translations .