Skip to content
Advertisement

Implementation of multilanguage in PHP

I was wondering how to implement multiple languages in PHP script. I couldn’t really find any handy way of how such thing should be done to make it easy for translator. I’ll give an example:

JavaScript

If the file outputs such text shattered by many different variables and code, how the language file should look like?

I came up with idea to keep the user language in $_SESSION[‘lang’], and in each file include file with correct language. But after I tried to do so, it does look like this:

JavaScript

It looks ridiculous, unreadable, and with many potential mistakes. And just imagine if You are translator that gets access only to the lang file – it is impossible to translate such text, right? How this should/could be done differently?

Advertisement

Answer

Yes, its done like that but not individual words or blocks of the sentence like your doing, else it would not translate well.

How it’s generally done to handle the problem, you define your templates and then have a function which you call with passed params.

see: https://www.php.net/manual/en/refs.international.php for getext etc, or find a lib.

Example:

JavaScript

https://3v4l.org/e1nEB

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