Yii Framework Forum: Get text message does not parse the PO files - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get text message does not parse the PO files Rate Topic: -----

#1 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 23 February 2010 - 04:04 AM

I have the following PO file:

msgid ""
msgstr ""
"Project-Id-Version: fewf\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: wefwef <ewf>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "Hello"
msgstr "REPLACED"


when the function

/**
	 * Loads messages from a PO file.
	 * @param string file path
	 * @param string message context
	 * @return array message translations (source message => translated message)
	 */
	public function load($file,$context)
	{
		$pattern='/msgctxt\s+"(.*?(?<!\\\\))"'
			. '\s+msgid\s+"(.*?(?<!\\\\))"'
			. '\s+msgstr\s+"(.*?(?<!\\\\))"/';
		$content=file_get_contents($file);
        $n=preg_match_all($pattern,$content,$matches);
        $messages=array();
        for($i=0;$i<$n;++$i)
        {
        	if($matches[1][$i]===$context)
        	{
	        	$id=$this->decode($matches[2][$i]);
	        	$message=$this->decode($matches[3][$i]);
	        	$messages[$id]=$message;
	        }
        }
        return $messages;
	}


from the CGettextPoFile class runs it returns nothing, Even the $matches returns nothing, meaning it does not parse the PO file correctly.

Any idea why?
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 24 February 2010 - 07:25 AM

Maybe it's a dumb question, but have you checked that $content does have the actual PO file loaded?
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 24 February 2010 - 01:09 PM

Yea, I did lots of tests including checking that it loads the correct file, and loads the content into the variable, But i got to the point that the problem is with the regex that does not match anything inside the PO file. Which seems to be as a bug since the regex doesn't work.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 25 February 2010 - 04:47 AM

The problem is that the current regex looks for message context (msgctxt) in every "record" like
msgctxt ""
msgid "Hello"
msgstr "REPLACED"

msgctxt ""
msgid "Hello2"
msgstr "REPLACED2"


So the regex should be changed so that the context msgctxt is optional (enclosed in ( ... )? in the regex) and the $matches index should be set appropriately...

here is the new load() function that works:
    public function load($file,$context)
    {
                $pattern='/(msgctxt\s+"(.*?(?<!\\\\))")?'
                        . '\s+msgid\s+"(.*?(?<!\\\\))"'
                        . '\s+msgstr\s+"(.*?(?<!\\\\))"/';
        $content=file_get_contents($file);
        $n=preg_match_all($pattern,$content,$matches);
        $messages=array();
        for($i=0;$i<$n;++$i)
        {
            if($matches[2][$i]===$context)
            {
                $id=$this->decode($matches[3][$i]);
                $message=$this->decode($matches[4][$i]);
                $messages[$id]=$message;
            }
        }
        return $messages;
    }

Find more about me.... btw. Do you know your WAN IP?
1

#5 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 25 February 2010 - 07:38 AM

I see, SO in other words it seems to be as this is a bug in the framework unless the msgctxt *must* exist for every record.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 25 February 2010 - 08:00 AM

View PostVince., on 25 February 2010 - 07:38 AM, said:

I see, SO in other words it seems to be as this is a bug in the framework unless the msgctxt *must* exist for every record.


PO file specification say that the msgctxt is optional... in Yii this is used for the context... so if you are using translations for different contexts than you should use the msgctxt on every record in this implementation as it is now...
Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   mintao 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-December 09
  • Location:Munich, Germany

Posted 25 February 2010 - 05:10 PM

A little bit off topc, but I'd change form gettext to simple arrays and use APC to keep those translation files in memory. This is a really heavy and time consuming regular expression you'd run many many times for each page request.
0

#8 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 27 February 2010 - 07:55 AM

Yes, Though on a production environment you will be using the MO files isntead which are complied.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users