Translation

Since Checkstyle 3.0

Description

Ensures the correct translation of code by checking property files for consistency regarding their keys. Two property files describing one and the same context are consistent if they contain the same keys. TranslationCheck also can check an existence of required translations which must exist in project, if requiredTranslations option is used.

Consider the following properties file in the same directory:

#messages.properties
hello=Hello
cancel=Cancel

#messages_de.properties
hell=Hallo
ok=OK
        

The Translation check will find the typo in the German hello key, the missing ok key in the default resource file and the missing cancel key in the German resource file:

messages_de.properties: Key 'hello' missing.
messages_de.properties: Key 'cancel' missing.
messages.properties: Key 'hell' missing.
messages.properties: Key 'ok' missing.
        

Notes

Language code for the property requiredTranslations is composed of the lowercase, two-letter codes as defined by ISO 639-1. Default value is empty String Set which means that only the existence of default translation is checked. Note, if you specify language codes (or just one language code) of required translations the check will also check for existence of default translation files in project.

Attention: the check will perform the validation of ISO codes if the option is used. So, if you specify, for example, "mm" for language code, TranslationCheck will rise violation that the language code is incorrect.

Attention: this Check could produce false-positives if it is used with Checker that use cache (property "cacheFile") This is known design problem, will be addressed at issue.

Properties

name description type default value since
fileExtensions Specify file type extension to identify translation files. Setting this property is typically only required if your translation files are preprocessed and the original files do not have the extension .properties String[] .properties 3.0
baseName Specify Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources. Pattern "^messages.*$" 6.17
requiredTranslations Specify language codes of required translations which must exist in project. String[] {} 6.11

Examples

Note, that files with the same path and base name but which have different extensions will be considered as files that belong to different resource bundles.

To configure the check to check only files which have '.properties' and '.translations' extensions:

<module name="Checker">
  <module name="Translation">
    <property name="fileExtensions" value="properties, translations"/>
    <property name="requiredTranslations" value="fr"/>
  </module>
</module>
        

Example:

#messages.properties
hello=Hello
cancel=Cancel

#messages.translations
hello=Hallo
ok=OK
        
messages.properties: Properties file 'messages_fr.properties' is missing.
messages.translations: Properties file 'messages_fr.translations' is missing.
        

An example of how to configure the check to validate only bundles which base names start with "ButtonLabels":

<module name="Checker">
  <module name="Translation">
    <property name="baseName" value="^ButtonLabels.*$"/>
    <property name="requiredTranslations" value="fr"/>
  </module>
</module>
        

Example:

#ButtonLabels.properties
hello=Hello
cancel=Cancel

#ButtonLabels_fr.properties
hello=Bonjour
name=Nom
        
ButtonLabels.properties: Key 'name' is missing.
ButtonLabels_fr.properties: Key 'cancel' is missing.
        

To configure the check to check existence of Japanese and French translations:

<module name="Checker">
  <module name="Translation">
    <property name="requiredTranslations" value="ja, fr"/>
  </module>
</module>
        

Example:

#messages.properties
hello=Hello
cancel=Cancel

#messages_ja.properties
greeting=こんにちは
age=年齢

#messages_fr.properties
greeting=Bonjour
name=Nom
        
messages.properties: Key 'age' missing.
messages.properties: Key 'name' missing.
messages_fr.properties: Key 'age' missing.
messages_fr.properties: Key 'cancel' missing.
messages_ja.properties: Key 'cancel' missing.
messages_ja.properties: Key 'name' missing.
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

Checker