Skip to content

Customization

BoBkiNN_ edited this page Feb 17, 2026 · 2 revisions

This page describes how to customize IndigoI18n behaviour.

🧱 Extending

You can extend different parts of system by subclassing and overriding methods
or adding new converters, renderers etc.

Adding argument converter

When you obtain an instance of TemplateFormatter you can use method putConverter to attach
your implementation of ArgumentConverter for some class. Example:

// obtain template formatter 
TemplateFormatter<String> f = ...
// argument converter that returns MyObject value wrapped in square brackets
ArgumentConverter<MyObject, String> converter = (ctx, obj, format) -> "["+obj.value+"]";
// use this converter for MyObject class
f.putConverter(MyObject.class, converter);

Adding renderer type

// obtain IndigoI18n instance
var i = new IndigoI18n();
// create render type or use existing
var type = new RenderType<>(String.class);
// add StringRenderer keyed with this type
i.addRenderer(type, StringRenderer::new);
// use this renderer using render type
i.parse(type, null, "en", "test", List.of());

Example simple implementation is
StringRenderer.
To create your own Renderer you need to subclass
Renderer and implement abstract
methods. Type variable O is output type of you renderer, for example String or your custom text.

🧰 Custom I18n

You can customize high-level api by adding renderers and mixins to IndigoI18n subclass. Usual steps are:

  1. Subclass IndigoI18n or its subclass:

    public class MyI18n extends IndigoI18n {
    }
  2. Add and configure renderers:
    Override addDefaultRenderers method to add your renderers

    @Override
    protected void addDefaultRenderers() {
        // add string renderer with key RenderType.STRING
        addRenderer(RenderType.STRING, StringRenderer::new);
    }
  3. Add mixins:
    Mixins are high-level interfaces with all default methods.
    They provide useful shortcuts for different render types and other common-used utilities.
    You can use them or create your own, for example they can be used to:

    • retrieve language from thread local or system environment
    • accept text viewer and extract language from it
    • alter created context with own data
    public class MyI18n extends IndigoI18n implements StringI18nMixin

    The StringI18nMixin adds new parse(...) methods that uses RenderType.STRING.
    Note that if mixin uses render type which is not registered, runtime errors like Unknown renderer for output String will occur later.

  4. Setup:
    Before using your instance, you must call setup() method
    to add renderers. Also, you probably want to load some sources.

    var i18n = new MyI18n();
    // invoke setup to register renderers
    i18n.setup();
    // obtain source
    TranslationSource source = ...
    // load source
    i18n.load(source);
    // use high-level methods provided by mixin
    i18n.parse("en", "key", "arg1");
  5. (Optional) Static shortcut:
    You can generate static shortcut using @GenStaticDefault annotation and annotation processor.

    // specify generated class name and static method to use
    @GenStaticDefault(name = "I18n", creator = "create")
    public class MyI18n extends IndigoI18n implements StringI18nMixin {
    
        // static method that is called by generated class to obtain instance.
        // must return instance of this class and take no args
        public static MyI18n create() {
            var r = new MyI18n();
            r.setup(); // do not forget to setup newly created instance
            return r;
        }
    }

    Now you can use: I18n.parse("en", "key", "arg1").
    Generated class is located in same package and have public static final INSTANCE
    field with type of your class.

Clone this wiki locally