Download the PHP package cooperaj/better-twig-i18n without Composer
On this page you can find all versions of the php package cooperaj/better-twig-i18n. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cooperaj/better-twig-i18n
More information about cooperaj/better-twig-i18n
Files in cooperaj/better-twig-i18n
Package better-twig-i18n
Short Description Provides i18n functionality based on gettext language files.
License mit
Informations about the package better-twig-i18n
Better Twig Gettext i18n
Allows the use of a POT/PO/MO (gettext) set of language definitions with Twig, using an identical syntax to the current official Twig translation extension.
It supports full extraction of Twig templates into language catalogues from the gettext/gettext library, which you can use to write out POT files if desired.
Additionally support for extraction of text values from PHP is possible when directly using the translate
function
from\Acpr\I18n\Translator
in your code. For instance, you may be creating translated flash messages and storing them
in your session to be used by subsequent twig templates.
Supports
Twig
- Translation tags (
{% trans %} ... {% endtrans %}
) and filters (| trans
) - Variable interpolation
{% trans with { '%var%': var } %}%var%{% endtrans %}
- Pluralisation
{% trans count 3 %}%count% item|%count% items{% endtrans %}
- Message specific domains
{% trans from 'errors' %} ... {% endtrans %}
- Message contexts
{% trans %} ... {% context %}Some context{% endtrans %}
-
Notes/comments for the translation
{% trans %} ... {% notes %}A translation note{% endtrans %}
-
or some horrid combination of all of them
The extraction of which would result in a
errors.pot
file that contains:And the (default, i.e. no language supplied) output of which would look like
PHP
The PHP extraction works by parsing the text of your PHP files through nikic/php-parser . This requires your PHP to be valid in order to work.
You could have a PHP file
The extraction of which would result in a errors.pot
file that contains:
Limitations
- The extraction specifically looks for usages of a
translate
function with the correct signature. This may result in false positives dependent on your code base. - It is not currently possible to add notes/comments to a translation entry.
- For the correct values to be parsed, the string arguments to the
translate
function must be inlined strings (quoted or heredoc). It is not possible to use variables.
Usage
See index.php for example usage.
Upgrading
Upgrading to v3.0.0 from prior versions
v3 introduces a new method of extracting keys that removes whitespace of more than 2 characters from the primary language keys (see the release notes for more information)
There is a scripts folder that will help to migrate any existing PO files you have to the newer keys and minimise the pain of poedit deciding everything is new rather than different. It will likely need editing to your requirements but is usage is simple.
All versions of better-twig-i18n with dependencies
ext-gettext Version *
twig/twig Version ^v3.0
symfony/finder Version ^4.0 || ^5.0
gettext/gettext Version ^5.5
gettext/translator Version ^1.0
nikic/php-parser Version ^4.9