Ignore:
Timestamp:
03/17/2009 09:07:25 AM (3 years ago)
Author:
ofer
Message:

Initial commit of auto-translate feature

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/parser.php

    r61 r68  
    9797                        $tag_start = $pos; 
    9898                        $pos = strpos($page, '>', $pos); 
    99                          
     99 
    100100                        //Mark tag end position 
    101101                        $tag_end = $pos; 
     
    156156        logger("Exit " . __METHOD__, 4); 
    157157} 
    158  
    159158 
    160159/* 
     
    209208                        global $is_in_channel; 
    210209                        $is_in_channel = TRUE; 
    211                         break;   
    212         } 
    213  
    214 } 
    215  
     210                        break; 
     211        } 
     212 
     213} 
    216214 
    217215/* 
     
    240238} 
    241239 
    242  
    243240/* 
    244241 * Process html tag. Set the direction for rtl languages. 
     
    301298        if(is_word('![CDATA[')) 
    302299        { 
    303                 $pos += 8; //skip to end of ![CDATA[  
     300                $pos += 8; //skip to end of ![CDATA[ 
    304301        } 
    305302        else 
     
    311308                } 
    312309        } 
    313          
     310 
    314311        logger("Exit " . __METHOD__. ": $pos", 5); 
    315312        return substr($page,$start, $pos - $start); 
     
    385382{ 
    386383        global $page, $pos; 
    387          
     384 
    388385        logger("Enter " . __METHOD__ , 4); 
    389386 
     
    394391                $start = $pos; 
    395392                $page_length =  strlen($page); 
    396                  
    397                 // Indicates if the html entity should break into a new translation segment.  
    398                 $is_breaker = FALSE;   
    399                  
     393 
     394                // Indicates if the html entity should break into a new translation segment. 
     395                $is_breaker = FALSE; 
     396 
    400397                while($pos < $page_length && $page[$pos] != '<') 
    401398                { 
     
    409406                                        $start = $end_of_entity; 
    410407                                } 
    411                                  
     408 
    412409                                //skip past entity 
    413410                                $pos = $end_of_entity; 
     
    433430} 
    434431 
    435  
    436 /* 
    437  * Translate the content of a cdata section.  For now we only expect to handle it  
    438  * within RSS feeds.  
     432/* 
     433 * Translate the content of a cdata section.  For now we only expect to handle it 
     434 * within RSS feeds. 
    439435 */ 
    440436function process_cdata_section() 
    441437{ 
    442438        global $page, $pos; 
    443          
     439 
    444440        logger("Enter " . __METHOD__  , 4); 
    445441 
     
    450446                $start = $pos; 
    451447                $page_length =  strlen($page); 
    452                  
     448 
    453449                while($pos < $page_length && !is_word(']]>')) 
    454450                { 
     
    477473/** 
    478474 * Determines position in page marks a transaltable tag in html page or rss feed section. 
    479  * Return TRUE if should be translated otherwise FALSE.   
     475 * Return TRUE if should be translated otherwise FALSE. 
    480476 */ 
    481477function is_translatable_section() 
     
    484480        $rc = FALSE; 
    485481        $current_tag = end($tags_list); 
    486          
     482 
    487483        if($is_in_body || $current_tag == 'title') 
    488484        { 
     
    496492 
    497493        logger("Exit " . __METHOD__ . " $current_tag, translate: " . ($rc ? "yes" : "no"), 4); 
    498         return $rc;    
     494        return $rc; 
    499495} 
    500496 
     
    525521                        $page[$position] == ';' || 
    526522                        //break on numbers but not like: 3rd, 4th 
    527                         (is_digit($position) && !is_a_to_z_character($position+1)))  
     523                        (is_digit($position) && !is_a_to_z_character($position+1))) 
    528524        { 
    529525                //break the sentence into segments regardless of the next character. 
     
    537533 * Determines if the current position marks the begining of an html 
    538534 * entity. E.g &amp; 
    539  * Return 0 if not an html entity otherwise return the position past this entity. In addition  
     535 * Return 0 if not an html entity otherwise return the position past this entity. In addition 
    540536 *        the $is_breaker will be set to TRUE if entity should break translation into a new segment. 
    541537 * 
     
    677673} 
    678674 
    679  
    680675/* 
    681676 * Check within page buffer position for the given word. 
    682677 * param word The word to look for. 
    683  * param index1 Optional position within the page buffer, if not available then the current  
    684  *              position ($pos) is used.   
     678 * param index1 Optional position within the page buffer, if not available then the current 
     679 *              position ($pos) is used. 
    685680 * Return TRUE if the word matches otherwise FALSE 
    686681 */ 
     
    689684        global $page, $pos; 
    690685        $rc = FALSE; 
    691          
     686 
    692687        if(!isset($index1)) 
    693688        { 
     
    695690                $index1 = $pos; 
    696691        } 
    697          
     692 
    698693        $index2 = 0; //position within word 
    699694        $word_len =   strlen($word); 
    700695        $page_length =  strlen($page); 
    701          
     696 
    702697        while($index1 < $page_length && $index2 < $word_len) 
    703698        { 
     
    712707                } 
    713708        } 
    714          
     709 
    715710        //check if we have full match 
    716711        if($index2 == $word_len) 
     
    718713                $rc = TRUE; 
    719714        } 
    720          
     715 
    721716        return $rc; 
    722717} 
    723718 
    724  
    725 /** 
     719/* 
    726720 * Translate the text between the given start position and the current 
    727721 * position (pos) within the buffer. 
     
    757751        } 
    758752 
    759         $translated_text = fetch_translation($original_text); 
    760  
    761         insert_translation($original_text, $translated_text, $start, $end); 
     753        list($translated_text, $source) = fetch_translation($original_text); 
     754 
     755        insert_translation($original_text, $translated_text, $source, $start, $end); 
    762756} 
    763757 
     
    769763 * param end Marks the end position of the text to be replaced within the original page 
    770764 */ 
    771 function insert_translation(&$original_text, &$translated_text, $start, $end) 
     765function insert_translation(&$original_text, &$translated_text, $source, $start, $end) 
    772766{ 
    773767        global $segment_id, $is_edit_mode, $tags_list; 
     
    784778        else 
    785779        { 
    786                 $span = "<span id=\"" . SPAN_PREFIX . "$segment_id\">"; 
     780                $span_prefix = SPAN_PREFIX; 
     781                // We will mark translated text with tr_t class and untranslated with tr_u 
     782                $span = "<span class=\"$span_prefix"; 
    787783 
    788784                if($translated_text == NULL) 
    789785                { 
     786                        $span .= "u\" id=\"{$span_prefix}{$segment_id}\">"; 
    790787                        $span .= $original_text . '</span>'; 
    791788                } 
    792789                else 
    793790                { 
     791                        $span .= "t\" id=\"{$span_prefix}{$segment_id}\">"; 
    794792                        $span .= $translated_text . "</span>"; 
    795793                        $is_translated = TRUE; 
     
    801799 
    802800                //Insert image to allow editing this segment 
    803                 $img = get_img_tag($original_text, $translated_text, $segment_id, $is_translated); 
     801                $img = get_img_tag($original_text, $translated_text, $source, $segment_id, $is_translated); 
    804802                update_translated_page($end + 1, - 1, $img); 
    805803 
     
    813811} 
    814812 
    815  
    816813/* 
    817814 * Scrubs text prior to translation to remove/encode special 
     
    833830        return $text; 
    834831} 
    835  
    836832 
    837833/** 
     
    865861 
    866862} 
    867  
    868863?> 
Note: See TracChangeset for help on using the changeset viewer.