Class TranslationCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
-
- com.puppycrawl.tools.checkstyle.checks.TranslationCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
public class TranslationCheck extends AbstractFileSetCheck
The TranslationCheck class helps to ensure the correct translation of code by checking locale-specific resource files for consistency regarding their keys. Two locale-specific resource 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.
An example of how to configure the check is:
<module name="Translation"/>
Check has the following options:baseName - a base name regexp for resource bundles which contain message resources. It helps the check to distinguish config and localization resources. Default value is ^messages.*$
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.*$"/> </module>
To configure the check to check only files which have '.properties' and '.translations' extensions:
<module name="Translation"> <property name="fileExtensions" value="properties, translations"/> </module>
requiredTranslations which allows to specify language codes of required translations which must exist in project. Language code 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TranslationCheck.ResourceBundle
Class which represents a resource bundle.
-
Field Summary
Fields Modifier and Type Field Description private java.util.regex.Pattern
baseName
The base name regexp pattern.private static java.lang.String
DEFAULT_TRANSLATION_FILE_NAME_FORMATTER
File name format for default translation.private static java.lang.String
DEFAULT_TRANSLATION_REGEXP
Regexp string for default translation files.private static java.lang.String
FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER
File name format with language code.private java.util.Set<java.io.File>
filesToProcess
The files to process.private static java.util.regex.Pattern
LANGUAGE_COUNTRY_PATTERN
Regexp pattern for bundles names wich end with language code, followed by country code suffix.private static java.util.regex.Pattern
LANGUAGE_COUNTRY_VARIANT_PATTERN
Regexp pattern for bundles names wich end with language code, followed by country code and variant suffix.private static java.util.regex.Pattern
LANGUAGE_PATTERN
Regexp pattern for bundles names wich end with language code suffix.private static org.apache.commons.logging.Log
LOG
Logger for TranslationCheck.static java.lang.String
MSG_KEY
A key is pointing to the warning message text for missing key in "messages.properties" file.static java.lang.String
MSG_KEY_MISSING_TRANSLATION_FILE
A key is pointing to the warning message text for missing translation file in "messages.properties" file.private static java.lang.String
REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS
Formatting string to form regexp to validate default translations file names.private static java.lang.String
REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS
Formatting string to form regexp to validate required translations file names.private java.util.Set<java.lang.String>
requiredTranslations
Language codes of required translations for the check (de, pt, ja, etc).private static java.lang.String
TRANSLATION_BUNDLE
Resource bundle which contains messages for TranslationCheck.private static java.lang.String
WRONG_LANGUAGE_CODE_KEY
A key is pointing to the warning message text for wrong language code in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description TranslationCheck()
Creates a newTranslationCheck
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginProcessing(java.lang.String charset)
Called when about to be called to process a set of files.private void
checkExistenceOfDefaultTranslation(TranslationCheck.ResourceBundle bundle)
Checks an existence of default translation file in the resource bundle.private void
checkExistenceOfRequiredTranslations(TranslationCheck.ResourceBundle bundle)
Checks an existence of translation files in the resource bundle.private void
checkFilesForConsistencyRegardingTheirKeys(com.google.common.collect.SetMultimap<java.io.File,java.lang.String> fileKeys, java.util.Set<java.lang.String> keysThatMustExist)
Compares th the specified key set with the key sets of the given translation files (arranged in a map).private void
checkTranslationKeys(TranslationCheck.ResourceBundle bundle)
Checks resource files in bundle for consistency regarding their keys.private static java.lang.String
extractBaseName(java.lang.String fileName)
Extracts the base name (the unique prefix) of resource bundle from translation file name.private static java.util.Optional<TranslationCheck.ResourceBundle>
findBundle(java.util.Set<TranslationCheck.ResourceBundle> bundles, TranslationCheck.ResourceBundle targetBundle)
Searches for specific resource bundle in a set of resource bundles.void
finishProcessing()
Called when all the files have been processed.private static java.util.Optional<java.lang.String>
getMissingFileName(TranslationCheck.ResourceBundle bundle, java.lang.String languageCode)
Returns the name of translation file which is absent in resource bundle or Guava's Optional, if there is not missing translation.private static java.lang.String
getPath(java.lang.String fileNameWithPath)
Extracts path from a file name which contains the path.private java.util.Set<java.lang.String>
getTranslationKeys(java.io.File file)
Loads the keys from the specified translation file into a set.private static java.util.Set<TranslationCheck.ResourceBundle>
groupFilesIntoBundles(java.util.Set<java.io.File> files, java.util.regex.Pattern baseNameRegexp)
Groups a set of files into bundles.private static boolean
isValidLanguageCode(java.lang.String userSpecifiedLanguageCode)
Checks whether user specified language code is correct (is contained in available locales).private void
logIoException(java.io.IOException exception, java.io.File file)
Helper method to log an io exception.private void
logMissingTranslation(java.lang.String filePath, java.lang.String fileName)
Logs that translation file is missing.protected void
processFiltered(java.io.File file, java.util.List<java.lang.String> lines)
Called to process a file that matches the specified file extensions.void
setBaseName(java.util.regex.Pattern baseName)
Sets the base name regexp pattern.void
setRequiredTranslations(java.lang.String... translationCodes)
Sets language codes of required translations for the check.private void
validateUserSpecifiedLanguageCodes(java.util.Set<java.lang.String> languageCodes)
Validates the correctness of user specified language codes for the check.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
destroy, fireErrors, getFileExtensions, getMessageCollector, getMessageDispatcher, init, log, log, process, setFileExtensions, setMessageDispatcher
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text for missing key in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_MISSING_TRANSLATION_FILE
public static final java.lang.String MSG_KEY_MISSING_TRANSLATION_FILE
A key is pointing to the warning message text for missing translation file in "messages.properties" file.- See Also:
- Constant Field Values
-
TRANSLATION_BUNDLE
private static final java.lang.String TRANSLATION_BUNDLE
Resource bundle which contains messages for TranslationCheck.- See Also:
- Constant Field Values
-
WRONG_LANGUAGE_CODE_KEY
private static final java.lang.String WRONG_LANGUAGE_CODE_KEY
A key is pointing to the warning message text for wrong language code in "messages.properties" file.- See Also:
- Constant Field Values
-
LOG
private static final org.apache.commons.logging.Log LOG
Logger for TranslationCheck.
-
DEFAULT_TRANSLATION_REGEXP
private static final java.lang.String DEFAULT_TRANSLATION_REGEXP
Regexp string for default translation files. For example, messages.properties.- See Also:
- Constant Field Values
-
LANGUAGE_COUNTRY_VARIANT_PATTERN
private static final java.util.regex.Pattern LANGUAGE_COUNTRY_VARIANT_PATTERN
Regexp pattern for bundles names wich end with language code, followed by country code and variant suffix. For example, messages_es_ES_UNIX.properties.
-
LANGUAGE_COUNTRY_PATTERN
private static final java.util.regex.Pattern LANGUAGE_COUNTRY_PATTERN
Regexp pattern for bundles names wich end with language code, followed by country code suffix. For example, messages_es_ES.properties.
-
LANGUAGE_PATTERN
private static final java.util.regex.Pattern LANGUAGE_PATTERN
Regexp pattern for bundles names wich end with language code suffix. For example, messages_es.properties.
-
DEFAULT_TRANSLATION_FILE_NAME_FORMATTER
private static final java.lang.String DEFAULT_TRANSLATION_FILE_NAME_FORMATTER
File name format for default translation.- See Also:
- Constant Field Values
-
FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER
private static final java.lang.String FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER
File name format with language code.- See Also:
- Constant Field Values
-
REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS
private static final java.lang.String REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS
Formatting string to form regexp to validate required translations file names.- See Also:
- Constant Field Values
-
REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS
private static final java.lang.String REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS
Formatting string to form regexp to validate default translations file names.- See Also:
- Constant Field Values
-
filesToProcess
private final java.util.Set<java.io.File> filesToProcess
The files to process.
-
baseName
private java.util.regex.Pattern baseName
The base name regexp pattern.
-
requiredTranslations
private java.util.Set<java.lang.String> requiredTranslations
Language codes of required translations for the check (de, pt, ja, etc).
-
-
Method Detail
-
setBaseName
public void setBaseName(java.util.regex.Pattern baseName)
Sets the base name regexp pattern.- Parameters:
baseName
- base name regexp.
-
setRequiredTranslations
public void setRequiredTranslations(java.lang.String... translationCodes)
Sets language codes of required translations for the check.- Parameters:
translationCodes
- a comma separated list of language codes.
-
validateUserSpecifiedLanguageCodes
private void validateUserSpecifiedLanguageCodes(java.util.Set<java.lang.String> languageCodes)
Validates the correctness of user specified language codes for the check.- Parameters:
languageCodes
- user specified language codes for the check.
-
isValidLanguageCode
private static boolean isValidLanguageCode(java.lang.String userSpecifiedLanguageCode)
Checks whether user specified language code is correct (is contained in available locales).- Parameters:
userSpecifiedLanguageCode
- user specified language code.- Returns:
- true if user specified language code is correct.
-
beginProcessing
public void beginProcessing(java.lang.String charset)
Description copied from interface:FileSetCheck
Called when about to be called to process a set of files.- Specified by:
beginProcessing
in interfaceFileSetCheck
- Overrides:
beginProcessing
in classAbstractFileSetCheck
- Parameters:
charset
- the character set used to read the files.
-
processFiltered
protected void processFiltered(java.io.File file, java.util.List<java.lang.String> lines)
Description copied from class:AbstractFileSetCheck
Called to process a file that matches the specified file extensions.- Specified by:
processFiltered
in classAbstractFileSetCheck
- Parameters:
file
- the file to be processedlines
- an immutable list of the contents of the file.
-
finishProcessing
public void finishProcessing()
Description copied from interface:FileSetCheck
Called when all the files have been processed. This is the time to perform any checks that need to be done across a set of files. In this method, the implementation is responsible for the logging of messages.- Specified by:
finishProcessing
in interfaceFileSetCheck
- Overrides:
finishProcessing
in classAbstractFileSetCheck
-
checkExistenceOfDefaultTranslation
private void checkExistenceOfDefaultTranslation(TranslationCheck.ResourceBundle bundle)
Checks an existence of default translation file in the resource bundle.- Parameters:
bundle
- resource bundle.
-
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.- Parameters:
bundle
- resource bundle.
-
getMissingFileName
private static java.util.Optional<java.lang.String> getMissingFileName(TranslationCheck.ResourceBundle bundle, java.lang.String languageCode)
Returns the name of translation file which is absent in resource bundle or Guava's Optional, if there is not missing translation.- Parameters:
bundle
- resource bundle.languageCode
- language code.- Returns:
- 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(java.lang.String filePath, java.lang.String fileName)
Logs that translation file is missing.- Parameters:
filePath
- file path.fileName
- file name.
-
groupFilesIntoBundles
private static java.util.Set<TranslationCheck.ResourceBundle> groupFilesIntoBundles(java.util.Set<java.io.File> files, java.util.regex.Pattern baseNameRegexp)
Groups a set of files into bundles. Only files, which names match base name regexp pattern will be grouped.- Parameters:
files
- set of files.baseNameRegexp
- base name regexp pattern.- Returns:
- set of ResourceBundles.
-
findBundle
private static java.util.Optional<TranslationCheck.ResourceBundle> findBundle(java.util.Set<TranslationCheck.ResourceBundle> bundles, TranslationCheck.ResourceBundle targetBundle)
Searches for specific resource bundle in a set of resource bundles.- Parameters:
bundles
- set of resource bundles.targetBundle
- target bundle to search for.- Returns:
- Guava's Optional of resource bundle (present if target bundle is found).
-
extractBaseName
private static java.lang.String extractBaseName(java.lang.String fileName)
Extracts the base name (the unique prefix) of resource bundle from translation file name. For example "messages" is the base name of "messages.properties", "messages_de_AT.properties", "messages_en.properties", etc.- Parameters:
fileName
- the fully qualified name of the translation file.- Returns:
- the extracted base name.
-
getPath
private static java.lang.String getPath(java.lang.String fileNameWithPath)
Extracts path from a file name which contains the path. For example, if file nam is /xyz/messages.properties, then the method will return /xyz/.- Parameters:
fileNameWithPath
- file name which contains the path.- Returns:
- 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 error message is posted giving information which key misses in which file.- Parameters:
bundle
- resource bundle.
-
checkFilesForConsistencyRegardingTheirKeys
private void checkFilesForConsistencyRegardingTheirKeys(com.google.common.collect.SetMultimap<java.io.File,java.lang.String> fileKeys, java.util.Set<java.lang.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.- Parameters:
fileKeys
- a Map from translation files to their key sets.keysThatMustExist
- the set of keys to compare with.
-
getTranslationKeys
private java.util.Set<java.lang.String> getTranslationKeys(java.io.File file)
Loads the keys from the specified translation file into a set.- Parameters:
file
- translation file.- Returns:
- a Set object which holds the loaded keys.
-
logIoException
private void logIoException(java.io.IOException exception, java.io.File file)
Helper method to log an io exception.- Parameters:
exception
- the exception that occurredfile
- the file that could not be processed
-
-