当前位置: 首页>>代码示例>>PHP>>正文


PHP Tool::getFallbackLanguagesFor方法代码示例

本文整理汇总了PHP中Pimcore\Tool::getFallbackLanguagesFor方法的典型用法代码示例。如果您正苦于以下问题:PHP Tool::getFallbackLanguagesFor方法的具体用法?PHP Tool::getFallbackLanguagesFor怎么用?PHP Tool::getFallbackLanguagesFor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Pimcore\Tool的用法示例。


在下文中一共展示了Tool::getFallbackLanguagesFor方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: createLocalizedViews

    /**
     *
     */
    public function createLocalizedViews()
    {
        // init
        $languages = Tool::getValidLanguages();
        $defaultTable = 'object_query_' . $this->model->getClass()->getId();
        $db = $this->db;
        /**
         * macro for creating ifnull statement
         * @param string $field
         * @param array  $languages
         *
         * @return string
         */
        $getFallbackValue = function ($field, array $languages) use(&$getFallbackValue, $db) {
            // init
            $lang = array_shift($languages);
            // get fallback for current language
            $fallback = count($languages) > 0 ? $getFallbackValue($field, $languages) : 'null';
            // create query
            $sql = sprintf('ifnull(`%s`.`%s`, %s)', $lang, $field, $fallback);
            return $fallback !== 'null' ? $sql : $db->quoteIdentifier($lang) . '.' . $db->quoteIdentifier($field);
        };
        foreach ($languages as $language) {
            try {
                $tablename = $this->getQueryTableName() . "_" . $language;
                // get available columns
                $viewColumns = array_merge($this->db->fetchAll('SHOW COLUMNS FROM `' . $defaultTable . '`'), $this->db->fetchAll('SHOW COLUMNS FROM `objects`'));
                $localizedColumns = $this->db->fetchAll('SHOW COLUMNS FROM `' . $tablename . '`');
                // get view fields
                $viewFields = [];
                foreach ($viewColumns as $row) {
                    $viewFields[] = $this->db->quoteIdentifier($row['Field']);
                }
                // create fallback select
                $localizedFields = [];
                $fallbackLanguages = array_unique(Tool::getFallbackLanguagesFor($language));
                array_unshift($fallbackLanguages, $language);
                foreach ($localizedColumns as $row) {
                    $localizedFields[] = $getFallbackValue($row['Field'], $fallbackLanguages) . sprintf(' as "%s"', $row['Field']);
                }
                // create view select fields
                $selectViewFields = implode(',', array_merge($viewFields, $localizedFields));
                // create view
                $viewQuery = <<<QUERY
CREATE OR REPLACE VIEW `object_localized_{$this->model->getClass()->getId()}_{$language}` AS

SELECT {$selectViewFields}
FROM `{$defaultTable}`
    JOIN `objects`
        ON (`objects`.`o_id` = `{$defaultTable}`.`oo_id`)
QUERY;
                // join fallback languages
                foreach ($fallbackLanguages as $lang) {
                    $viewQuery .= <<<QUERY
LEFT JOIN {$this->getQueryTableName()}_{$lang} as {$lang}
    ON( 1
        AND {$defaultTable}.oo_id = {$lang}.ooo_id
    )
QUERY;
                }
                // execute
                $this->db->query($viewQuery);
            } catch (\Exception $e) {
                Logger::error($e);
            }
        }
    }
开发者ID:pimcore,项目名称:pimcore,代码行数:70,代码来源:Dao.php

示例2: translate

 /**
  * @param array|string $messageId
  * @param null $locale
  * @return array|string
  * @throws \Exception
  */
 public function translate($messageId, $locale = null)
 {
     $messageIdOriginal = $messageId;
     $messageId = mb_strtolower($messageId);
     // the maximum length of message-id's is 255
     if (strlen($messageId) > 255) {
         throw new \Exception("Pimcore_Translate: Message ID's longer than 255 characters are invalid!");
     }
     if ($locale === null) {
         $locale = $this->_options['locale'];
     }
     // list isn't cacheable, just get a single item
     if (!$this->isCacheable) {
         $backend = self::getBackend();
         $translation = $backend::getByKeyLocalized($messageIdOriginal, true, true, $locale);
         if ($translation == $messageIdOriginal) {
             foreach (Tool::getFallbackLanguagesFor($locale) as $fallbackLanguage) {
                 $translation = $backend::getByKeyLocalized($messageIdOriginal, true, true, $fallbackLanguage);
                 if ($translation != $messageIdOriginal) {
                     break;
                 }
             }
         }
         return $translation;
     }
     if (empty($this->_translate[$locale])) {
         $this->_loadTranslationData(null, $locale);
     }
     if (!empty($this->_translate[$locale][$messageId])) {
         // return original translation
         return $this->_translate[$locale][$messageId];
     } else {
         // check if there is a translation in a lower step
         $keyParts = explode(".", $messageId);
         if (count($keyParts) > 1) {
             krsort($keyParts);
             $keysLoop = array();
             foreach ($keyParts as $key) {
                 array_unshift($keysLoop, $key);
                 $tmpKey = implode(".", $keysLoop);
                 if (!empty($this->_translate[$locale][$tmpKey])) {
                     return $this->_translate[$locale][$tmpKey];
                 }
             }
         }
         // check if there is a translation in a lower step (without reverting the keys)
         $tmpKey = $messageId;
         if (strrpos($tmpKey, ':')) {
             while ($tmpKey = substr($tmpKey, 0, strrpos($tmpKey, ':'))) {
                 if (!empty($this->_translate[$locale][$tmpKey])) {
                     return $this->_translate[$locale][$tmpKey];
                 }
             }
         }
     }
     // do not create a new translation if it is only empty, but do not return empty values
     if (!array_key_exists($messageId, $this->_translate[$locale])) {
         $this->createEmptyTranslation($locale, $messageIdOriginal);
     } else {
         // look for a fallback translation
         foreach (Tool::getFallbackLanguagesFor($locale) as $fallbackLanguage) {
             // check if data for fallback language is loaded, if not force it
             if (empty($this->_translate[$fallbackLanguage])) {
                 $this->_loadTranslationData(null, $fallbackLanguage);
             }
             if (!empty($this->_translate[$fallbackLanguage][$messageId])) {
                 return $this->_translate[$fallbackLanguage][$messageId];
             }
         }
     }
     // no translation found, return original
     return $messageIdOriginal;
 }
