Need help translating forms [standalone]

bernhard
Member | 40
+
+1
-

Hi everybody,

I read the docs about translating forms but I don't understand them. https://doc.nette.org/…on/templates#… seems to be easy enough, but how do I get that $translator variable?? And how do I set german translations? Are there translation files for the framework already or do I need to translate all validator messages on my own?

Could anybody please help me what I'd have to do if I just wanted to translate the default “Please enter a valid email address.” validation message to “Bitte geben Sie eine gültige Mailadresse ein.” ?

Thanks in advance!

Pavel Kravčík
Member | 1182
+
0
-

Simple solution (insert in config.neon). Via https://doc.nette.org/en/configuring#…

forms:
	messages:
		EMAIL: Bitte geben Sie eine gültige Mailadresse ein.

You need clear cache after that.

bernhard
Member | 40
+
0
-

Thx Pavel,
yeah I also read about that, but where do I put this file?? Often when reading the docs in nette I feel really dump. It seems that many things should be obvious but for me they are not :(

Pavel Kravčík
Member | 1182
+
0
-

If you using standard webproject

Or you can add endless configs in Bootstrap, like here on L27

Last edited by Pavel Kravčík (2021-06-25 14:30)

Felix
Nette Core | 1189
+
-1
-

There are also project skeletons in Contributte organization (https://github.com/contributte?…).

jiri.pudil
Nette Blogger | 1028
+
+4
-

Hi, you can always provide an error message as part of the addRule() method call. If you don't, Nette falls back to default messages from Nette\Forms\Validators::$messages – the property is public, so you can change the messages to your liking. Neon configuration is a more idiomatic way to change them if you're using the whole Nette Framework, but since you're only using stand-alone nette/forms, feel free to change Nette\Forms\Validators::$messages directly in code.

The translator is useful if you need to support multiple languages dynamically. In that case it's recommended to use some sort of a translation key in place of the error message, and then provide an implementation of the Translator interface. You can write one from scratch, or use contributte/translation which is an adapter of the Symfony Translation component.

bernhard
Member | 40
+
+3
-

Thx Jiri, that got me on the right track and I got it working :)

The $messages array is in Nette\Forms\Validator (without s) though. But I got that sorted. As I did not find any german translations for nette forms I've created a method ->setLang(“de”) that uses these translations:

[
  CsrfProtection::PROTECTION => 'Ihre Sitzung ist abgelaufen. Bitte kehren Sie zur Startseite zurück und versuchen Sie es erneut.',
  Form::EQUAL => 'Bitte geben Sie %s ein.',
  Form::NOT_EQUAL => 'Dieser Wert sollte nicht %s sein.',
  Form::FILLED => 'Dieses Feld ist erforderlich.',
  Form::BLANK => 'Dieses Feld sollte leer sein.',
  Form::MIN_LENGTH => 'Bitte geben Sie mindestens %d Zeichen ein.',
  Form::MAX_LENGTH => 'Bitte geben Sie nicht mehr als %d Zeichen ein.',
  Form::LENGTH => 'Bitte geben Sie einen Wert zwischen %d und %d Zeichen Länge ein.',
  Form::EMAIL => 'Bitte geben Sie eine gültige Mailadresse ein.',
  Form::URL => 'Bitte geben Sie eine gültige URL ein.',
  Form::INTEGER => 'Bitte geben Sie eine gültige Ganzzahl ein.',
  Form::FLOAT => 'Bitte geben Sie eine gültige Zahl ein.',
  Form::MIN => 'Bitte geben Sie einen Wert ein, der größer oder gleich %d ist.',
  Form::MAX => 'Bitte geben Sie einen Wert kleiner oder gleich %d ein.',
  Form::RANGE => 'Bitte geben Sie einen Wert zwischen %d und %d ein.',
  Form::MAX_FILE_SIZE => 'Die Größe der hochgeladenen Datei kann bis zu %d Bytes betragen.',
  Form::MAX_POST_SIZE => 'Die hochgeladenen Daten überschreiten die Grenze von %d Bytes.',
  Form::MIME_TYPE => 'Die hochgeladene Datei hat nicht das erwartete Format.',
  Form::IMAGE => 'Die hochgeladene Datei muss ein Bild im Format JPEG, GIF, PNG oder WebP sein.',
  SelectBox::VALID => 'Bitte wählen Sie eine gültige Option aus.',
  UploadControl::VALID => 'Beim Hochladen der Datei ist ein Fehler aufgetreten.',
]