Enhancement of Cal10n, a library helps to localization Java software

Localization of Java project is not the easy task especially when you are working in the large codebase. While we have been working on the multi-languages of MyCollab project, we thought that the standard Java localization solution is not our right choice by several following reasons:

  • We have a lot of word phrases need to be externalized. In every release, we may add or remove many word phrases. We do not want to keep each word phrase as a string constant because we can not use the analytic tools to detect the unused word phrases
  • Weak validation tool: we can not check the matching between the string constants used in java file and the key of the Java properties automatically. Any mistyping will causes the word phrases are not translated

The library Cal10n comes to match our requirements. It can verify message keys in the source code, detects the error in message keys. It is simple to use, and because we use the enum value instead of string constants so we are able to refactor the word phrases easily.

Cal10n uses enum instead of String constants

and here is the project-page_en-US.properties file

To retrieve the internalized message

Caching the Locale instances and failover message

To improve the speed of getting the internalized message, you should cache the IMessageConveyor in the map object, and retrieve the instance IMessageConveyor from the map object with the key is the locale instance

In MyCollab, all phrases are English and we do not guarantee all messages are translated to some language. So, if the program can not seek the key from another language, it will get the associated value in the English language

Reloading resource files upon change

Cal10n supports to detect the resource changes and reloading the Message instances. However, Cal10n detect file changes by checking the latest updated time of file per 5 minutes, so it does not detect the changes immediately after the file change. We can enhance the Cal10n by using the Watching service API of Java 7. We let a thread detect any change of resource files, and mark the associated Message instances has change

You can check the full source codes of our enhancement at https://github.com/MyCollab/mycollab/tree/master/mycollab-localization/src/main/java/ch/qos/cal10n.

The drawback of using the Watching Service API, that it requires the Java 7+ Cal10n supports Java 5+ but now who care to support Java 5,6 except the legacy system 🙂

2 Comments

  • While it’s a bit different approach (not a compile-time), resource4j library does support enumeration constants, reloading of message bundles from multiple sources (even from database) and custom message bundle formats (e.g. HOCON). Have you seen it?

  • Hi Ivan,

    At the time I searched a tool for localization, we just find two tools: gettext and Cal10n at the time of 2 years ago. Unfortunately, we can not add your tool on our evaluation list because we did not found it by Google though it seems it is started before the time I seek such tool. Per our project needs, it seems Cal10n is simpler, and we do not have any extra features that Cal10n did not support except its reloading resource checking is awful but we can customize it as this blog post give the way.

Leave a Reply

Your email address will not be published. Required fields are marked *