almacil / php-translate
Fund package maintenance!
rubenperezlopez
Requires
- php: >=7.0.0
This package is not auto-updated.
Last update: 2024-05-24 11:24:53 UTC
README
🈳 Almacil PHP Translate
This is a library written in PHP to internationalize websites and apps made with PHP.
Features
- Translate from json files
- Translate words or phrases on the fly with Google Translate and store to json files
Installation
Installation is possible using Composer
composer require almacil/php-translate
Usage
Create an instance of \Almacil\Translate:
// Require composer autoloader require __DIR__ . '/vendor/autoload.php'; // Language to translate from auto detectable language $lang = 'en'; // Directory containing the json files with the translations $directory = __DIR__ . '/i18n'; // We want the library to search for the translations that it cannot find in the files and to include the translations in the files $findMissingTranslations = true; // Create de instance $translate = new \Almacil\Translate($lang, $directory, $findMissingTranslations); echo $translate->get('Hola mundo!'); // Hello world!
Create a function with a short name for ease:
// ... after the setup function t($text, $params = null) { global $translate; return $translate->get($text, $params); } echo '<p>' . t('Hola mundo!') . '</p>'; // <p>Hello World!</p>
Params
We can include parameters in translations like this:
// ./i18n/en.json { "hola-name": "Hello {{name}}!" }
// Some PHP file echo '<p>' . t('hola-name', array('name' => 'Rubén')) . '</p>'; // <p>Hello Rubén!</p>
Made with ❤️ by developer for developers