Added gernic method to handle i18n tags
authorAlexander Ebert <ebert@woltlab.com>
Mon, 30 Nov 2015 13:50:41 +0000 (14:50 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 30 Nov 2015 13:50:41 +0000 (14:50 +0100)
wcfsetup/install/files/lib/system/package/plugin/AbstractXMLPackageInstallationPlugin.class.php

index b1be298988ad8c41e3596833eaacfdf4bb11d157..cc7772f921908f34ca50e0979b79a7466d2359cd 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\system\package\plugin;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\SystemException;
+use wcf\system\language\LanguageFactory;
 use wcf\system\package\PackageArchive;
 use wcf\system\package\PackageInstallationDispatcher;
 use wcf\system\WCF;
@@ -186,6 +187,58 @@ abstract class AbstractXMLPackageInstallationPlugin extends AbstractPackageInsta
                $elements[$element->tagName] = $element->nodeValue;
        }
        
+       /**
+        * Returns i18n values by validating each value against the list of installed
+        * languages, optionally returning only the best matching value.
+        * 
+        * @param       string[]        $values                 list of values by language code
+        * @param       boolean         $singleValueOnly        true to return only the best matching value
+        * @return      string[]|string matching i18n values controller by `$singleValueOnly`
+        */
+       protected function getI18nValues(array $values, $singleValueOnly = false) {
+               if (empty($values)) {
+                       return ($singleValueOnly) ? '' : [];
+               }
+               
+               // check for a value with an empty language code and treat it as 'en' unless 'en' exists
+               if (isset($values[''])) {
+                       if (!isset($values['en'])) {
+                               $values['en'] = $values[''];
+                       }
+                       
+                       unset($values['']);
+               }
+               
+               $matchingValues = [];
+               foreach ($values as $languageCode => $value) {
+                       if (LanguageFactory::getInstance()->getLanguageByCode($languageCode) !== null) {
+                               $matchingValues[$languageCode] = $value;
+                       }
+               }
+               
+               // no matching value found
+               if (empty($matchingValues)) {
+                       if (isset($values['en'])) {
+                               // safest route: pick English
+                               $matchingValues['en'] = $values['en'];
+                       }
+                       else if (isset($values[''])) {
+                               // fallback: use the value w/o a language code
+                               $matchingValues[''] = $values[''];
+                       }
+                       else {
+                               // failsafe: just use the first found value in whatever language
+                               $matchingValues = array_splice($values, 0, 1);
+                       }
+               }
+               
+               if ($singleValueOnly) {
+                       return array_shift($matchingValues);
+               }
+               
+               return $matchingValues;
+       }
+       
        /**
         * Inserts or updates new items.
         *