Loading…

Kryptel/Java

Library Localization

Translating to Other Languages

Although library components do not interact with the user directly, the library includes a number of informational and error messages in Message class. You can easily localize the library by adding a custom localization class.

First Step: Creating Localization Class

The library obtains localized string via Message.ILocalizedMessage interface. It contains a single method Get, which accepts a library message code and returns the corresponding translated message – or null if there is no translated message for that code. Note that translation may be partial; in addition, the localization class will work even if a future library version adds new messages. Those new messages will simply remain untranslated.

class GermanLocalization implements Message.ILocalizedMessage {

    public String Get(Message.Code code) {
        if (code == Message.Code.ParcelIntegrity)
            return "Prüfe Paket integrität...";
            
        else if (code == Message.Code.DetectTampering)
            return "Erkennung von Paketverflechtung...";
            
        else
            return null;
    }
}

We just created a simple localization class, which contains two translated messages.

Last Step: Linking the Translation

The second and the last step is connecting the just created localization class to the library.

Message.Localize(new GermanLocalization());

That's all! Now those two message will be shown in German; the remaining messages will remain the default English ones. In order to disconnect the translated messages call Message.Localize with null argument.