# Gettext translate with grammatical case

**URL:** <https://discourse.laminas.dev/t/gettext-translate-with-grammatical-case/938>\
**Category:** Components & MVC\
**Tags:** zend-i18n\
**Created:** [February 10, 2019, 8:44pm UTC](https://discourse.laminas.dev/t/gettext-translate-with-grammatical-case/938 "2019-02-10T20:44:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alaksander\_A](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/alaksander_a/32/498_2.png) [@Alaksander\_A](https://discourse.laminas.dev/u/Alaksander_A)\
**Post date:** [February 10, 2019, 8:44pm UTC](https://discourse.laminas.dev/t/gettext-translate-with-grammatical-case/938/1 "2019-02-10T20:44:54Z")

</div>

Zend 3. I’m using gettext translator with some locales. By defaut I use en locale without specific .po file. How I can translate with grammatical case, I try to use: `$this->translate("genitive" . "translate_text")` and this works with languages, that have .po file, but for english locale (without .po file) I have `"genitivetranslate_text"` in result. How should I to use translate method with grammatical case to get the correct result?  
For example, in wordpress it looks like this: `_x("January", "genitive")` (grammatical case by second argument)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [February 11, 2019, 7:12am UTC](https://discourse.laminas.dev/t/gettext-translate-with-grammatical-case/938/2 "2019-02-11T07:12:41Z")

</div>

> [@Alaksander\_A](#):
>
> …grammatical case…

It can be used for much more than a “grammatical case”. Therefore it is called “context”.

See:

- [XLIFF 1.2 Specification](http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#context)
- [Contexts (GNU gettext utilities)](https://www.gnu.org/software/gettext/manual/html_node/Contexts.html)

> [@Alaksander\_A](#):
>
> I try to use: `$this->translate("genitive" . "translate_text")` and this works with languages that have .po file…

It works, because the entire string is used as “message identifier” and if a translation file (`.po` file) contains this message, then you will get the translated text.

> [@Alaksander\_A](#):
>
> …but for english locale (without .po file) I have `genitivetranslate_text` in result.

This also correct, because:

> When there is no translation for a specific message identifier in a locale, the message identifier itself will be returned by default.

> **[Translation - zend-i18n - Zend Framework Docs](https://docs.zendframework.com/zend-i18n/translation/#setting-a-locale)**
>
> Provide translations for your application, and filter and validate internationalized values.

> [@Alaksander\_A](#):
>
> How should I to use translate method with grammatical case to get the correct result?

The translator of zend-i18n currently ships with support of 3 translation formats (PHP arrays [file and memory], Gettext, INI) and provides unified access for these formats. But only Gettext supports the context feature and so you will not find an option for that in the unified access and the related helpers.

To solve your problem you can:

- add another `.po` file for english
- create your own view helper and add the support for a context (compare with [comment on php.net](http://php.net/manual/en/book.gettext.php#89975) and the [`Translation_Entry ` class](https://core.trac.wordpress.org/browser/tags/5.0.3/src/wp-includes/pomo/entry.php#L74) of Wordpress)

* * *

Btw. PHP itself does not support the context feature of Gettext.

Search for “pgettext”:

- [Contexts (GNU gettext utilities)](https://www.gnu.org/software/gettext/manual/html_node/Contexts.html)
- [PHP: Gettext - Manual](http://php.net/manual/en/book.gettext.php)

---

<div class="post-metadata">

**Author:** ![Alaksander\_A](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/alaksander_a/32/498_2.png) [@Alaksander\_A](https://discourse.laminas.dev/u/Alaksander_A)\
**Post date:** [February 11, 2019, 4:12pm UTC](https://discourse.laminas.dev/t/gettext-translate-with-grammatical-case/938/3 "2019-02-11T16:12:26Z")

</div>

Thanks for your the answer. I also thought about the English .po file, but this is a duplicate of the common translation list. Probably I’ll try to expand the base class of the translator.
