Module uk.autores

Class GenerateMessagesFromProperties

java.lang.Object
uk.autores.handling.GenerateMessagesFromProperties
All Implemented Interfaces:
Handler

public final class GenerateMessagesFromProperties extends Object implements Handler

Handler that generates message classes from Properties files using Locales to match localized strings and MessageFormat to create typed method signatures.

The properties file name is used to name the class. The keys are used to name the methods.

Locale.getAvailableLocales() and ResourceBundle.Control.toBundleName(String, Locale) are used to discover localized properties.

Generated method signatures will vary based on need.

  • A method to return the raw value is always generated.
    • If "localize" is true and localized files have been detected the method requires a Locale argument.
  • q A second method is generated if "format" is true and format expressions have been detected in the value.
    • Locale is the first argument if "localize" is true and localized files have been detected and number/choice/date us used.
    • Format expressions form the remaining arguments in index order. MessageFormat expressions are mapped as follows.

Example

Example file Cosmic.properties:

planet-event=At {1,time} on {2,date}, there was {3} on planet {0,number,integer}.

This will generate a class Cosmic with the method signature:

static String planet_event(Locale l, Number v0, ZonedDateTime v1, ZonedDateTime v2, String v3)

Usage:

     var now = ZonedDateTime.now();
     Cosmic.planet_event(Locale.US, 4, now, "an attack")
 

Generally variables can be reused in MessageFormat patterns but this is unsupported when dates are present due to implementation limitations in time zone handling.

  • Constructor Details

    • GenerateMessagesFromProperties

      public GenerateMessagesFromProperties()
      Ctor
  • Method Details