# How is translation of labels achieved in Forms?

**URL:** <https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016>\
**Category:** Components & MVC\
**Tags:** laminas-form, laminas-mvc, laminas-mvc-i18n, laminas-i18n\
**Created:** [August 22, 2022, 10:38am UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016 "2022-08-22T10:38:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjivsharmalv](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sanjivsharmalv/32/1164_2.png) [@sanjivsharmalv](https://discourse.laminas.dev/u/sanjivsharmalv)\
**Post date:** [August 22, 2022, 10:38am UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016/1 "2022-08-22T10:38:11Z")

</div>

While creating Form in Laminas I want to know translate the Form Labels. One way is I pass the translator to the form, is there a better way of translation and how does it get the language of the currently logged in user in Form?Also which view helper is used for translation?

---

<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:** [August 22, 2022, 1:01pm UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016/2 "2022-08-22T13:01:19Z")

</div>

> [@sanjivsharmalv](#):
>
> One way is I pass the translator to the form, is there a better way of translation…

I assume that you are using a laminas-mvc based application.

1. Install `laminas-mvc-i18n`:

> **[laminas-mvc-i18n - Laminas Docs](https://docs.laminas.dev/laminas-mvc-i18n/)**
>
> Integration between laminas-mvc and laminas-i18n

1. Add translation files and configuration for the translator to the application configuration. Example:

> **[Factory - laminas-i18n - Laminas Docs](https://docs.laminas.dev/laminas-i18n/translator/factory/#setting-translation-files)**
>
> Provide translations for your application, and filter and validate internationalized values.

1. Set the locale.

Done!

> [@sanjivsharmalv](#):
>
> …how does it get the language of the currently logged in user in Form?

First: The forms are irrelevant, the translation is done outside the forms, at the view layer.

There are several ways to set and get the locale. Therefore, it depends on what you use in your application. Do you use laminas-authentication or something else?

> [@sanjivsharmalv](#):
>
> Also which view helper is used for translation?

The form view helpers extends [the abstract view helper of laminas-i18n](https://docs.laminas.dev/laminas-i18n/view-helpers/introduction/#abstract-translator-helper), which uses the translator of your application (injected via the view helper plugin manager of laminas-view).

---

<div class="post-metadata">

**Author:** ![sanjivsharmalv](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sanjivsharmalv/32/1164_2.png) [@sanjivsharmalv](https://discourse.laminas.dev/u/sanjivsharmalv)\
**Post date:** [August 22, 2022, 2:46pm UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016/3 "2022-08-22T14:46:41Z")

</div>

I understand the fields are still added in the form which has also the untranslated labels; the only thing is these labels are translated later in view? Also please discuss what are the benefits in following this new approach. Thanks!

---

<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:** [August 22, 2022, 3:36pm UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016/4 "2022-08-22T15:36:16Z")

</div>

> [@sanjivsharmalv](#):
>
> I understand the fields are still added in the form which has also the untranslated labels; the only thing is these labels are translated later in view?

Correct, labels are added as untranslated strings and the form view helpers use the translator to translated them.

> [@sanjivsharmalv](#):
>
> Also please discuss what are the benefits in following this new approach.

This is not a _new approach_, this is the normal way. It is not necessary to pass around a translator instance. If you use the MVC integration of laminas-i18n then laminas-view and laminas-form will use it and therefore you do not have to do more. The components interact here and therefore this approach is sufficient.

---

<div class="post-metadata">

**Author:** ![sanjivsharmalv](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sanjivsharmalv/32/1164_2.png) [@sanjivsharmalv](https://discourse.laminas.dev/u/sanjivsharmalv)\
**Post date:** [August 22, 2022, 8:28pm UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016/5 "2022-08-22T20:28:59Z")

</div>

How do you attach validators to the form elements ?

---

<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:** [August 22, 2022, 8:42pm UTC](https://discourse.laminas.dev/t/how-is-translation-of-labels-achieved-in-forms/3016/6 "2022-08-22T20:42:14Z")

</div>

New topic, please create a new post. (This helps other users to find the different topics more easily.)  
Thanks in advance! 👍🏼
