Backend\Modules\Locale\Engine\Model::importXML PHP Method

importXML() public static method

Import a locale XML file.
public static importXML ( SimpleXMLElement $xml, boolean $overwriteConflicts = false, array $frontendLanguages = null, array $backendLanguages = null, integer $userId = null, integer $date = null ) : array
$xml SimpleXMLElement The locale XML.
$overwriteConflicts boolean Should we overwrite when there is a conflict?
$frontendLanguages array The frontend languages to install locale for.
$backendLanguages array The backend languages to install locale for.
$userId integer Id of the user these translations should be inserted for.
$date integer The date the translation has been inserted.
return array The import statistics
    public static function importXML(\SimpleXMLElement $xml, $overwriteConflicts = false, $frontendLanguages = null, $backendLanguages = null, $userId = null, $date = null)
    {
        $overwriteConflicts = (bool) $overwriteConflicts;
        $statistics = array('total' => 0, 'imported' => 0);
        // set defaults if necessary
        // we can't simply use these right away, because this function is also calls by the installer,
        // which does not have Backend-functions
        if ($frontendLanguages === null) {
            $frontendLanguages = array_keys(BL::getWorkingLanguages());
        }
        if ($backendLanguages === null) {
            $backendLanguages = array_keys(BL::getInterfaceLanguages());
        }
        if ($userId === null) {
            $userId = BackendAuthentication::getUser()->getUserId();
        }
        if ($date === null) {
            $date = BackendModel::getUTCDate();
        }
        // get database instance
        $db = BackendModel::getContainer()->get('database');
        // possible values
        $possibleApplications = array('Frontend', 'Backend');
        $possibleModules = (array) $db->getColumn('SELECT m.name FROM modules AS m');
        // types
        $typesShort = (array) $db->getEnumValues('locale', 'type');
        foreach ($typesShort as $type) {
            $possibleTypes[$type] = self::getTypeName($type);
        }
        // install English translations anyhow, they're fallback
        $possibleLanguages = array('Frontend' => array_unique(array_merge(array('en'), $frontendLanguages)), 'Backend' => array_unique(array_merge(array('en'), $backendLanguages)));
        // current locale items (used to check for conflicts)
        $currentLocale = (array) $db->getColumn('SELECT CONCAT(application, module, type, language, name)
             FROM locale');
        // applications
        foreach ($xml as $application => $modules) {
            // application does not exist
            if (!in_array($application, $possibleApplications)) {
                continue;
            }
            // modules
            foreach ($modules as $module => $items) {
                // module does not exist
                if (!in_array($module, $possibleModules)) {
                    continue;
                }
                // items
                foreach ($items as $item) {
                    // attributes
                    $attributes = $item->attributes();
                    $type = \SpoonFilter::getValue($attributes['type'], $possibleTypes, '');
                    $name = ucfirst(\SpoonFilter::getValue($attributes['name'], null, ''));
                    // missing attributes
                    if ($type == '' || $name == '') {
                        continue;
                    }
                    // real type (shortened)
                    $type = array_search($type, $possibleTypes);
                    // translations
                    foreach ($item->translation as $translation) {
                        // statistics
                        ++$statistics['total'];
                        // attributes
                        $attributes = $translation->attributes();
                        $language = \SpoonFilter::getValue($attributes['language'], $possibleLanguages[$application], '');
                        // language does not exist
                        if ($language == '') {
                            continue;
                        }
                        // the actual translation
                        $translation = (string) $translation;
                        // locale item
                        $locale['user_id'] = $userId;
                        $locale['language'] = $language;
                        $locale['application'] = $application;
                        $locale['module'] = $module;
                        $locale['type'] = $type;
                        $locale['name'] = $name;
                        $locale['value'] = $translation;
                        $locale['edited_on'] = $date;
                        // check if translation does not yet exist, or if the translation can be overridden
                        if (!in_array($application . $module . $type . $language . $name, $currentLocale) || $overwriteConflicts) {
                            $db->execute('INSERT INTO locale (user_id, language, application, module, type, name, value, edited_on)
                                 VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                                 ON DUPLICATE KEY UPDATE user_id = ?, value = ?, edited_on = ?', array($locale['user_id'], $locale['language'], $locale['application'], $locale['module'], $locale['type'], $locale['name'], $locale['value'], $locale['edited_on'], $locale['user_id'], $locale['value'], $locale['edited_on']));
                            // statistics
                            ++$statistics['imported'];
                        }
                    }
                }
            }
        }
        // rebuild cache
        foreach ($possibleApplications as $application) {
            foreach ($possibleLanguages[$application] as $language) {
                self::buildCache($language, $application);
            }
        }
        return $statistics;
    }

Usage Example

Example #1
0
 /**
  * @param string $localePath
  * @param bool $overwrite
  * @param OutputInterFace $output
  * @throws Exception
  */
 private function importLocale($localePath, $overwrite, OutputInterface $output)
 {
     // Load the xml from the file
     $xmlData = @simplexml_load_file($localePath);
     // This is an invalid xml file
     if ($xmlData === false) {
         throw new Exception('Invalid locale.xml file.');
     }
     // Everything ok, let's import the locale
     $results = BackendLocaleModel::importXML($xmlData, $overwrite, null, null, 1);
     if ($results['total'] < 0) {
         $output->writeln('<error>Something went wrong during import.</error>');
         return;
     }
     if ($results['imported'] > 0) {
         $output->writeln('<comment>Imported ' . $results['imported'] . ' translations succesfully!</comment>');
         return;
     }
     if ($results['imported'] == 0) {
         $output->writeln('<info>No locale was imported. Try adding the overwrite (-o) option.</info>');
         return;
     }
 }
All Usage Examples Of Backend\Modules\Locale\Engine\Model::importXML