oddities of Yii:t()

I’m pulling out multiline strings from mysql DB and trying to use Yii:t() for mulit-language, but something is not working and I can figure out why.

I have on the page (output 1)




<p><?php Yii::t('messages', 'record2

line2');?><p>

then I have also (output 2):




<?php

$testVar = 'record2

line2';

?>

<p><?php echo Yii:t('messages', $testVar);?></p>

then I also have (output 3 & output 4):




<p><?php echo CHtml::encode(Yii::t('messages', $data->description)); ?></p>

<p><?php echo Yii::t('messages', $data->description); ?></p>

Where $data->description is TEXT field in a record in a mysql db of string value:


record2

line2

The messages.php looks like this:




<?php

return array (

  'record2

line2' => '紀錄2

第二行',

);

When I then do a language switch, output 1 & 2 works fine, but output 3 & 4 fails to pull the translated text strings from the message.php file.

I looked at the html output and they, to me, all look identical, even the carriage return characters are the same.

The html output of the untranslated:




<p>record2

line2</p>


<p>record2

line2</p>


 	<p>record2

line2</p>


	<p>record2

line2</p>

The html output of the translated:




<p>紀錄2

第二行</p>


<p>紀錄2

第二行</p>


<p>record2

line2</p>


<p>record2

line2</p>

But if I try to do


$testVar == CHtml::encode(Yii::t('multiline', $data->description))

$testVar == Yii::t('multiline', $data->description)

$testVar == $data->description

they would all return false!

At first I though maybe the type came out different, so I used gettype() on $testVar, $data->description, Yii::t(‘messages’, $data->description), CHtml::encode(Yii::t(‘multiline’, $data->description)), and they are all strings.

I’ve also tried this where the data is single line instead of multiple lines, and everything works just fine.

Can someone help me figure out what’s going on and how to fix this so output 3 & 4 would correctly show the translated text?

Thanx in advance.

Hi,

Please make var_dump() also for $testVar and $data->description.

I’d also remove \r\n from the key word, if it suits for you to use it in a form ‘record_line’, for example. From my experience, having any kind of invisible characters in key words may lead to errors.

Thank you. That helped. It turned out that the text came out from the db uses \r\n where the ones I typed uses only \n. That’s where the difference is, my text editor only tells me there is a return char there…

I don’t quite follow you on the ‘record_line’, can you elaborate a bit?

What I’m trying to do is to let user type multiline text with markdown syntax in a mysql TEXT field and I can do translation.

The users won’t be very technical, so teaching them to write in markdown syntax is about as far as they can go. The rest I have to code.

So it seems the problem lies with mysql output using \r\n.

For me, the only suitable solution seems to be to make sure mysql only uses \n. Is that possible?

I hear you about hidden character not playing well as key, but since I know \n works, I would like to try to modify mysql behavior.

Glad to hear you found the cause of the problem. Unfortunately, I didn’t clearly understand what you’re trying to accomplish, but, if I understood you correctly, I suppose proper strings handling should solve your problem.

Nothing specific :)

Just an example of my usual practice having key words as simple as possible and spaces replaced with underscores.

[EDITED] , I found an advice to use SET @newline=CHAR(10); hope it’ll help (not tested).

Sorry, late light post, I wasn’t clear.

To sum it up, I need to find a way to make sure when I echo $data->description, the string I get will use \n instead of \r\n. I can always do a str_replace which works (tested). But I would prefer to make some kind of one time command (at table or field creation time) or configuration to make sure mysql use \n instead of \r\n to save data into TEXT field. You see, the data is input by users with a HTML TEXTAREA in a form. They will just hit return/enter key to make a new line.

I looked up SET @newline=CHAR(10), but have yet to find a good reference on how to use it.