2017 © Pedro Peláez
 

library gettext

PHP gettext manager

image

gettext/gettext

PHP gettext manager

  • Tuesday, June 26, 2018
  • by oscarotero
  • Repository
  • 28 Watchers
  • 388 Stars
  • 899,604 Installations
  • PHP
  • 59 Dependents
  • 2 Suggesters
  • 102 Forks
  • 8 Open issues
  • 54 Versions
  • 16 % Grown

The README.md

Gettext

Latest Version on Packagist ![Software License][ico-license] !ico-ga [![Total Downloads][ico-downloads]]link-downloads, (*1)

Note: this is the documentation of the new 5.x version. Go to 4.x branch if you're looking for the old 4.x version, (*2)

Created by Oscar Otero http://oscarotero.com oom@oscarotero.com (MIT License), (*3)

Gettext is a PHP (^7.2) library to import/export/edit gettext from PO, MO, PHP, JS files, etc., (*4)

Installation

composer require gettext/gettext

Classes and functions

This package contains the following classes:, (*5)

  • Gettext\Translation - A translation definition
  • Gettext\Translations - A collection of translations (under the same domain)
  • Gettext\Scanner\* - Scan files to extract translations (php, js, twig templates, ...)
  • Gettext\Loader\* - Load translations from different formats (po, mo, json, ...)
  • Gettext\Generator\* - Export translations to various formats (po, mo, json, ...)

Usage example

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');

Translation

The Gettext\Translation class stores all information about a translation: the original text, the translated text, source references, comments, etc., (*6)

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...

Translations

The Gettext\Translations class stores a collection of translations:, (*7)

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');

Loaders

The loaders allow to get gettext values from multiple formats. For example, to load a .po file:, (*8)

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);

As of version 5.7.0, a StrictPoLoader has been included, with a parser more aligned to the GNU gettext tooling with the same expectations and failures (see the tests for more details). - It will fail with an exception when there's anything wrong with the syntax, and display the reason together with the line/byte where it happened. - It might also emit useful warnings, e.g. when there are more/less plural translations than needed, missing translation header, dangling comments not associated with any translation, etc. - Due to its strictness and speed (about 50% slower than the PoLoader), it might be interesting to be used as a kind of .po linter in a build system. - It also implements the previous translation comment (e.g. #| msgid "previous") and extra escapes (16-bit unicode \u, 32-bit unicode \U, hexadecimal \xFF and octal \77)., (*9)

The usage is basically the same as the PoLoader:, (*10)

use Gettext\Loader\StrictPoLoader;

$loader = new StrictPoLoader();

//From a file
$translations = $loader->loadFile('locales/en.po');

//From a string
$string = file_get_contents('locales2/en.po');
$translations = $loader->loadString($string);

//Display error messages using "at line X column Y" instead of "at byte X"
$loader->displayErrorLine = true;
//Throw an exception when a warning happens
$loader->throwOnWarning = true;
//Retrieve the warnings
$loader->getWarnings();

This package includes the following loaders:, (*11)

  • MoLoader
  • PoLoader
  • StrictPoLoader

And you can install other formats with loaders and generators:, (*12)

Generators

The generators export a Gettext\Translations instance to any format (po, mo, etc)., (*13)

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);

This package includes the following generators:, (*14)

  • MoGenerator
  • PoGenerator

And you can install other formats with loaders and generators:, (*15)

Scanners

Scanners allow to search and extract new gettext entries from different sources like php files, twig templates, blade templates, etc. Unlike loaders, scanners allows to extract gettext entries with different domains at the same time:, (*16)

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();

This package does not include any scanner by default. But there are some that you can install:, (*17)

Merging translations

You will want to update or merge translations. The function mergeWith create a new Translations instance with other translations merged:, (*18)

$translations3 = $translations1->mergeWith($translations2);

But sometimes this is not enough, and this is why we have merging options, allowing to configure how two translations will be merged. These options are defined as constants in the Gettext\Merge class, and are the following:, (*19)

Constant Description
Merge::TRANSLATIONS_OURS Use only the translations present in $translations1
Merge::TRANSLATIONS_THEIRS Use only the translations present in $translations2
Merge::TRANSLATIONS_OVERRIDE Override the translation and plural translations with the value of $translation2
Merge::HEADERS_OURS Use only the headers of $translations1
Merge::HEADERS_REMOVE Use only the headers of $translations2
Merge::HEADERS_OVERRIDE Overrides the headers with the values of $translations2
Merge::COMMENTS_OURS Use only the comments of $translation1
Merge::COMMENTS_THEIRS Use only the comments of $translation2
Merge::EXTRACTED_COMMENTS_OURS Use only the extracted comments of $translation1
Merge::EXTRACTED_COMMENTS_THEIRS Use only the extracted comments of $translation2
Merge::FLAGS_OURS Use only the flags of $translation1
Merge::FLAGS_THEIRS Use only the flags of $translation2
Merge::REFERENCES_OURS Use only the references of $translation1
Merge::REFERENCES_THEIRS Use only the references of $translation2

