Download the PHP package omega-mvc/gettext without Composer

On this page you can find all versions of the php package omega-mvc/gettext. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package gettext

Omega Logo

Documentation | Changelog | Contributing | Code of Conduct | License

PHPUnit PHPCS PHPStan Packagist Version SemVer 1.0.0

Gettext

Gettext is a PHP 8.4+ library for working with internationalization (i18n) and localization (l10n).
It provides tools to import, export, edit, and merge translations across multiple formats, such as PO, MO, PHP arrays, JSON, and JavaScript files.

The library is designed to be flexible: you can scan source code to extract translatable strings, load existing translations from files, manipulate them in PHP, and finally generate the compiled output in the format required by your application or framework.

Whether you are building a multilingual website, a PHP application, or a project that integrates templates and scripts, this package aims to provide a consistent API for all gettext-related operations.

Table of Contents

Installation

Running Test

Known issues

When the test suite is run, the summary reports:

These 2 warnings are intentional: two tests (Scanner\CodeScannerTest::testScanFileThrowsWhenFileIsUnreadable and Loader\LoaderEdgesTest::testUnreadableFilesThrow) deliberately create a file with no read permissions to verify that the error is handled correctly (with the expected exception). The warning is therefore a test tool, not a real problem in the code.

Why is the detail suppressed? Not to hide anything, but to avoid bothering users. The test still passes (the exception is thrown as expected) and the warning message adds no useful information to whoever runs the suite.

How to re-enable the detail display. If you want to see the full report of the two warnings (file paths and stack traces), open phpunit.xml.dist and:

  1. Set displayDetailsOnTestsThatTriggerWarnings to true.
  2. (Recommended) set failOnWarning back to true to make the suite fail in the presence of warnings.

Classes and functions

This package contains the following classes:

Quick Start

Translation

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

Translations

The Omega\Gettext\Translations class stores a collection of translations:

Translator

GettextTranslator

The class Omega\Gettext\GettextTranslator uses the gettext extension. It's useful because combines the performance of using real gettext functions but with the same API as Translator class, so you can switch to one or other translator without change code of your app.

TranslatorFunctions

The TranslatorFunctions class is a static registry that binds a translator and a formatter instance, so they can be reached from anywhere in the application:

Applications willing to expose global helper functions (like __()) can build them on top of this registry. The PhpScanner recognizes the standard gettext function names (__, n__, p__, d__...) when extracting translatable strings.

Loaders

This package includes the following loaders:

ArrayLoader

JsonLoader

MoLoader

PoLoader

StrictPoLoader

StrictPoLoader is a parser more aligned to the GNU gettext tooling with the same expectations and failures (see the tests for more details).

The usage is basically the same as the PoLoader:

Generators

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

This package includes the following generators:

ArrayGenerator

ArrayGenerator generates PHP array files from gettext translations with optional pretty-printing and strict types.

JsonGenerator

JsonGenerator generates JSON files from gettext translations with configurable JSON options.

MoGenerator

MoGenerator generates binary .mo files from gettext translations, optionally including headers

PoGenerator

PoGenerator generates human-readable .po files from gettext translations.

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 includes the following scanners:

JsScanner

PhpScanner

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 Omega\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::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:

There are some typical scenarios, one of the most common:

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

gettext language list automatically generated from CLDR data

Static usage

To use the languages data generated from this tool you can use the bin/export-plural-rules command.

Export command line options

export-plural-rules supports the following options:

Export formats

export-plural-rules can generate data in the following formats:

Dynamic usage

With Composer

You can use Composer to include this tool in your project. Simply launch composer require omega-mvc/gettext or add "omega-mvc/gettext": "*" to the "require" section of your composer.json file.

Main methods

The most useful functions of these tools are the following

getAll returns a list of Omega\Gettext\Languages\Language instances, getById returns a single Omega\Gettext\Languages\Language instance (or null if the specified language identifier is not valid).

The main properties of the Omega\Gettext\Languages\Language instances are:

Is this data correct?

Yes - as far as you trust the Unicode CLDR project.

The conversion from CLDR to gettext includes also a lot of tests to check the results. And all passes :wink:.

Reference

CLDR

The CLDR specifications define the following variables to be used in the CLDR plural formulas:

gettext

The gettext specifications define the following variables to be used in the gettext plural formulas:

Conversion CLDR > gettext

CLDR variable gettext equivalent
n n
i n
v 0
w 0
f empty
t empty
c empty
e empty

Parenthesis in ternary operators

The generated gettext formulas contain some extra parenthesis, in order to avoid problems in some programming language. For instance, let's assume we have this formula: (0 == 0) ? 0 : (0 == 1) ? 1 : 2

So, in order to avoid problems, instead of a simple a ? 0 : b ? 1 : 2 the resulting formulas will be in this format: a ? 0 : (b ? 1 : 2)

Official Documentation

The official documentation for Omega is available here

Contributing

If you'd like to contribute to the Omega example application package, please follow our contribution guidelines.

License

This project is open-source software licensed under the GNU General Public License v3.0.


All versions of gettext with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
nikic/php-parser Version ^v5.6.1
mck89/peast Version ^1.16.3-dev
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package omega-mvc/gettext contains the following files

Loading the files please wait ...