Class TranslationCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable, FileSetCheck

    public class TranslationCheck
    extends AbstractFileSetCheck

    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:


    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: Key 'hello' missing. Key 'cancel' missing. Key 'hell' missing. Key 'ok' missing.

    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.

    • Property 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 Type is java.lang.String[]. Default value is .properties.
    • Property baseName - Specify Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources. Type is java.util.regex.Pattern. Default value is "^messages.*$".
    • Property requiredTranslations - Specify language codes of required translations which must exist in project. Type is java.lang.String[]. Default value is "".

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

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

    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.

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

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

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

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

    The following example shows how the check works if there is a message bundle which element name contains language code, county code, platform name. Consider that we have the below configuration:

     <module name="Translation">
       <property name="requiredTranslations" value="es, fr, de"/>

    As we can see from the configuration, the TranslationCheck was configured to check an existence of 'es', 'fr' and 'de' translations. Let's assume that we have the resource bundle:

    Than the check will rise the following violation: "0: Properties file '' is missing."

    Parent is

    Violation Message Keys:

    • translation.missingKey
    • translation.missingTranslationFile
    • Constructor Detail

      • TranslationCheck

        public TranslationCheck()
        Creates a new TranslationCheck instance.
    • Method Detail

      • setBaseName

        public void setBaseName​(Pattern baseName)
        Setter to specify Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources.
        baseName - base name regexp.
      • setRequiredTranslations

        public void setRequiredTranslations​(String... translationCodes)
        Setter to specify language codes of required translations which must exist in project.
        translationCodes - language codes.
      • validateUserSpecifiedLanguageCodes

        private void validateUserSpecifiedLanguageCodes​(Set<String> languageCodes)
        Validates the correctness of user specified language codes for the check.
        languageCodes - user specified language codes for the check.
        IllegalArgumentException - when any item of languageCodes is not valid language code
      • isValidLanguageCode

        private static boolean isValidLanguageCode​(String userSpecifiedLanguageCode)
        Checks whether user specified language code is correct (is contained in available locales).
        userSpecifiedLanguageCode - user specified language code.
        true if user specified language code is correct.
      • checkExistenceOfRequiredTranslations

        private void checkExistenceOfRequiredTranslations​(TranslationCheck.ResourceBundle bundle)
        Checks an existence of translation files in the resource bundle. The name of translation file begins with the base name of resource bundle which is followed by '_' and a language code (country and variant are optional), it ends with the extension suffix.
        bundle - resource bundle.
      • getMissingFileName

        private static Optional<StringgetMissingFileName​(TranslationCheck.ResourceBundle bundle,
                                                           String languageCode)
        Returns the name of translation file which is absent in resource bundle or Guava's Optional, if there is not missing translation.
        bundle - resource bundle.
        languageCode - language code.
        the name of translation file which is absent in resource bundle or Guava's Optional, if there is not missing translation.
      • logMissingTranslation

        private void logMissingTranslation​(String filePath,
                                           String fileName)
        Logs that translation file is missing.
        filePath - file path.
        fileName - file name.
      • groupFilesIntoBundles

        private static Set<TranslationCheck.ResourceBundlegroupFilesIntoBundles​(Set<File> files,
                                                                                  Pattern baseNameRegexp)
        Groups a set of files into bundles. Only files, which names match base name regexp pattern will be grouped.
        files - set of files.
        baseNameRegexp - base name regexp pattern.
        set of ResourceBundles.
      • extractBaseName

        private static String extractBaseName​(String fileName)
        Extracts the base name (the unique prefix) of resource bundle from translation file name. For example "messages" is the base name of "", "", "", etc.
        fileName - the fully qualified name of the translation file.
        the extracted base name.
      • getPath

        private static String getPath​(String fileNameWithPath)
        Extracts path from a file name which contains the path. For example, if the file name is /xyz/, then the method will return /xyz/.
        fileNameWithPath - file name which contains the path.
        file path.
      • checkTranslationKeys

        private void checkTranslationKeys​(TranslationCheck.ResourceBundle bundle)
        Checks resource files in bundle for consistency regarding their keys. All files in bundle must have the same key set. If this is not the case an audit event message is posted giving information which key misses in which file.
        bundle - resource bundle.
      • checkFilesForConsistencyRegardingTheirKeys

        private void checkFilesForConsistencyRegardingTheirKeys​(Map<File,​Set<String>> fileKeys,
                                                                Set<String> keysThatMustExist)
        Compares th the specified key set with the key sets of the given translation files (arranged in a map). All missing keys are reported.
        fileKeys - a Map from translation files to their key sets.
        keysThatMustExist - the set of keys to compare with.
      • getTranslationKeys

        private Set<StringgetTranslationKeys​(File file)
        Loads the keys from the specified translation file into a set.
        file - translation file.
        a Set object which holds the loaded keys.
      • logException

        private void logException​(Exception exception,
                                  File file)
        Helper method to log an exception.
        exception - the exception that occurred
        file - the file that could not be processed