{"id":16728895,"url":"https://github.com/saeven/zf3-twig-trans","last_synced_at":"2025-04-10T11:07:29.296Z","repository":{"id":27301134,"uuid":"30775092","full_name":"Saeven/zf3-twig-trans","owner":"Saeven","description":"ZF3 module that glues Twig's {% trans %} and the MvcTranslator together","archived":false,"fork":false,"pushed_at":"2024-03-20T13:41:08.000Z","size":75,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-04T07:37:30.316Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"aio-libs/aiohttp","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Saeven.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-02-13T20:41:13.000Z","updated_at":"2023-01-14T21:13:38.000Z","dependencies_parsed_at":"2025-02-17T02:44:01.727Z","dependency_job_id":null,"html_url":"https://github.com/Saeven/zf3-twig-trans","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saeven%2Fzf3-twig-trans","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saeven%2Fzf3-twig-trans/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saeven%2Fzf3-twig-trans/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saeven%2Fzf3-twig-trans/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Saeven","download_url":"https://codeload.github.com/Saeven/zf3-twig-trans/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248208541,"owners_count":21065202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T23:12:18.852Z","updated_at":"2025-04-10T11:07:29.264Z","avatar_url":"https://github.com/Saeven.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zf3-twig-trans\n\n[![Build Status](https://travis-ci.org/Saeven/zf3-twig-trans.svg?branch=master)](https://travis-ci.org/Saeven/zf3-circlical-user)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/e9b80ae1c4c94159abe7bcb49b851cac)](https://www.codacy.com/app/saeven/zf3-twig-trans?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=Saeven/zf3-twig-trans\u0026amp;utm_campaign=Badge_Grade)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/e9b80ae1c4c94159abe7bcb49b851cac)](https://www.codacy.com/app/saeven/zf3-twig-trans?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=Saeven/zf3-twig-trans\u0026utm_campaign=Badge_Coverage)\n[![Total Downloads](https://poser.pugx.org/saeven/zf2-circlical-trans/downloads)](https://packagist.org/packages/saeven/zf2-circlical-trans)\n\nMagic glue to create the expected experience when developing with ZfcTwig, the MvcTranslator (e.g., from within your controllers), and your twig templates using a custom {% trans %} that this module provides.  Usage is very simple! With this package included, \"trans\" becomes available in your templates.\n\n\u003e New! This module supports text domains!\n\nInspired by ZfcTwig and its extensions project at https://github.com/twigphp/Twig-extensions\n\n\n## Requirements\n\n\n|Item               |  Version     |\n|-------------------|--------------|\n|PHP                | 7+           |\n|Zend Framework     | 3.*          |\n|Gettext PHP Module | *            | \n\n\n## Installation\n\n```js\ncomposer require \"saeven/zf3-twig-trans\"\n```\n\n## Configuration\n\n\n#### Loading the module: application.config.php\n\nIn your module's application.config.php, make sure you've got these modules loaded:\n\n    'ZfcTwig',\n    'CirclicalTwigTrans'\n\nBy loading CirclicalTwigTrans, you will be setting an alias from 'translator' to 'MvcTranslator'.  If you have an existing translator alias in your system, please remove it.\n\n#### Managing Locale: Your Application's Module.php\n\nIt's assumed that you are managing locale in your app's bootstrap.  For example, in your Application module's onBootstrap:\n\n```php\npublic function onBootstrap(MvcEvent $e)\n{\n\n    $translator = $e-\u003egetApplication()-\u003egetServiceManager()-\u003eget('translator');\n    $translator\n        -\u003esetLocale( 'fr_CA' )\n        -\u003esetFallbackLocale( 'en_US' );\n}\n```\n\n#### Proper Language File Setup\n\ngettext imposes a certain file structure; language folders for a module would look like so:\n\n```\nmodule/\n    Application/\n        language/\n            en_US/\n                LC_MESSAGES/\n                    default.mo\n                    default.po\n                    errors.mo\n                    errors.po\n            fr_CA/\n                LC_MESSAGES/\n                    default.mo\n                    default.po\n                    errors.mo\n                    errors.po\n```\n\nThe .mo files are truly the ones that matter.  The .po files, are the source files that are used to compile the .mo files with msgfmt.\n\nThe nomenclature, default.mo, indicates that that file contains text strings for the 'default' text domain.  In other words, the name of the files is vital to proper functionality.  \n\nYou need to tweak your translator configuration to support this file structure, it's very simple.  Per module:\n\n```php\n'translator' =\u003e [\n    \n    'translation_file_patterns' =\u003e [\n        [\n            'locale'        =\u003e 'en_US',\n            'type'          =\u003e 'gettext',\n            'base_dir'      =\u003e __DIR__ . '/../language',\n            'pattern'       =\u003e '%s/LC_MESSAGES/default.mo',\n            'text_domain'   =\u003e 'default',\n        ],\n        [\n            'locale'        =\u003e 'en_US',\n            'type'          =\u003e 'gettext',\n            'base_dir'      =\u003e __DIR__ . '/../language',\n            'pattern'       =\u003e '%s/LC_MESSAGES/errors.mo',\n            'text_domain'   =\u003e 'errors',\n        ],\n    ],\n],\n```\n\nVery important: there's a critical difference between Zend's translator implementations, and gettext's implementation.  The Zend translator\nwill allow you to use multiple .mo files for a same domain, but gettext does not support this behavior.  To ensure that both the Twig translations, and\nyour in-app translations (e.g., `$translator-\u003etranslate('foo')`) work properly, your domain names must be unique.  A good practice is to name your domain, \nafter your module.\n\n## Usage\n\nIncluded tests support all flavors of trans, adding direct support for domain overrides from within the template.  These syntax structures all work:\n\n\n#### Translate 'Sorry' from text-domain 'errors'\n```twig\n{% trans from \"errors\" %}Sorry{% endtrans %}\n```\n\n#### Translate 'Home' from the 'default' domain\n```twig\n{% trans %}Home{% endtrans %}\n```\n\n\n#### Translate \"A 404 error occurred\" from the 'default' domain\n```twig\n{% trans \"A 404 error occurred\" %}\n```\n\n#### Translate with pluralization from the 'default' domain\n```twig\n{% set birds = 422 %}\n{% trans %}\n    There is one bird\n{% plural birds %}\n    There are {{ birds }} birds\n{% endtrans %}\n```\n\n\n#### Translate with pluralization from the 'errors' domain\n```twig\n{% set birds = 422 %}\n{% trans from \"errors\" %}\n    There is one bird\n{% plural birds %}\n    There are {{ birds }} birds\n{% endtrans %}\n```\n\n#### Translate with notes\n```twig\n{% trans %}\n    Hello User!\n{% notes %}\n    This is used in the greeting area.\n{% endtrans %}\n```\n\n#### Controllers\nUsage in controllers doesn't change.\n\n```php\n/** @var Laminas\\I18n\\Translator\\Translator $tr */\n$tr = $sm-\u003eget('translator');\n\n$tr-\u003etranslate( 'Home' );\n$tr-\u003etranslate( 'Sorry', 'errors' );\n\n\n$num = 422;\nsprintf( $tr-\u003etranslatePlural( \"There is one bird\", \"There are %d birds\", 422 ), $num );\nsprintf( $tr-\u003etranslatePlural( \"There is one bird\", \"There are %d birds\", 422, 'errors' ), $num );\n```\n\n\nYou can test it with the ZF3 Skeleton, by translating \"Home\" to \"fr_CA\" which becomes \"Acceuil\" (good test).\n\n## Enjoy!\n\nLet me know if you find any issues.  I use this module as well in production, so definitely want to hunt bugs down!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaeven%2Fzf3-twig-trans","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaeven%2Fzf3-twig-trans","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaeven%2Fzf3-twig-trans/lists"}