parse gettext() into html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrdebian
Forum Newbie
Posts: 9
Joined: Thu May 10, 2007 4:38 am

parse gettext() into html

Post by mrdebian »

Hello all,

I'm trying to find how I can localize using gettext the following form:

Code: Select all

<?php
if (!empty($_GET["error"]))
{
    switch ($_GET["error"])
    {
        case "not_enough_info": ?>
            <strong style="color: red;">You need to complete all fields marked *<br/>
            and also be sure that you type the security code as it appears in the image.<strong><?php
            break;
        case "invalid_email": ?>
            <strong style="color: red;">Please provide a valid email address</strong><?php
            break;
        case "upload_failed": ?>
            <strong style="color: red;">The file you uploaded failed to attach, this could be a temporary problem.
            Please try later.</strong><?php
            break;
        case "sending_failed": ?>
            <strong style="color: red;">Temporary problem, please try later.</strong><?php
            break;
    }
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  	<title>Contact Form</title>
  	<meta name="description" content="contact form" />
	<meta name="keywords" content="Contact form, contact us. " />
	<!-- <link href="../../css/main.css" rel="stylesheet" type="text/css" /> -->
</head>
<body>
<form action="handle_form.php" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td class="label">Name *</td>
            <td><input type="text" name="sender_name" value="" /></td>
        </tr>
        <tr>
            <td class="label">E-mail address *</td>
            <td><input type="text" name="sender_email" value="" /></td>
        </tr>
        <tr>
            <td class="label">Title *</td>
            <td><input type="text" name="comment_title" value="" /></td>
        </tr>
        <tr>
            <td class="label">Attachment (optional)</td>
            <td><input type="file" name="attachment" /></td>
        </tr>
        <tr>
            <td colspan="2">Comment *<br />
                <textarea name="comment_body" rows="10" cols="60"></textarea></td>
        </tr>
        <tr>
        	<td>Security Code: *
			<input id="security_code" name="security_code" type="text" />
			<img src="../../includes/captcha.php?width=150&height=50&characters=5" />
			</td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
        </tr>
    </table>
</form>
</body>
</html>
The only examples that I fount is with the use of gettext like:
echo gettext("some comments");

How can I use the above to translate the comments between the case for example and also to the html form?
Is there a way to do that or I need to create a seperate file/form for each language?

The po files etc are already fine and working. I just need to find how I can use gettext() to translate the non echo values.

Thanks a lot
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

well these are not non-echo values.
if they are seen...then they are obviously echoed.


You just need to translate each

Code: Select all

if (!empty($_GET["error"]))
{
    switch ($_GET["error"])
    {
        case "not_enough_info": ?>
            <strong style="color: red;"><?php echo _("You need to complete all fields marked *") ?> <br/>
            <?php echo _("and also be sure that you type the security code as it appears in the image.") ?><strong><?php
            break;
}
}

As you see it is getting really ugly to read....so you should follow some rules to design stuff...and definately separate html(view) from php (business)
logic.
You definately don't need separate file for each language. Thats the whole point.

You will create diferent locales for gettext to use...but forms will be same.

Also try/test top to bottom stuff. It is tricky. everytime you rebuild your .mo files you will need to restart the server...otherwise translations won't kick in magically, unless using not-native php gettext support, but some 3rd party lib.
Post Reply