PHP Multi Language Library
This class is a PHP library that allows you to add multi-language support to your project., (*1)
Run the following code in the directory of your project., (*3)
composer require mirac/multilanguage dev-master, (*4)
If there is no Composer autoload command on your page, add the following command at the beginning of your page., (*5)
require_once __DIR__ . '/vendor/autoload.php';
Let's continue below this line. Since we will create a 'lang' folder in the project directory, add the following commands., (*6)
use Mirac\MultiLanguage\Create; Create::folder();
After adding these lines to your page, access it through your browser. You will see a folder named 'lang' created in your directory., (*7)
We will add all language files to this folder later., (*8)
After seeing that the folder has been created, let's delete the two lines we added earlier because our job is done., (*9)
/* use Mirac\MultiLanguage\Create; Create::folder(); */
Instead, let's add the following lines., (*10)
use Mirac\MultiLanguage\Language; use Mirac\MultiLanguage\Select; $lang = new Language(); $select = new Select();
That's all for configuration. Now we can add languages., (*11)
To translate files, we need to create folders with country codes inside the created 'lang' folder in our directory., (*14)
Example:, (*15)
For Turkish, we create a folder named 'tr'. Then we add our translation files to it., (*16)
You can add a folder and translation file for any language you want., (*17)
Let's create a language file named 'homepage' for Turkish. Depending on the size of your site, you can duplicate your files. But don't forget to translate the same files!, (*18)
Let's create our file containing the translations of our homepage in the 'tr' folder as 'homepage.php'., (*19)
lang/tr/homepage.php, (*20)
```php <?php, (*21)
return [ 'title' => 'Homepage', 'contact' => 'Contact', 'page' => 'Page', 'photo' => 'Photo' ];, (*22)
``` You can translate this file into any language you want., (*23)
lang/en/homepage.php, (*24)
```php <?php, (*25)
return [ 'title' => 'Homepage', 'contact' => 'Contact', 'page' => 'Page', 'photo' => 'Photo' ];, (*26)
```, (*27)
lang/de/homepage.php, (*28)
```php <?php, (*29)
return [ 'title' => 'Homepage', 'contact' => 'Contact', 'page' => 'Page', 'photo' => 'Photo' ];, (*30)
```, (*31)
You can add unlimited languages and files like this., (*32)
After creating the files, we use the following command to use them on our pages., (*34)
$lang->lang('filename.key');, (*35)
For Example:, (*36)
$lang->lang('homepage.photo');, (*37)
This example command will give us the translation of the 'photo' key in the 'homepage.php' file inside the selected language folder., (*38)
```php <?php, (*39)
echo "Menus ".
$lang->lang('homepage.title')."
".
$lang->lang('homepage.photo')."
".
$lang->lang('homepage.contact')."
";
```, (*40)
You can use it in your code as shown in the example above., (*41)
### Visitor Language Selection, (*42)
To allow your visitors to choose a language, you can integrate the following command into your page., (*43)
$select->language('tr'), (*44)
$select->language('en'), (*45)
$select->language('de'), (*46)
$select->language('..'), (*47)
Example:
php
echo "<a href='".$select->language('tr')."'>TR</a><br>";
echo "<a href='".$select->language('en')."'>EN</a><br>";
echo "<a href='".$select->language('de')."'>DE</a><br>";
, (*48)
By providing links like this, when the visitor clicks, the language file you added will be activated automatically., (*49)
These language names are connected to the language files we created in the 'lang' folder., (*50)