gettext-bilal-azzam / gettext
PHP gettext manager
Requires
- php: ^7.2
- gettext/languages: ^2.3
Requires (Dev)
- brick/varexporter: ^0.2.1
- friendsofphp/php-cs-fixer: ^2.15
- oscarotero/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.0
- dev-master
- v5.4.1
- v5.4.0
- v5.3.0
- v5.2.2
- v5.2.1
- v5.2.0
- v5.1.0
- v5.0.0
- 4.x-dev
- v4.8.2
- v4.8.1
- v4.8.0
- v4.7.0
- v4.6.3
- v4.6.2
- v4.6.1
- v4.6.0
- v4.5.0
- v4.4.4
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4.0
- v4.3.0
- v4.2.0
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- 3.x-dev
- v3.6.1
- v3.6.0
- v3.5.9
- v3.5.8
- v3.5.7
- v3.5.6
- v3.5.5
- v3.5.4
- v3.5.3
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4
- v3.3
- v3.2
- v3.1.1
- v3.1
- v3.0
- v2.3.0
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.1
- v2.1
- 2.0
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- dev-feature/php-messages-only
- dev-feature/functions-handlers-trait
This package is auto-updated.
Last update: 2025-02-20 23:26:37 UTC
README
Created by Oscar Otero http://oscarotero.com oom@oscarotero.com (MIT License)
Gettext is a PHP (>=5.4) library to import/export/edit gettext from PO, MO, PHP, JS files, etc.
Installation
With composer (recomended):
composer require gettext/gettext
If you don't use composer in your project, you have to download and place this package in a directory of your project. You need to install also gettext/languages. Then, include the autoloaders of both projects in any place of your php code:
include_once "libs/gettext/src/autoloader.php"; include_once "libs/cldr-to-gettext-plural-rules/src/autoloader.php";
Classes and functions
This package contains the following classes:
Gettext\Translation
- A translation definitionGettext\Translations
- A collection of translationsGettext\Extractors\*
- Import translations from various sources (po, mo, php, js, etc)Gettext\Generators\*
- Export translations to various formats (po, mo, php, json, etc)Gettext\Translator
- To use the translations in your php templates instead the gettext extensionGettext\GettextTranslator
- To use the gettext extension
Usage example
use Gettext\Translations; //import from a .po file: $translations = Translations::fromPoFile('locales/gl.po'); //edit some translations: $translation = $translations->find(null, 'apple'); if ($translation) { $translation->setTranslation('Mazá'); } //export to a php array: $translations->toPhpArrayFile('locales/gl.php'); //and to a .mo file $translations->toMoFile('Locale/gl/LC_MESSAGES/messages.mo');
If you want use this translations in your php templates without using the gettext extension:
use Gettext\Translator; //Create the translator instance $t = new Translator(); //Load your translations (exported as PhpArray): $t->loadTranslations('locales/gl.php'); //Use it: echo $t->gettext('apple'); // "Mazá" //If you want use global functions: $t->register(); echo __('apple'); // "Mazá"
To use this translations with the gettext extension:
use Gettext\GettextTranslator; //Create the translator instance $t = new GettextTranslator(); //Set the language and load the domain $t->setLanguage('gl'); $t->loadDomain('messages', 'Locale'); //Use it: echo $t->gettext('apple'); // "Mazá" //Or use the gettext functions echo gettext('apple'); // "Mazá" //If you want use the global functions $t->register(); echo __('apple'); // "Mazá" //And use sprintf/strtr placeholders echo __('Hello %s', 'world'); //Hello world echo __('Hello {name}', ['{name}' => 'world']); //Hello world
The benefits of using the functions provided by this library (__()
instead _()
or gettext()
) are:
- You are using the same functions, no matter whether the translations are provided by gettext extension or any other method.
- You can use variables easier because
sprintf
functionality is included. For example:__('Hello %s', 'world')
insteadsprintf(_('Hello %s'), 'world')
. - You can also use named placeholders if the second argument is an array. For example:
__('Hello %name%', ['%name%' => 'world'])
instead ofstrtr(_('Hello %name%'), ['%name%' => 'world'])
.
Translation
The Gettext\Translation
class stores all information about a translation: the original text, the translated text, source references, comments, etc.
// __construct($context, $original, $plural) $translation = new Gettext\Translation('comments', 'One comment', '%s comments'); $translation->setTranslation('Un comentario'); $translation->setPluralTranslation('%s comentarios'); $translation->addReference('templates/comments/comment.php', 34); $translation->addComment('To display the amount of comments in a post'); echo $translation->getContext(); // comments echo $translation->getOriginal(); // One comment echo $translation->getTranslation(); // Un comentario // etc...
Translations
The Gettext\Translations
class stores a collection of translations:
$translations = new Gettext\Translations(); //You can add new translations using the array syntax $translations[] = new Gettext\Translation('comments', 'One comment', '%s comments'); //Or using the "insert" method $insertedTranslation = $translations->insert('comments', 'One comment', '%s comments'); //Find a specific translation $translation = $translations->find('comments', 'One comment'); //Edit headers, domain, etc $translations->setHeader('Last-Translator', 'Oscar Otero'); $translations->setDomain('my-blog');
Extractors
The extrators allows to fetch gettext values from any source. For example, to scan a .po file:
$translations = new Gettext\Translations(); //From a file Gettext\Extractors\Po::fromFile('locales/en.po', $translations); //From a string $string = file_get_contents('locales2/en.po'); Gettext\Extractors\Po::fromString($string, $translations);
The better way to use extractors is using the magic methods of Gettext\Translations
:
//Create a Translations instance using a po file $translations = Gettext\Translations::fromPoFile('locales/en.po'); //Add more messages from other files $translations->addFromPoFile('locales2/en.po');
The available extractors are the following:
Generators
The generators export a Gettext\Translations
instance to any format (po, mo, array, etc).
//Save to a file Gettext\Generators\Po::toFile($translations, 'locales/en.po'); //Return as a string $content = Gettext\Generators\Po::toString($translations); file_put_contents('locales/en.po', $content);
Like extractors, the better way to use generators is using the magic methods of Gettext\Translations
:
//Extract messages from a php code file $translations = Gettext\Translations::fromPhpCodeFile('templates/index.php'); //Export to a po file $translations->toPoFile('locales/en.po'); //Export to a po string $content = $translations->toPoString(); file_put_contents('locales/en.po', $content);
The available generators are the following:
Translator
The class Gettext\Translator
implements the gettext functions in php. Useful if you don't have the native gettext extension for php or want to avoid problems with it. You can load the translations from a php array file or using a Gettext\Translations
instance:
use Gettext\Translator; //Create a new instance of the translator $t = new Translator(); //Load the translations using any of the following ways: // 1. from php files (generated by Gettext\Extractors\PhpArray) $t->loadTranslations('locales/gl.php'); // 2. using the array directly $array = include 'locales/gl.php'; $t->loadTranslations($array); // 3. using a Gettext\Translations instance (slower) $translations = Gettext\Translations::fromPoFile('locales/gl.po'); $t->loadTranslations($translations); //Now you can use it in your templates echo $t->gettext('apple');
GettextTranslator
The class Gettext\GettextTranslator
uses the gettext extension. It's useful because combines the performance of using real gettext functions but with the same API than Translator
class, so you can switch to one or other translator deppending of the environment without change code of your app.
use Gettext\GettextTranslator; //Create a new instance $t = new GettextTranslator(); //It detects the environment variables to set the locale, but you can change it: $t->setLanguage('gl'); //Load the domains: $t->loadDomain('messages', 'project/Locale'); //this means you have the file "project/Locale/gl/LC_MESSAGES/messages.po" //Now you can use it in your templates echo $t->gettext('apple');
Global functions
To ease the use of translations in your php templates, you can use the provided functions:
//Register the translator to use the global functions $t->register(); echo __('apple'); // it's the same than $t->gettext('apple');
You can scan the php files containing these functions and extract the values with the PhpCode extractor:
<!-- index.php --> <html> <body> <?= __('Hello world'); ?> </body> </html>
Merge translations
To work with different translations you may want merge them in an unique file. There are two ways to do this:
The simplest way is adding new translations:
use Gettext\Translations; $translations = Translations::fromPoFile('my-file1.po'); $translations->addFromPoFile('my-file2.po');
A more advanced way is merge two Translations
instances:
use Gettext\Translations; //Create a new Translations instances with our translations. $translations1 = Translations::fromPoFile('my-file1.po'); $translations2 = Translations::fromPoFile('my-file2.po'); //Merge one inside other: $translations1->mergeWith($translations2); //Now translations1 has all values
The second argument of mergeWith
defines how the merge will be done. Use the Gettext\Merge
constants to configure the merging:
Example:
use Gettext\Translations; use Gettext\Merge; //Scan the php code to find the latest gettext translations $phpTranslations = Translations::fromPhpCodeFile('my-templates.php'); //Get the translations of the code that are stored in a po file $poTranslations = Translations::fromPoFile('locale.po'); //Merge the translations from the po file using the references from `$phpTranslations`: $translations->mergeWith($poTranslations, Merge::REFERENCES_OURS); //Now save a po file with the result $translations->toPoFile('locale.po');
Note, if the second argument is not defined, the default value is Merge::DEFAULTS
that's equivalent to Merge::ADD | Merge::HEADERS_ADD
.
Use from CLI
There's a Robo task to use this library from the command line interface: https://github.com/oscarotero/GettextRobo
Use in the browser
If you want to use your translations in the browser, there's a javascript translator: https://github.com/oscarotero/gettext-translator
Third party packages
Twig integration:
Framework integration:
Contributors
Thanks to all contributors specially to @mlocati.
Donations
If this library is useful for you, consider to donate to the author.
Thanks in advance!