sy/translate

Translation library

1.0.1 2020-12-08 03:02 UTC

This package is auto-updated.

Last update: 2024-12-08 12:40:17 UTC


README

Translation library

Installation

Install the latest version with

$ composer require sy/translate

Basic Usage

PHP translator

Translation data are stored in a PHP file, for example in fr.php:

<?php

return [
	'Hello world' => 'Bonjour monde', 
	...
]
<?php

use Sy\Translate\PhpTranslator;

// Create a translator
$translator = new PhpTranslator();

// Set translation files directory
$translator->setTranslationDir(__DIR__ . '/path/to/directory');

// Set translation file
$translator->setTranslationLang('fr');

// Return 'Bonjour monde'
$translator->translate('Hello world'),

Gettext translator

Translation data are stored in a .mo file, for example in fr.mo

<?php

use Sy\Translate\GettextTranslator;

// Create a translator
$translator = new GettextTranslator();

// Set translation files directory
$translator->setTranslationDir(__DIR__ . '/path/to/directory');

// Set translation file
$translator->setTranslationLang('fr');

// Return 'Bonjour monde'
$translator->translate('Hello world'),