Repository navigation
Customization
This page describes how to customize IndigoI18n behaviour.
You can extend different parts of system by subclassing and overriding methods
or adding new converters, renderers etc.
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);// 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.
You can customize high-level api by adding renderers and mixins to IndigoI18n subclass. Usual steps are:
-
Subclass IndigoI18n or its subclass:
public class MyI18n extends IndigoI18n { }
-
Add and configure renderers:
OverrideaddDefaultRenderersmethod to add your renderers@Override protected void addDefaultRenderers() { // add string renderer with key RenderType.STRING addRenderer(RenderType.STRING, StringRenderer::new); }
-
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 usesRenderType.STRING.
Note that if mixin uses render type which is not registered, runtime errors likeUnknown renderer for output Stringwill occur later. -
Setup:
Before using your instance, you must callsetup()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");
-
(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 havepublic static final INSTANCE
field with type of your class.