benfiratkaya / translator
Translator class for based PHP systems.
Requires
- php: >=5.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.2
- phpunit/phpunit: ~4.1
This package is auto-updated.
Last update: 2025-05-21 22:58:00 UTC
README
Translator class for based PHP (>= 5.4) systems.
About the Project
What is this?
It is a PHP class that provides multi-language system service to your website.
Features:
- Translate text with variable
- Supported types: PHP, PHP (SQL), JSON, INI
- Translate SQL querys
- Tiny files and best performance
- Simple usage
- Short functions
Supported Types
Type | Support | Example |
---|---|---|
PHP | ✔️ | Click |
PHP (SQL) | ✔️ | Click |
JSON | ✔️ | Click |
INI | ✔️ | Click |
Installation & Loading
Composer (recomended):
Installation
composer require benfiratkaya/translator
Loading
use Translator\Translator; use Translator\Exception; include_once 'vendor/autoload.php';
Github:
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';
Usage
Language File:
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:
// 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(); }
Translate:
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
Useful Links
Wiki: https://github.com/benfiratkaya/Translator/wiki
Changelog: https://github.com/benfiratkaya/Translator/blob/master/CHANGELOG.md
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Firat Kaya - benfiratkaya@gmail.com
Project Link: https://github.com/benfiratkaya/Translator