How best to implement localization in php

or

define("_MENU","");
echo '<a href="/menu">'._MENU.'</a>'; //  

Answer 1, authority 100%

It’s enough just to include different templates depending on the settings, i.e. for example, all templates are stored in the view
folder
For example, we describe the Localization class that takes the language value, in the view folder we create folders like ru, ua, en. The task of this class will be very simple – to connect templates from these folders, depending on the accepted parameters, if the language is 2-3 it is normal practice, if more, then naturally you need to use the database. But again, a class like Localization will help us here. Simply then, its task will be, depending on the accepted parameters, to give data to the templates in one language or another.

PS: The Localization class is best done as a singleton


Answer 2, authority 40%

If there are not enough system phrases – the second way. Moreover, connected from a separate file.
if there is a lot, it is better through the database: it is easier to edit and manage, although the processing time increases slightly.
In Drupal, for example, there is a function

t($str)

, which searches the database for the English version of the string and returns its counterpart in the requested language (specified in the settings). If nothing is found or if the connection fails, the original string is returned. This allows you to use even the emergency option.


Answer 3, authority 40%

I implemented it in sessions. All dialogs are located in the ru.php and en.php files and are defined by constants. And there is a check in the undex.phpfile: if ($_SESSION['lang']=='ru') {include_once'ru.php';} else {include_once'en.php';}.
Articles are stored in the database, in the tables of articles and news there are fields text_rusand text_eng.


Answer 4, authority 20%

The nicest way I’ve seen is to use some function/class
the incoming parameters will contain text and language. The function looks in its cache (which preloads from the database) to see if there is such a translation for the given text into the desired language, if there is, then it returns it, if not, then it adds an entry to the table with the same translation as the translated word itself.
And in the database you already translate this word as you want. After the translation, you need to reset the cache.


Answer 5

Just yesterday I did something similar to myself…
We make a form that sends the value rus and eng to 2.
Next php.

<?
//   ru   $_SESSION['lang']   ru
if($_POST['ru']){
$_SESSION['lang'] = $_POST['ru'];
}
   en   $_SESSION['lang']   en
if($_POST['en']){
$_SESSION['lang'] = $_POST['en'];
}
if(!$_SESSION['lang']) { include("lang/ru.php"); }//   -    - 
//   ...
if($_SESSION['lang'] == russian) { include("lang/ru.php"); }
if($_SESSION['lang'] == english) { include("lang/en.php"); }
?>
<? echo $lang['find']; ?> //

The ru.php and en.php files contain the $langarray.

If you have several scattered pages, then lines

if($_SESSION['lang'] == russian) { include("lang/ru.php"); }
if($_SESSION['lang'] == english) { include("lang/en.php"); }

must be inserted on every page AT THE BEGINNING.


Answer 6

I did the following.
Content translation – in the database, in the corresponding fields of the product.name_rutype (or in a separate tablefor localization ).
UI translation – in separate files, with a name, for example /view/themes/autumn_in_yellow/lang/ru.php. We create our own file for each language. The content of the localization files is something like this:

$t['catalog.minicart.content.after_nbr'] =