Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unable to use gettext; alternatives?

0 views
Skip to first unread message

Michiel

unread,
Oct 1, 2002, 9:35:08 AM10/1/02
to
Hi,

I'm asked to do a multilingual script for a client who's hosted on a
server which does not offer gettext support in PHP. Is there an
attractive alternative?

The solution I thought up was to place all language strings in an array
and index it with a number, something like this:

en-strings.php:
$Str[0]="ad...@site.co.uk";
$Str[1]="You are logged out";
$Str[2]="Hello, World!";

de-strings.php:
$Str[0]="ad...@site.de";
$Str[1]="Du bist abgelogt";
$Str[2]="Gutentag, Welt!";

etc, for each language one file.
And then use it like this:

$langcode="es"; // español

include $langcode."-strings.php";

echo Str[2]; // Hello World! (outputs: !Ola, Mundo!)

$langcode="de"; // deutsch

include $langcode."-strings.php";

echo Str[2]; // Hello World! (outputs: Gutentag, Welt!)


--
Spam is spam no matter who's doing it or for what reason.

Henrik Hansen

unread,
Oct 1, 2002, 9:43:02 AM10/1/02
to
(Michiel) wrote:
> Hi,
>
> I'm asked to do a multilingual script for a client who's hosted on a
> server which does not offer gettext support in PHP. Is there an
> attractive alternative?
>
> The solution I thought up was to place all language strings in an array
> and index it with a number, something like this:
>

I've used that method some times too and it's working good... though
maybe you should make more includes if you get too many strings so you
dont include 100 strings for no use, but you only said "a multilingual
script" so it does not sound like thats a problem. Another was is to
make a string table in your database so easier update, but if the coder
is the only that has to update the strings, the file based version is fine.

--
Henrik Hansen

stephan

unread,
Oct 1, 2002, 10:03:43 AM10/1/02
to


Even better, write a simple function:

function my_translate( $string ) {
...
}

and use it to do your translations, then you can change from file-based to
db based without changing the rest of your app.

--
----- stephan
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Henrik Hansen

unread,
Oct 1, 2002, 10:10:05 AM10/1/02
to
stephan wrote:
> Henrik Hansen wrote:
>
>>(Michiel) wrote:
>>
>>>Hi,
>>>
>>>I'm asked to do a multilingual script for a client who's hosted on a
>>>server which does not offer gettext support in PHP. Is there an
>>>attractive alternative?
>>>
>>>The solution I thought up was to place all language strings in an array
>>>and index it with a number, something like this:
>>>
>>
>>I've used that method some times too and it's working good... though
>>maybe you should make more includes if you get too many strings so you
>>dont include 100 strings for no use, but you only said "a multilingual
>>script" so it does not sound like thats a problem. Another was is to
>>make a string table in your database so easier update, but if the coder
>>is the only that has to update the strings, the file based version is
>>fine.
>>
>
>
>
> Even better, write a simple function:
>
> function my_translate( $string ) {
> ...
> }
>
> and use it to do your translations, then you can change from file-based to
> db based without changing the rest of your app.

thats true, thats a good way of making sure you dont make your app
obsolete ;) just be sure only to include your array file one time per
script run when using the function so you dont "make" the array 100 time
per script run, if y'all know what I am blabbing about ;)

--
Henrik Hansen

stephan

unread,
Oct 1, 2002, 10:36:09 AM10/1/02
to
Henrik Hansen wrote:
> thats true, thats a good way of making sure you dont make your app
> obsolete ;) just be sure only to include your array file one time per
> script run when using the function so you dont "make" the array 100 time
> per script run, if y'all know what I am blabbing about ;)


hypothetical:
function my_translate($key) {
$lang = my_config_get( "lang" );
include_once( "translations.$lang.php" );
global $TRANSLATIONS;
return $TRANSLATIONS[$key] || $key;
}

(obviously needs more error checking)

Croc O'Dile

unread,
Oct 3, 2002, 6:56:17 PM10/3/02
to
Well you can use the CtNls class as described in PHPBuilder.com. I believe
it is also available from ftp://ftp.coralys.com/ somewhere in the "free"
section (last time I saw).

"stephan" <ste...@wanderinghorse.net> wrote in message
news:ancbsn$99k$1...@ork.backup.noris.net...

0 new messages