开发者ID:pawansgi92,项目名称:pimcore2,代码行数:79,代码来源:Translate.php

示例3: getLocalizedValue

 /**
  * @param $name
  * @param null $language
  * @return 
  */
 public function getLocalizedValue($name, $language = null, $ignoreFallbackLanguage = false)
 {
     $data = null;
     $language = $this->getLanguage($language);
     $fieldDefinition = $this->getObject()->getClass()->getFieldDefinition("localizedfields")->getFieldDefinition($name);
     if ($fieldDefinition instanceof Model\Object\ClassDefinition\Data\CalculatedValue) {
         $valueData = new Model\Object\Data\CalculatedValue($fieldDefinition->getName());
         $valueData->setContextualData("localizedfield", "localizedfields", null, $language);
         $data = Service::getCalculatedFieldValue($this->getObject(), $valueData);
         return $data;
     }
     if ($this->languageExists($language)) {
         if (array_key_exists($name, $this->items[$language])) {
             $data = $this->items[$language][$name];
         }
     }
     // check for inherited value
     $doGetInheritedValues = AbstractObject::doGetInheritedValues();
     if ($fieldDefinition->isEmpty($data) && $doGetInheritedValues) {
         $object = $this->getObject();
         $class = $object->getClass();
         $allowInherit = $class->getAllowInherit();
         if ($allowInherit) {
             if ($object->getParent() instanceof AbstractObject) {
                 $parent = $object->getParent();
                 while ($parent && $parent->getType() == "folder") {
                     $parent = $parent->getParent();
                 }
                 if ($parent && ($parent->getType() == "object" || $parent->getType() == "variant")) {
                     if ($parent->getClassId() == $object->getClassId()) {
                         $method = "getLocalizedfields";
                         if (method_exists($parent, $method)) {
                             $localizedFields = $parent->getLocalizedFields();
                             if ($localizedFields instanceof Localizedfield) {
                                 if ($localizedFields->object->getId() != $this->object->getId()) {
                                     $data = $localizedFields->getLocalizedValue($name, $language, true);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // check for fallback value
     if ($fieldDefinition->isEmpty($data) && !$ignoreFallbackLanguage && self::doGetFallbackValues()) {
         foreach (Tool::getFallbackLanguagesFor($language) as $l) {
             if ($this->languageExists($l)) {
                 if (array_key_exists($name, $this->items[$l])) {
                     $data = $this->getLocalizedValue($name, $l);
                 }
             }
         }
     }
     if ($fieldDefinition && method_exists($fieldDefinition, "preGetData")) {
         $data = $fieldDefinition->preGetData($this, array("data" => $data, "language" => $language, "name" => $name));
     }
     return $data;
 }
开发者ID:ChristophWurst,项目名称:pimcore,代码行数:64,代码来源:Localizedfield.php

示例4: getFallbackValue

 protected function getFallbackValue($groupId, $keyId, $language, $fielddefinition)
 {
     $fallbackLanguages = Tool::getFallbackLanguagesFor($language);
     $data = null;
     foreach ($fallbackLanguages as $l) {
         if (array_key_exists($groupId, $this->items) && array_key_exists($keyId, $this->items[$groupId]) && array_key_exists($l, $this->items[$groupId][$keyId])) {
             $data = $this->items[$groupId][$keyId][$l];
             if (!$fielddefinition->isEmpty($data)) {
                 return $data;
             }
         }
     }
     foreach ($fallbackLanguages as $l) {
         $data = $this->getFallbackValue($groupId, $keyId, $l, $fielddefinition);
     }
     return $data;
 }
开发者ID:krugerke,项目名称:pimcore,代码行数:17,代码来源:Classificationstore.php


注:本文中的Pimcore\Tool::getFallbackLanguagesFor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。