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.

Recommended Answers

All 5 Replies

Member Avatar for diafol

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);
commented: 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. +0
Member Avatar for diafol

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.

commented: 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?

Member Avatar for diafol

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.

Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.