almacil/php-translate

1.1.0 2023-10-13 08:23 UTC

This package is not auto-updated.

Last update: 2024-05-24 11:24:53 UTC


README

GitHub last commit GitHub tag (latest by date) Packagist PHP Version Support GitHub

🈳 Almacil PHP Translate

This is a library written in PHP to internationalize websites and apps made with PHP.


Do you want to contribute?
Donate 1€

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>

Do you want to contribute?
Donate 1€

Made with ❤️ by developer for developers