Show in other languages from database

Can anyone help with this,

the below will basically get the « sex » value from my database table,

So it will show « Boy » or « Girl » depending on the value,

$Details= “({sex})”;

What i am trying to do is get « sex » in another language on the result, my database is only english but would like results to show in other languages,

Is there an easy way to do this ?

I want to avoid doing my Database in different languages

Thanks for replies

Well, PHP does not really translate by itself. If you have a valid Google paid account, you can use their Google-Translate-API. It lets you translate to most any language using PHP. Google it and you can find the details. You have to pay for a key and I think it is a monthly charge.

Another way is to use the “gettext” function. This requires two modules to be installed into PHP and also language files to be downloaded for each language you want to use. The use of the function once the modules and language files are installed is fairly easy. ( I have never played with it but have read about it.) It might be a way to go. You basically set the locale of the area the user is in and then use a function named " _( ) " to echo text with. All the text in the ( ) are translated to the language of the locale.
Here is a link for a short tutorial on it: Gettext-Translation

Thanks for the reply, not really what i’m looking for, thought there could be an easier way to do it.
Everything else is translated by echo statements with no problems, its just that one thing that i cannot find a way around it.
Maybe with an if or else statement just dont know how the result from “sex” can be included in an echo with if and else.
Basically

If “sex” = “boy”;
echo “Boy”;
else echo 'Girl"

Then i could just change "Boy or “Girl” to another Language.

Surely there must ne a way to do it ?

If it’s just a matter of translating a single word depending on the current language of your site, why not just create a map with different languages, something like this:

$languageMap = [
    'es' => [
        'boy' => 'niño',
        'girl' => 'niña'
    ]
];

Then you can access your array with:

$languageMap['es']['boy'];

If you are not satisfied with a simple translation like that, you can use a ready library, like a Symfony translations component: Translations (Symfony Docs) . It all depends how much of a complex solution you need.

Thankyou but that will not work, that is how i can translate everything else except that.
the problem is it gets the “sex” from the database table (which can be girl or boy)
then will echo “Girl” or “Boy”

I need something that will get the “sex” from the datebase and translate into the desired language before it echos the result.

Really not sure if its possible to do, there are over 50 thousand entries in my database so is nearly impossible to add the “sex” of every entry in different Languages.

I guess I must be missing something here?

If you already have the boy/girl value RETURNED from your query… why not do a simple translation before outputting to the screen.

The problem is WHAT LANGUAGE? You’ll need to (more or less) create a look-up list.

jpajak’s solution seems like the right path for this, and can be used like you expressed… so I’m not clear as to why ‘it wont work’?

thanks for your reply, maybe i didnt explain properly

I have this $Details= “({sex})”;
which which will get the “sex” from my database which can be either “Boy” or “Girl” as the result shown on the page.

If i change $Details= “({sex})” to $Details= “({anything})” the result shown on the page will say (anything)

What i want to do is change $Details= “({sex})” to something that will get “sex” from the datebase and translate from “Boy” or “Girl” so that the resulting sex will show on the page in the language i want.
I have tried Language files with arrays etc, i just cant work out what i need to replace $Details= “({sex})” with.

I agree with Whispers! Also, let the user change the language using the browser’s language options.
Most people do that now anyways…

You typed just as I did.

If you get the value into $Details, use that as your index into your language files.
$results = $language_file[$Details][“French”];
Since you only give us small parts of your system we have to guess.

If you already have the language files, what are the formats of them?
The gettext() functions that I mentioned earlier will do translations to many many other languages.

What language do you need to translate into and what structure is in your “language-files”?

In my config.php file i have this : $Details= “({sex}), {dob:Y-m-d},”;

on the results page i have this : PrintReportHeader(“Details of:”, $details, true);

I will try your solution i think you pointed me in the right direction it was the following i couldnt work out
$results = $language_file[$Details][“French”];
Everything else on my website is in French or English.
Thankyou.

Well, you are setting the $Details variable to a field which is filled in another area, you do not know at that point what you have for data. Therefore, you can not use it as an index into the language files directly.
You need to get the value of sex, wherever you convert the template field {sex} into the actual data. Then, there you can use the real value as an index. At some point you load the template fields with the live data from the database. That would be the place that you would convert each field to the correct language. And, you would do that BEFORE you store the value into the template fields. ( In this case {sex}…

Sponsor our Newsletter | Privacy Policy | Terms of Service