Index: trunk/core/kernel/languages/phrases_cache.php =================================================================== diff -u -N -r932 -r1339 --- trunk/core/kernel/languages/phrases_cache.php (.../phrases_cache.php) (revision 932) +++ trunk/core/kernel/languages/phrases_cache.php (.../phrases_cache.php) (revision 1339) @@ -3,10 +3,13 @@ class PhrasesCache extends kDBBase { var $Phrases = Array(); - var $Ids; + var $Ids = Array(); + var $OriginalIds = Array(); //for comparing cache var $LanguageId = 1; + var $fromTag = false; + function PhrasesCache($LanguageId=1) { parent::kDBBase(); @@ -18,7 +21,7 @@ function GetCachedIds() { $query = sprintf("SELECT PhraseList FROM %s WHERE Template = %s", - 'PhraseCache', + TABLE_PREFIX.'PhraseCache', $this->Conn->Qstr($this->Application->GetVar('t'))); $phrases_ids = $this->Conn->GetOne($query); if ($phrases_ids === false) return Array(); @@ -27,9 +30,9 @@ function LoadPhrases($ids) { - if (!is_array($ids) || count($ids) == 0) return; + if ( !is_array($ids) || !implode('', $ids) ) return; $query = sprintf("SELECT Translation,Phrase FROM %s WHERE LanguageId = %s AND PhraseId IN (%s)", - 'Phrase', + TABLE_PREFIX.'Phrase', $this->LanguageId, join(',', $ids)); $phrases = $this->Conn->GetCol($query,'Phrase'); @@ -38,6 +41,7 @@ $this->AddCachedPhrase(strtoupper($phrase), $tanslation); } $this->Ids = $ids; + $this->OriginalIds = $ids; } function AddCachedPhrase($label, $value) @@ -48,10 +52,11 @@ function UpdateCache() { - if (!is_array($this->Ids) || count($Ids) == 0) return; + if (!is_array($this->Ids) || count($this->Ids) == 0) return; + if ($this->Ids == $this->OriginalIds) return; //nothing changed $query = sprintf("REPLACE %s (PhraseList, CacheDate, Template) VALUES (%s, %s, %s)", - 'PhraseCache', + TABLE_PREFIX.'PhraseCache', $this->Conn->Qstr(join(',', $this->Ids)), mktime(), $this->Conn->Qstr($this->Application->GetVar('t'))); @@ -60,31 +65,99 @@ function GetPhrase($label) { + if (ereg("^!.+!$", $label) > 0) { + $label = substr($label, 1, -1); //cut exclamation marks + } + + $original_label = $label; $label = strtoupper($label); - if (array_key_exists($label, $this->Phrases)) - return $this->Phrases[$label]; + if( isset($this->Phrases[$label]) ) return $this->Phrases[$label]; - $this->LoadPhraseByLabel($label); + $this->LoadPhraseByLabel($label, $original_label); return $this->GetPhrase($label); } - function LoadPhraseByLabel($label) + function LoadPhraseByLabel($label, $original_label) { $query = sprintf("SELECT PhraseId, Translation FROM %s WHERE LanguageId = %s AND UPPER(Phrase) = UPPER(%s)", - Phrase, + TABLE_PREFIX.'Phrase', $this->LanguageId, $this->Conn->qstr($label)); $res = $this->Conn->GetRow($query); - if ($res === false || count($res) == 0) { - $this->AddCachedPhrase($label, '!'.$label.'!'); //add it as already cached, as long as we dont need to cache not found phrase + if ($res === false || count($res) == 0) + { + $translation = '!'.$label.'!'; + if( defined('DEBUG_MODE') && defined('ADMIN') && ADMIN && dbg_ConstOn('DBG_PHRASES')) + { + $edit_url = $this->Application->HREF('in-commerce/regional/phrases_edit','',Array('m_opener'=>'d','phrases_label'=>$original_label,'phrases_event'=>'OnNew', 'pass'=>'all,phrases','index_file'=>'index4.php') ); + $translation = '!'.$label.'!'; + if($this->fromTag) $translation = $this->escapeTagReserved($translation); + } + $this->AddCachedPhrase($label, $translation); //add it as already cached, as long as we dont need to cache not found phrase return false; } $this->Phrases[$label] = $res['Translation']; array_push($this->Ids, $res['PhraseId']); - $this->Ids = array_unique ( $this->Ids ); //just to make sure + $this->Ids = array_unique($this->Ids); //just to make sure return true; } + + /** + * Sort params by name and then by length + * + * @param string $a + * @param string $b + * @return int + * @access private + */ + function CmpParams($a, $b) + { + $a_len = strlen($a); + $b_len = strlen($b); + if ($a_len == $b_len) return 0; + return $a_len > $b_len ? -1 : 1; + } + + /** + * Replace language tags in exclamation marks found in text + * + * @param string $text + * @return string + * @access public + */ + function ReplaceLanguageTags($text) + { + $this->fromTag = true; + preg_match_all("(!(la|lu)[^!]+!)", $text, $res, PREG_PATTERN_ORDER); + $language_tags = $res[0]; + uasort($language_tags, Array(&$this, 'CmpParams') ); + + $values = Array(); + $i = 0; + foreach ($language_tags as $label) { + array_push($values, $this->GetPhrase($label) ); + //array_push($values, $this->Application->Phrase($label) ); + $language_tags[$i] = '/' . $language_tags[$i] . '/'; + $i++; + } + $this->fromTag = false; + return preg_replace($language_tags, $values, $text); + } + + /** + * Escape chars in phrase translation, that could harm parser to process tag + * + * @param string $text + * @return string + * @access private + */ + function escapeTagReserved($text) + { + $reserved = Array('"',"'"); // = + $replacement = Array('\"',"\'"); // \= + return str_replace($reserved,$replacement,$text); + } }