formio icon indicating copy to clipboard operation
formio copied to clipboard

Is it possible to get "better" labels by default without using .properties files?

Open skinkie opened this issue 5 years ago • 3 comments

I have set up a properties file to map between:

quantity-unitCode=Unit Code

Is there any way that I could do for example programmatically instruct to render this in a better way than:

???quantity-unitCode???:

skinkie avatar Jul 28 '20 02:07 skinkie

Hi skinkie,

what is your solution/template engine to generate markup of your forms (or are you using FormRenderer class)?

In provided demo, you can see translation of form field names within: https://github.com/beranradek/formio-demo/blob/master/src/main/resources/webapp/WEB-INF/jsp/simple.jsp#L5 https://github.com/beranradek/formio-demo/blob/master/src/main/resources/webapp/WEB-INF/tags/forms/input.tag#L74 https://github.com/beranradek/formio-demo/blob/master/src/main/resources/net/formio/demo/domain/Person.properties

But generally, you can make your custom solution for translations based on labelKey attribute of form fields.

As stated in http://www.formio.net/documentation/configuration/: Default message bundle name (also used by FormRenderer) is derived from the class of edited object. If com.example.Registration class is edited, com/example/Registration bundle (Registration.properties in package com.example) is searched. If translation in message bundle is not found, ??? is written to output. When using FormRenderer for automatic form rendering, FormRenderer.getMessageTranslator can be overriden to provide custom message translator that does not use properties files.

beranradek avatar Aug 29 '20 14:08 beranradek

what is your solution/template engine to generate markup of your forms (or are you using FormRenderer class)?

I am using the FormRenderer class.

But generally, you can make your custom solution for translations based on labelKey attribute of form fields.

Maybe I don't understand something. What I would expect is that the attribute names would be extracted from the classes, at least to give a sane default. Maybe camelCases rewriten to "Camel Cases" label. As developer I don't want to do this manually for an XSD schema.

skinkie avatar Aug 29 '20 15:08 skinkie

You can see example of FormRenderer usage in https://github.com/beranradek/formio-demo/blob/master/src/main/java/net/formio/demo/forms/CarForm.java. Translation properties associated with Car object are: https://github.com/beranradek/formio-demo/blob/master/src/main/resources/net/formio/demo/domain/car/Car.properties (file corresponding to class name is taken by default, letter case is important, I assume).

As I stated above, method FormRenderer.getMessageTranslator (or probably FormRenderer.renderMarkupFieldLabel) can be overriden (see the implementation of renderMarkupFieldLabel) if default usage of .properties file does not suit for you.

beranradek avatar Aug 29 '20 16:08 beranradek