Download the PHP package epiconcept/gettext without Composer
On this page you can find all versions of the php package epiconcept/gettext. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package gettext
Gettext
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
Created by Oscar Otero http://oscarotero.com [email protected] (MIT License)
Gettext is a PHP (^7.2) library to import/export/edit gettext from PO, MO, PHP, JS files, etc.
Installation
Classes and functions
This package contains the following classes:
Gettext\Translation
- A translation definitionGettext\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
Translation
The Gettext\Translation
class stores all information about a translation: the original text, the translated text, source references, comments, etc.
Translations
The Gettext\Translations
class stores a collection of translations:
Loaders
The loaders allows to get gettext values from any format. For example, to load a .po file:
This package includes the following loaders:
MoLoader
PoLoader
And you can install other formats with loaders and generators:
Generators
The generators export a Gettext\Translations
instance to any format (po, mo, etc).
This package includes the following generators:
MoGenerator
PoGenerator
And you can install other formats with loaders and generators:
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:
This package does not include any scanner by default. But there are some that you can install:
Merging translations
You will want to update or merge translations. The function mergeWith
create a new Translations
instance with other translations merged:
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:
Constant | Description |
---|---|
Merge::TRANSLATIONS_OURS |
Use only the translations present in $translations1 |
Merge::TRANSLATIONS_THEIRS |
Use only the translations present in $translations2 |
Merge::TRANSLATION_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:
There are some typical scenarios, one of the most common:
- 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).
More common scenarios may be added in a future.
Contributors
Thanks to all contributors specially to @mlocati.
Please see CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.