Use the second argument to configure the merging strategy:, (*20)

$strategy = Merge::TRANSLATIONS_OURS | Merge::HEADERS_OURS;

$translations3 = $translations1->mergeWith($translations2, $strategy);

There are some typical scenarios, one of the most common:, (*21)

  • Scan php templates searching for entries to translate
  • Complete these entries with the translations stored in a .po file
  • You may want to add new entries to the .po file
  • And also remove those entries present in the .po file but not in the templates (because they were removed)
  • But you want to update some translations with new references and extracted comments
  • And keep the translations, comments and flags defined in .po file

For this scenario, you can use the option Merge::SCAN_AND_LOAD with the combination of options to fit this needs (SCAN new entries and LOAD a .po file)., (*22)

$newEntries = $scanner->scanFile('template.php');
$previousEntries = $loader->loadFile('translations.po');

$updatedEntries = $newEntries->mergeWith($previousEntries);

More common scenarios may be added in a future., (*23)

Contributors

Thanks to all contributors specially to @mlocati., (*24)


Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*25)

The MIT License (MIT). Please see LICENSE for more information., (*26)

The Versions

04/03 2017

v4.3.0

4.3.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

14/01 2017

v4.2.0

4.2.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

21/09 2016

v4.1.0

4.1.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

28/08 2016

v4.0.2

4.0.2.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

01/08 2016

3.x-dev

3.9999999.9999999.9999999-dev https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

01/08 2016

v3.6.1

3.6.1.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

01/08 2016

v4.0.1

4.0.1.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

15/06 2016

v4.0.0

4.0.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

27/04 2016

v3.6.0

3.6.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

19/02 2016

v3.5.9

3.5.9.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

20/01 2016

v3.5.8

3.5.8.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

22/12 2015

v3.5.7

3.5.7.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

17/11 2015

v3.5.6

3.5.6.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

11/11 2015

v3.5.5

3.5.5.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

28/10 2015

v3.5.4

3.5.4.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

27/08 2015

v3.5.3

3.5.3.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

26/08 2015

v3.5.2

3.5.2.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

26/08 2015

v3.5.1

3.5.1.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

25/08 2015

v3.5.0

3.5.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

13/08 2015

v3.4.3

3.4.3.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

28/04 2015

v3.4.2

3.4.2.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

28/04 2015

v3.4.1

3.4.1.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

06/04 2015

v3.4

3.4.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

The Development Requires

translation js i18n gettext mo po

16/02 2015

v3.3

3.3.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

 

translation js i18n gettext mo po

12/02 2015

v3.2

3.2.0.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

02/02 2015

v3.1.1

3.1.1.0 https://github.com/oscarotero/Gettext

PHP gettext manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

28/01 2015

v3.1

3.1.0.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

22/01 2015

v3.0

3.0.0.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

19/01 2015

v2.3.0

2.3.0.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

07/01 2015

v2.2.3

2.2.3.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

07/01 2015

v2.2.2

2.2.2.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

06/01 2015

v2.2.1

2.2.1.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

18/12 2014

v2.2.0

2.2.0.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

17/12 2014

v2.1.1

2.1.1.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

21/11 2014

v2.1

2.1.0.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

20/11 2014

2.0

2.0.0.0 https://github.com/oscarotero/Gettext

PHP gettext mananger

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

translation js i18n gettext mo po

22/10 2014

v1.1.5

1.1.5.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

21/10 2014

v1.1.4

1.1.4.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

15/10 2014

v1.1.3

1.1.3.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

14/08 2014

v1.1.2

1.1.2.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

12/08 2014

v1.1.1

1.1.1.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

24/07 2014

v1.1.0

1.1.0.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

20/02 2014

v1.0.3

1.0.3.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

26/12 2013

v1.0.2

1.0.2.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

23/12 2013

v1.0.1

1.0.1.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext

01/12 2013

v1.0

1.0.0.0 https://github.com/oscarotero/Gettext

PHP - JS gettext conversor

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

translation js i18n gettext