Changeset 522


Ignore:
Timestamp:
08/27/2010 02:26:28 PM (18 months ago)
Author:
ofer
Message:

Provide a working gettext interface that blends with Transposh

Location:
trunk/WordPress/plugin/transposh
Files:
2 edited

Legend:

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

    r518 r522  
    270270        //· 
    271271        if (ord($char) == 194 && ord($nextchar) == 183) return 2; 
    272         return (strpos(',?()[]"!:|;', $char) !== false) ? 1 : 0; // TODO: might need to add < and > here 
     272        return (strpos(',?()[]"!:|;'.TP_GETTEXT_BREAKER.TP_GETTEXT_CLOSER.TP_GETTEXT_INNER_BREAKER.TP_GETTEXT_INNER_BREAKER_CLOSER, $char) !== false) ? 1 : 0; // TODO: might need to add < and > here 
    273273    } 
    274274 
     
    288288    function tag_phrase($string, $start, $end) { 
    289289        $phrase = trim(substr($string, $start, $end - $start)); 
    290         if ($this->in_get_text && !$this->in_get_text_inner) { 
     290//        $logstr = str_replace(array(chr(1),chr(2),chr(3),chr(4)), array('[1]','[2]','[3]','[4]'), $string); 
     291//        logger ("p:$phrase, s:$logstr, st:$start, en:$end, gt:{$this->in_get_text}, gti:{$this->in_get_text_inner}"); 
     292//        logger (''); 
     293        if ($this->in_get_text > $this->in_get_text_inner) { 
    291294            logger('not tagging ' . $phrase . ' assumed gettext translated', 4); 
    292295            return; 
     
    343346                $pos++; 
    344347                $start = $pos; 
    345             } elseif ($string[$pos] == TP_GETTEXT_BREAKER) { 
    346                 logger("text breaker $start $pos $string " . (($this->in_get_text) ? 'true' : 'false'), 5); 
     348            } elseif ($string[$pos] == TP_GTXT_BRK || $string[$pos] == TP_GTXT_BRK_CLOSER) { 
     349//                $logstr = str_replace(array(chr(1),chr(2),chr(3),chr(4)), array('[1]','[2]','[3]','[4]'), $string); 
     350//                $closers = ($string[$pos] == TP_GTXT_BRK) ? '': 'closer'; 
     351//                logger(" $closers TEXT breaker $logstr start:$start pos:$pos gt:" . $this->in_get_text, 3); 
    347352                $this->tag_phrase($string, $start, $pos); 
     353                ($string[$pos] == TP_GTXT_BRK) ? $this->in_get_text += 1: $this->in_get_text -= 1; 
    348354                $pos++; 
    349355                $start = $pos; 
    350                 $this->in_get_text = !$this->in_get_text; // flipping state 
    351                 if ($pos == 1) 
    352                         $this->in_get_text = true; // reset state based on string start 
    353  $this->in_get_text_inner = false; 
    354             } elseif ($string[$pos] == TP_GETTEXT_INNER_BREAKER) { 
    355                 logger("inner text breaker $start $pos $string " . (($this->in_get_text_inner) ? 'true' : 'false'), 5); 
     356                // reset state based on string start, no need to flip 
     357                //$this->in_get_text = ($pos == 1); 
     358                //if (!$this->in_get_text) $this->in_get_text_inner = false; 
     359            } elseif ($string[$pos] == TP_GTXT_IBRK || $string[$pos] == TP_GTXT_IBRK_CLOSER ) { 
     360//                $logstr = str_replace(array(chr(1),chr(2),chr(3),chr(4)), array('[1]','[2]','[3]','[4]'), $string); 
     361//                $closers = ($string[$pos] == TP_GTXT_IBRK) ? '': 'closer'; 
     362//                logger("   $closers INNER text breaker $logstr start:$start pos:$pos gt:" . $this->in_get_text_inner, 3); 
     363                //logger("inner text breaker $start $pos $string " . (($this->in_get_text_inner) ? 'true' : 'false'), 5); 
    356364                $this->tag_phrase($string, $start, $pos); 
     365                if ($this->in_get_text) 
     366                ($string[$pos] == TP_GTXT_IBRK) ? $this->in_get_text_inner += 1 : $this->in_get_text_inner -=1; 
    357367                $pos++; 
    358368                $start = $pos; 
    359                 $this->in_get_text_inner = !$this->in_get_text_inner; 
     369                //$this->in_get_text_inner = !$this->in_get_text_inner; 
    360370            } 
    361371            // will break translation unit when there's a breaker ",.[]()..." 
    362372            elseif ($senb_len = $this->is_sentence_breaker($string[$pos], $string[$pos + 1], $string[$pos + 2])) { 
     373//                logger ("sentence breaker..."); 
    363374                $this->tag_phrase($string, $start, $pos); 
    364375                $pos += $senb_len; 
     
    368379            // also prefixed by whitespace? 
    369380            elseif ($num_len = $this->is_number($string, $pos)) { 
     381//                logger ("numnum... $num_len"); 
    370382                // this is the case of B2 or B2, 
    371383                if (($this->is_white_space($string[$pos - 1]) || ($start == $pos) 
     
    373385                        ($this->is_white_space($string[$pos + $num_len]) || $this->is_sentence_breaker($string[$pos + $num_len], $string[$pos + $num_len + 1], $string[$pos + $num_len + 2]))) { 
    374386                    // we will now compensate on the number followed by breaker case, if we need to 
     387//                            logger ("compensate part1?"); 
    375388                    if (!($this->is_white_space($string[$pos - 1]) || ($start == $pos))) { 
     389//                            logger ("compensate part2?"); 
    376390                        if ($this->is_sentence_breaker($string[$pos + $num_len - 1], $string[$pos + $num_len], $string[$pos + $num_len + 1])) { 
     391//                            logger ("compensate 3?"); 
    377392                            $num_len--; //this makes the added number shorter by one, and the pos will be at a sentence breaker next so we don't have to compensate 
    378393                        } 
     
    381396                    } 
    382397                    $this->tag_phrase($string, $start, $pos); 
    383                     $start = $pos + $num_len + 1; 
    384                 } 
    385                 $pos += $num_len + 1; 
     398                    $start = $pos + $num_len /*+1*/; 
     399                } 
     400                $pos += $num_len/* + 1*/; 
     401//                logger ("numnumpos... $pos"); 
    386402            } else { 
    387403                // smarter marking of start location 
     
    557573        $this->html = str_get_html($string); 
    558574        // mark translateable elements 
     575        $this->html->find('html', 0)->lang = ''; // Document defined lang may be preset to correct lang, but should be ignored TODO: Better? 
    559576        $this->translate_tagging($this->html->root); 
    560577 
     
    758775 
    759776        // we make sure that the result is clear from our shananigans 
    760         return str_replace(array(TP_GETTEXT_BREAKER, TP_GETTEXT_INNER_BREAKER), '', $this->html->outertext); 
     777        return str_replace(array(TP_GTXT_BRK, TP_GTXT_IBRK, TP_GTXT_BRK_CLOSER, TP_GTXT_IBRK_CLOSER), '', $this->html->outertext); 
    761778        // Changed because of places where tostring failed 
    762779        //return $this->html; 
  • trunk/WordPress/plugin/transposh/transposh.php

    r500 r522  
    6060    /** @var transposh_3rdparty Happens after editing */ 
    6161    private $third_party; 
    62  
    6362    // list of properties 
    6463    /** @var string The site url */ 
     
    7271    /** @var boolean Enable rewriting of URLs */ 
    7372    public $enable_permalinks_rewrite; 
    74     /** @var string The language to translate the page to */ 
     73    /** @var string The language to translate the page to, from params */ 
    7574    public $target_language; 
     75    /** @var string The language extracted from the url */ 
     76    public $tgl; 
    7677    /** @var boolean Are we currently editing the page? */ 
    7778    public $edit_mode; 
     
    139140//        add_action('admin_menu', array(&$this, 'transposh_post_language')); 
    140141//        add_action('save_post', array(&$this, 'transposh_save_post_language')); 
    141  
    142142        //TODO add_action('manage_comments_nav', array(&$this,'manage_comments_nav')); 
    143143        //TODO comment_row_actions (filter) 
    144         // toying around the mo/po stuff 
    145         add_filter('gettext', array(&$this, 'transposh_gettext_filter'), 10, 3); 
     144        // Intergrating with the gettext interface 
     145        add_filter('gettext', array(&$this, 'transposh_gettext_filter'), 10, 2); 
     146        add_filter('gettext_with_context', array(&$this, 'transposh_gettext_filter'), 10, 2); 
     147        add_filter('ngettext', array(&$this, 'transposh_ngettext_filter'), 10, 3); 
     148        add_filter('ngettext_with_context', array(&$this, 'transposh_ngettext_filter'), 10, 3); 
    146149        add_filter('locale', array(&$this, 'transposh_locale_filter')); 
    147150 
     151        //CHECK TODO!!!!!!!!!!!! 
     152        $this->tgl = transposh_utils::get_language_from_url($_SERVER['REQUEST_URI'], $this->home_url); 
    148153 
    149154        register_activation_hook(__FILE__, array(&$this, 'plugin_activate')); 
     
    244249     */ 
    245250    function on_shutdown() { 
    246         ob_flush(); 
     251        //TODO !!!!!!!!!!!! ob_flush(); 
    247252    } 
    248253 
     
    321326 
    322327        // first we get the target language 
    323         $this->target_language = $wp->query_vars[LANG_PARAM]; 
    324         if (!$this->target_language) 
    325                 $this->target_language = $this->options->get_default_language(); 
     328        /*        $this->target_language = (isset($wp->query_vars[LANG_PARAM])) ? $wp->query_vars[LANG_PARAM] : ''; 
     329          if (!$this->target_language) 
     330          $this->target_language = $this->options->get_default_language(); 
     331          logger("requested language: {$this->target_language}"); */ 
     332        // TODO TOCHECK!!!!!!!!!!!!!!!!!!!!!!!!!!1 
     333        $this->target_language = $this->tgl; 
    326334        logger("requested language: {$this->target_language}"); 
     335 
    327336 
    328337        // make themes that support rtl - go rtl http://wordpress.tv/2010/05/01/yoav-farhi-right-to-left-themes-sf10 
     
    676685            $n = !empty($q['exact']) ? '' : '%'; 
    677686            $searchand = ''; 
     687            $search = ''; 
    678688            foreach ((array) $q['search_terms'] as $term) { 
    679689                // now we'll get possible translations for this term 
     
    820830    } 
    821831 
    822     function transposh_gettext_filter($a, $orig, $domain) { 
    823         //logger("$a $orig $domain"); 
    824         return $a; 
     832    /** 
     833     * This function adds our markings around gettext results 
     834     * @param string $translation 
     835     * @param string $orig 
     836     * @return string 
     837     */ 
     838    function transposh_gettext_filter($translation, $orig) { 
     839        if ($this->is_special_page($_SERVER['REQUEST_URI']) || ($this->options->is_default_language($this->tgl) && !$this->options->get_enable_default_translate())) { 
     840            logger($translation); 
     841            return $translation; 
     842        } 
     843        if ($translation != $orig) { 
     844            $translation = TP_GTXT_BRK . $translation . TP_GTXT_BRK_CLOSER; 
     845        } 
     846        $translation = str_replace(array('%s', '%1$s', '%2$s', '%3$s', '%4$s', '%5$s'), array(TP_GTXT_IBRK . '%s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%1$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%2$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%3$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%4$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%5$s' . TP_GTXT_IBRK_CLOSER), $translation); 
     847        return $translation; 
     848    } 
     849 
     850    /** 
     851     * This function adds our markings around ngettext results 
     852     * @param string $translation 
     853     * @param string $single 
     854     * @param string $plural 
     855     * @return string 
     856     */ 
     857    function transposh_ngettext_filter($translation, $single, $plural) { 
     858        if ($this->is_special_page($_SERVER['REQUEST_URI']) || ($this->options->is_default_language($this->tgl) && !$this->options->get_enable_default_translate())) 
     859                return $translation; 
     860        if ($translation != $single && $translation != $plural) { 
     861            $translation = TP_GTXT_BRK . $translation . TP_GTXT_BRK_CLOSER; 
     862        } 
     863        $translation = str_replace(array('%s', '%1$s', '%2$s', '%3$s', '%4$s', '%5$s'), array(TP_GTXT_IBRK . '%s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%1$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%2$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%3$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%4$s' . TP_GTXT_IBRK_CLOSER, TP_GTXT_IBRK . '%5$s' . TP_GTXT_IBRK_CLOSER), $translation); 
     864        return $translation; 
    825865    } 
    826866 
    827867    function transposh_locale_filter($locale) { 
    828         //logger($locale); 
     868        $lang = transposh_utils::get_language_from_url($_SERVER['REQUEST_URI'], $this->home_url); 
     869        if (!$lang) return $locale; 
     870        list ($l, $n, $f, $locale) = explode(',', transposh_consts::$languages[$lang]); 
     871        if ($locale) { 
     872            return $locale; 
     873        } else { 
     874            return $lang; 
     875        } 
    829876        return $locale; 
    830877    } 
Note: See TracChangeset for help on using the changeset viewer.