Jump to content

How to set a variable within a function macro??


GeoffEll

Recommended Posts

Dear PHP freaks,

 

I have the following minimal example of my code:

 

-----------------------------------------

if ($Language == 0) {$Help = "Help Me";}

if ($Language == 1) {$Help = "Hilfe Mir";}

if ($Language == 2) {$Help = "Aidez Moi";}

------------------------------------------

 

And I would like to simplify things by making a function, say called translate, so that in the future all I need type in is...

 

------------------------------------------

translate($Help, "Help Me", "Hilfe Mir", "Aidez Moi");

------------------------------------------

 

But something always seems to go wrong. Does anyone have any ideas?

Many thanks in advance,

 

Geoff.

I'd advise against hard-coding translations in the program. Better to store them outside. If you don't have a database use a text file. Then you can add phrases without changing the code

 

One way would be

 

:: test01.txt ::

[help me]
1=hilfe mir
2=aidez moi

[excuse me]
1=enschuldigan sie bitte
2=excusez moi

 

then

<?php
$translations = parse_ini_file('test01.txt', true);

echo translate ('help me', 2);

function translate  ($str, $lang)
{
    global $translations;
    
    if ($lang==0 || !isset($translations[$str]))
        return $str;
    else
        return $translations[$str][$lang];
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.