PHP: How can you use line breaks in Poedit

I use poedit to translate gettext strings. I use identifiers for translation, for example, for example, the following identifier:

"msg_forgot_pw"

will be translated into:

"I've forgot my password"

The line will be echoed, so the line must be valid HTML. I am currently using <br>to create new lines in translation in Poedit. We have already figured out what you can use \nin a translation and use a type translation function Álvaro G. Vicario. The only problem is that you have to enter \nmanually in Pedit, because it does not replace the input \n. So, is there a way to make new lines in the outputs of these translation lines without input into it <br>or \n?

+4
source share
2 answers

The PO catalog format does not limit translations to a single layer, but you need to use proper markup:

#: example.php:2
msgid "__multi_line_example__"
msgstr ""
"One line\n"
"Two lines\n"
"Three lines\n"

Unfortunately, the popular program Poedit does not simplify: you can enter new new lines with the key Enter, but they are silently deleted when saved! You can insert new lines even with Poedit, but you need to manually enter the escape sequence \n:

New lines in poedit

(I don’t know what other graphical tools do.)

The line will be printed using echo, so the line must be valid HTML.

IMHO, HTML- . , . HTML, :

<p><?php echo htmlspecialchars(_('msg_forgot_pw')); ?></p>

, HTML (, ).

:

  • <br>

    <p><?php echo nl2br(htmlspecialchars(_('msg_forgot_pw'))); ?></p>
    

, :

/**
 * Fetches translation as HTML (block with line feeds)
 * 
 * @param string $msg_id Message ID
 * @return string HTML
 */
function p($msg_id){
    return nl2br(htmlspecialchars(_($msg_id), ENT_COMPAT | ENT_HTML401, 'UTF-8'), false);
}

/**
 * Fetches translation as HTML (single line)
 * 
 * @param string $msg_id Message ID
 * @return string HTML
 */
function l($msg_id){
    return htmlspecialchars(_($msg_id), ENT_COMPAT | ENT_HTML401, 'UTF-8');
}

: : ", Poedit". .

+4

, "" \n, \r \r\n ( ) PHP <br /> - PHP: nl2br. , , :

function translate($key){
   // do something to get the translation from the file and store it in some variable ($translation in this case)
    return nl2br($translation)
}

:

echo translation("msg_forgot_pw");

:

array(
    // you can use a \n, \r, \r\n tag
    "msg_forgot_pw" => "I have forgot \nmy password",
     // or you can simply add a linebreak
    "msg_forgot_pw2" => "I have forgot 
my password",

    // ... more entries
)
+3

Source: https://habr.com/ru/post/1526207/


All Articles