Skip to content
Advertisement

Website language translation in PHP (SQL)

i want to implement in my website a language translation. For this is created a table “translations” in my database:

Based on this i have added the following query into my header:

I want to show the text translation based on the user language. I am wondering if there is a simple php echo method i can use now. As explained above i have the translation in the table available but whta is the easiest method to echo it?

For example. Current state:

New state with translation (of course its not working):

So what i am missing is a simple method to adress the id (row) to this text output which is in that example “1”.

As a possible method i found this example:

I think i can use that and it looks from text size as efficient. But i am not sure how to modify the query for my table above. Best would be i can do somethink like:

and it shows the text output: “Hallo“. And in case:

it will show “Hello“.

What do you think about this method? And can you help me please to modify my query to get this method working ?

Thanks a lot!

Advertisement

Answer

Based on your questions under my first answer i decided to add another solution – easier without functions etc.

Fetch all translations in single query with fetchAll:

Your $textsRaw will look like this:

Now we transform $textsRaw into $texts with easier structure:

Now $texts look like this:

If you have this, then translating is as easy as doing:

If you access translation id that doesnt exist you will get normal php notice: PHP Notice: Undefined offset: 3 in ....php on line ...

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement