0

I have a web page in PHP and I'm translating with gettext _("STRING_TO_TRANSLATE").

I have in my DB one table with all user profiles for my website. I put them in a selection box to choose one.

Now I want to translate the profile names.

Is there ANY way to translate (USING GETTEXT) the profile names coming from database?

Code example of my selection box:

while($row = mysqli_fetch_array($result_user_type))
{
    echo "<option $selected value=\"".$row['id']."\">".$row['designation']." </option>";
}   
Jorge B.
  • 1,144
  • 2
  • 17
  • 37

3 Answers3

1

Wouldnt you just do...

while($row = mysqli_fetch_array($result_user_type))
{
   echo "<option $selected value=\"".$row['id']."\">"._($row['designation'])." </option>";
}   

Im not sure though, never used gettext or anything but if its just a function that takes an argument and returns a translated string, then this should do it.

Adder
  • 5,708
  • 1
  • 28
  • 56
Kylie
  • 11,421
  • 11
  • 47
  • 78
  • Did you do all steps outlined in this tutorial, for example: http://blog.lingohub.com/developers/2013/07/php-internationalization-with-gettext-tutorial/ – Adder Jun 26 '14 at 11:28
  • @Adder My translations with gettext work perfectly. My problem is to translate the variables from DB. – Jorge B. Jun 26 '14 at 14:40
  • Im pretty sure he knows your problem. He posted that, because what I posted didn't work for you. So he's asking if you set gettext up correctly. And how do you use it in other contexts?? Find them, and just copy the exact same...and put the variable in the brackets of the function. Should work. – Kylie Jun 26 '14 at 21:13
1

You just can't translate PHP variables! The gettext doesn't execute PHP, it just scans your code to get plain strings.

You should look this for example http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/

Cheers

msturdy
  • 10,479
  • 11
  • 41
  • 52
evonline
  • 11
  • 1
  • Do you know any alternative solution for this? – Jorge B. Jun 26 '14 at 14:36
  • Maybe you could build an array like this array( [0] => __('User Type 1'), [1] => __('User type 2') [...] ) and inside your loop you call your $arr_user_type[ $row['id'] ]. Hope it helps. Cheers! – evonline Jun 26 '14 at 17:27
0

To translate my user_type array I created a class that prints the result from the DB to a file translate.php:

public function createArrayType()
{
    $filePHP   = fopen("translate.php", "a");
    $initial = true;

    if (!is_resource($filePHP))
        return false;

    $sql_activity     = "SELECT id, name FROM user_type";
    $result_activity  = mysqli_query( $this->mysqli , $sql_activity  );

    fwrite($filePHP, "\n  \$user_types = array(");
    while($row = mysqli_fetch_array($result_activity))
    {
        if(!$initial)
        fwrite($filePHP, ",");

        fwrite($filePHP, "'".$row['id']."' => _('".$row['name']."')" );

        $initial = false;
    }
    fwrite($filePHP, "); \n");
    fclose($filePHP);
}

Then just use poEdit to translate and use $user_types array:

while($row = mysqli_fetch_array($result_user_type))
{
   echo "<option $selected value=\"".$row['id']."\">".$user_types[$row['id']]." </option>";
}  

I don't know if is it the best option but that options solves my problem.

Jorge B.
  • 1,144
  • 2
  • 17
  • 37