Translator class for based PHP (>= 5.4) systems.
It is a PHP class that provides multi-language system service to your website.
- Translate text with variable
- Supported types: PHP, PHP (SQL), JSON, INI
- Translate SQL querys
- Tiny files and best performance
- Simple usage
- Short functions
Type | Support | Example |
---|---|---|
PHP | ✔️ | Click |
PHP (SQL) | ✔️ | Click |
JSON | ✔️ | Click |
INI | ✔️ | Click |
Installation
composer require benfiratkaya/translator
Loading
use Translator\Translator;
use Translator\Exception;
include_once 'vendor/autoload.php';
If you don't use composer in your project you can include files.
use Translator\Translator;
use Translator\Exception;
include_once 'translator/Exception.php';
include_once 'translator/Register.php';
include_once 'translator/Generator.php';
include_once 'translator/Translator.php';
Supported Types: PHP, PHP (SQL), JSON, INI
PHP (es_ES.php)
return array (
"Hello!" => "Hola!",
"Hello %user%" => "Hola %user%"
);
PHP (SQL) (es_ES.php)
// Connect database
try {
$db = new PDO("mysql:host=localhost;dbname=test", "root", "password");
} catch (PDOException $e) {
echo $e->getMessage();
}
$words = array();
$query = $db->query('SELECT text_en, text_ts FROM Table');
while ($row = $query->fetch()) {
$words[$row['text_en']] = $row['text_es'];
}
return $words;
JSON (es_ES.json)
{
"Hello!": "Hola!",
"Hello %user%": "Hola %user%"
}
INI (es_ES.ini)
Hello! = Hola!
Hello %user% = Hola %user%
// Exception
try {
// Exception Status, Type, Language, Path
$translator = new Translator(true, 'json', 'es_ES', 'translator/languages');
// Register Functions: translate(), translator(), t__(), e__()
$translator->register();
$translator->setException(true); // true or false
// path/lang.type -> /languages/en_US.json
$translator->setType('json'); // php, json, ini
$translator->setLang('es_ES'); // Language Code.
$translator->setPath('translator/languages'); // Language Files Directory
// Update Changes
$translator->update();
} catch (Exception $e) {
echo 'Error: '.$e->errorMessage();
}
Text
echo translator('Hello!'); // Output: Hola!
Text With Variable
$var = 'Firat Kaya';
echo translator('Hello %user%', array('%user%' => $var)); // Output: Hola Firat Kaya
Other Functions
echo translator('Hello!'); // Output: Hola!
echo translate('Hello!'); // Output: Hola!
echo t__('Hello!'); // Output: Hola!
// Does not require the use of 'echo' with this function.
e__('Hello!'); // Output: Hola!
For more information, please refer to the Wiki
Wiki: https://github.com/benfiratkaya/Translator/wiki
Changelog: https://github.com/benfiratkaya/Translator/blob/master/CHANGELOG.md
Distributed under the MIT License. See LICENSE
for more information.
Firat Kaya - benfiratkaya@gmail.com
Project Link: https://github.com/benfiratkaya/Translator