1. Go to this page and download the library: Download epiconcept/gettext library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
epiconcept / gettext example snippets
use Gettext\Loader\PoLoader;
use Gettext\Generator\MoGenerator;
//import from a .po file:
$loader = new PoLoader();
$translations = $loader->loadFile('locales/gl.po');
//edit some translations:
$translation = $translations->find(null, 'apple');
if ($translation) {
$translation->translate('Mazá');
}
//export to a .mo file:
$generator = new MoGenerator();
$generator->generateFile($translations, 'Locale/gl/LC_MESSAGES/messages.mo');
use Gettext\Translation;
$translation = Translation::create('comments', 'One comment', '%s comments');
$translation->translate('Un comentario');
$translation->translatePlural('%s comentarios');
$translation->getReferences()->add('templates/comments/comment.php', 34);
$translation->getComments()->add('To display the amount of comments in a post');
echo $translation->getContext(); // comments
echo $translation->getOriginal(); // One comment
echo $translation->getTranslation(); // Un comentario
// etc...
use Gettext\Translations;
$translations = Translations::create('my-domain');
//You can add new translations:
$translation = Translation::create('comments', 'One comment', '%s comments');
$translations->add($translation);
//Find a specific translation
$translation = $translations->find('comments', 'One comment');
//Edit headers, domain, etc
$translations->getHeaders()->set('Last-Translator', 'Oscar Otero');
$translations->setDomain('my-blog');
use Gettext\Loader\PoLoader;
$loader = new PoLoader();
//From a file
$translations = $loader->loadFile('locales/en.po');
//From a string
$string = file_get_contents('locales2/en.po');
$translations = $loader->loadString($string);
use Gettext\Loader\PoLoader;
use Gettext\Generator\MoGenerator;
//Load a PO file
$poLoader = new PoLoader();
$translations = $poLoader->loadFile('locales/en.po');
//Save to MO file
$moGenerator = new MoGenerator();
$moGenerator->generateFile($translations, 'locales/en.mo');
//Or return as a string
$content = $moGenerator->generateString($translations);
file_put_contents('locales/en.mo', $content);
use Gettext\Scanner\PhpScanner;
use Gettext\Translations;
//Create a new scanner, adding a translation for each domain we want to get:
$phpScanner = new PhpScanner(
Translations::create('domain1'),
Translations::create('domain2'),
Translations::create('domain3')
);
//Set a default domain, so any translations with no domain specified, will be added to that domain
$phpScanner->setDefaultDomain('domain1');
//Extract all comments starting with 'i18n:' and 'Translators:'
$phpScanner->extractCommentsStartingWith('i18n:', 'Translators:');
//Scan files
foreach (glob('*.php') as $file) {
$phpScanner->scanFile($file);
}
//Get the translations
list('domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3) = $phpScanner->getTranslations();