-
-
Notifications
You must be signed in to change notification settings - Fork 136
Migration from v4 to v5
Some functionalities (like support for some formats or translators) have been moved to different packages. Gettext v4 is a monolith containing everything in the same repository. The core of v5 is more light and some features like PHP extractor, Javascript extractor or JSON support are in different packages. The core package only contain support for PO and MO formats.
Gettext 4 contains magic methods to input/output entries. For example:
use Gettext\Translations;
$translations = Translations::fromPoFile('locales/gl.po');
$translations->toMoFile('Locale/gl/LC_MESSAGES/messages.mo');
In Gettext 5 this was removed, in order to provide much more strict types and IDE support:
use Gettext\Loader\PoLoader;
use Gettext\Generator\MoGenerator;
$loader = new PoLoader();
$translations = $loader->loadFile('locales/gl.po');
$generator = new MoGenerator();
$generator->generateFile($translations, 'Locale/gl/LC_MESSAGES/messages.mo');
In Gettext 4 the extractors are responsible to read files and generate a translations. There are extractors for many different formats (po, mo, json, php code, php array, javascript, etc).
use Gettext\Translations;
$poEntries = Translations::fromPoFile('locales.po');
$phpEntries = Translations::fromPhpCodeFile('template.php');
In Gettext 5 this was divided in two groups:
To load the translations from a format intended to store translations. For example the formats mo
, po
or json
are "loaded" because gettext can read and generate these files.
use Gettext\Loader\PoLoader;
$loader = new PoLoader();
$translations = $loader->loadFile('locales.po');
To extract translations from other formats like php code, javascript code, etc. Scanners can load entries of different domains and output several Translations
.
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')
);
//Set a default domain, so any translations with no domain specified, will be added to that domain
$phpScanner->setDefaultDomain('domain1');
$phpScanner->scanFile('template.php');
Not all formats supported by Gettext 4 were ported to Gettext 5. The following formats are not supported currently:
- Blade
- Csv
- Xliff
- Yaml
- VueJs
Thanks to divide the project in different packages, the support for some formats was greatly improved. For example PHP code scanner use nikic/php-parser under the hood so it's much more solid. Javascript support has improved too because it uses mck89/peast to parse the javascript, providing support for modern code and even jsx.