rodionmoiseev / c10n

A Java library, focused on making internationalisation more modular, easier to evolve and maintain, robust-to-change and IDE-friendly without excess of external tools.
Apache License 2.0
67 stars 10 forks source link

Simplify Enum support (beta idea) #47

Open rodionmoiseev opened 4 years ago

rodionmoiseev commented 4 years ago

Inspired by @lelmarir Automated Enum binding with Guice commit.

Motivation

Currently providing locale sensitive translations for Enums is relatively complex. In overview it requires 2 steps:

  1. Create a separate c10n-interface with translations
  2. Install an "enumMapping" filter for the Enum in question

The biggest problems with this approach are:

A Better Approach

Being able to specify translations directly on the values of the Enum seems to do the job better:

enum Pet {
    @En("Cat")
    @Ru("Кот")
    CAT,

    @En("Dog")
    @Ru("Собака")
    DOG
}

Enum-type arguments can be retrieved and cached at c10n initialization time, in the same way other standard translations are fetched, instead of c10n interface method invocation time.

This approach would make Enum support more transparent, with no extra configuration, and with less overhead.

It would also work transparently with other c10n extensions, such as external/internal resource loading, Guice support and Logging tool.

lelmarir commented 4 years ago

Being able to specify translations directly on the values of the Enum

don't really like this, it is not always possible to directly modify the enum to add locale annotations, and beside a separate PetMessages seems a more clean approach