0

Hi,
I've developed a php web-app recently in english language. Now, I want to add support to other languages. I've googled & found some results like using gettext(),storing in .ini files etc. This is good for static websites.

But, the thing here is I want to display some data from mysql database in the middle of the message.
For example, In english it is

<?php
$user = $row['username'];
$balance = $row['balance'];
echo "Hello $user, you've $balance credits in your account";
?>

and in hindi,

<?php
$user = $row['username'];
$balance = $row['balance'];
echo "Namaste $user, Aapke paas $balance credits hai";
?>

Now, how do I translate like this. Thanks for any help.

3
Contributors
5
Replies
27
Views
3 Years
Discussion Span
Last Post by diafol
2

This is quite simple. You use placeholders like %s and use sprintf().

http://php.net/manual/en/function.sprintf.php

However, look at example 3 because often in international strings, the position of the placeholders may be changed, so you have to give them a position specifier.

If you're interested in GETTEXT, I wrote a few blog articles about it and the use of PoEdit here:

http://diafol.blogspot.co.uk/p/quick-index.html

See the sections under PHP and PoEdit

You may also find this interesting for gettext:

http://stackoverflow.com/questions/5445815/gettext-placeholders

If you're just using array files to hold data, for example...

en.php

$lang = array(
    "balance"=>"Hello %1$s, you've %2$f credits in your account",
    ...
);

hi.php

$lang = array(
    "balance"=>"Namaste %1$s, Aapke paas %2$f credits hai",
    ...
);

In your code...

$user = $row['username'];
$balance = $row['balance'];

echo sprintf($lang['balance'], $user, $balance);

Edited by diafol

Votes + Comments
Thank you very much, what do you think is the best way? Using array files or gettext or parsing .ini files? We'll have 3 languages for now but may increase in future.
1

Thank you very much, what do you think is the best way? Using array files or gettext or parsing .ini files? We'll have 3 languages for now but may increase in future.

This depends on how complicated your site is or how you intend on updating translations. The array files are probably the simplest, as you can send the language files to your translators to complete. However, creating the language files and manually keeping them in sync can be a bit of a nightmare. Finding the default language entry if the chosen language entry does not exist can be fun - you need to have an approach for this.

GETTEXT is beautiful IF you can set it up to play nicely. You can then just send out the po files in need of updating - so not need to manually keep the files in sync with the text in the site.

There are sites such as getlocalization that will allow you to store localizations online which translators can then add to. Pootle servers typically use this type of system.

So it all depends on your needs. Having used all of them, I must say that I do like the "getlocalization" type system - it also has the advantage of not having files sent all over the place.

Edited by diafol

Votes + Comments
Thank you. The site isn't so large, So, I think, I'll go with array files. :)
0

One more final question. Why did you suggest me to use, echo sprintf() instead of printf() . What difference does it make?

0

printf() will work just as well of course, but it prints immediately. Using sprintf() means that you can store it in a variable, but from the usage in my example printf() could/should be used.

0

Thank you. The site isn't so large, So, I think, I'll go with array files. :)

OK, fine, but just remember the problems with keeping all those files synchronised - a version control system could help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.