Class TranslationCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
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.
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.
Note: If your project uses preprocessed translation files and the original files do not have the
properties
extension, you can specify additional file extensions
via the fileExtensions
property.
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
baseName
- Specify Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources. Type isjava.util.regex.Pattern
. Default value is"^messages.*$"
. -
Property
fileExtensions
- Specify the file extensions of the files to process. Type isjava.lang.String[]
. Default value is.properties
. -
Property
requiredTranslations
- Specify language codes of required translations which must exist in project. Type isjava.lang.String[]
. Default value is""
.
Parent is com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
translation.missingKey
-
translation.missingTranslationFile
- Since:
- 3.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Class which represents a resource bundle.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Pattern
Specify Base name of resource bundles which contain message resources.private static final String
File name format for default translation.private static final String
Regexp string for default translation files.private static final String
File name format with language code.The files to process.private static final Pattern
Regexp pattern for bundles names which end with language code, followed by country code suffix.private static final Pattern
Regexp pattern for bundles names which end with language code, followed by country code and variant suffix.private static final Pattern
Regexp pattern for bundles names which end with language code suffix.private final org.apache.commons.logging.Log
Logger for TranslationCheck.static final String
A key is pointing to the warning message text for missing key in "messages.properties" file.static final String
A key is pointing to the warning message text for missing translation file in "messages.properties" file.private static final String
Formatting string to form regexp to validate default translations file names.private static final String
Formatting string to form regexp to validate required translations file names.Specify language codes of required translations which must exist in project.private static final String
Resource bundle which contains messages for TranslationCheck.private static final String
A key is pointing to the warning message text for wrong language code in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
beginProcessing
(String charset) Called when about to be called to process a set of files.private void
Checks an existence of default translation file in the resource bundle.private void
Checks an existence of translation files in the resource bundle.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).private void
Checks resource files in bundle for consistency regarding their keys.private static String
extractBaseName
(String fileName) Extracts the base name (the unique prefix) of resource bundle from translation file name.private static Optional<TranslationCheck.ResourceBundle>
findBundle
(Set<TranslationCheck.ResourceBundle> bundles, TranslationCheck.ResourceBundle targetBundle) Searches for specific resource bundle in a set of resource bundles.void
Called when all the files have been processed.getMissingFileName
(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.private static String
Extracts path from a file name which contains the path.getTranslationKeys
(File file) Loads the keys from the specified translation file into a set.private static Set<TranslationCheck.ResourceBundle>
groupFilesIntoBundles
(Set<File> files, Pattern baseNameRegexp) Groups a set of files into bundles.private static boolean
isValidLanguageCode
(String userSpecifiedLanguageCode) Checks whether user specified language code is correct (is contained in available locales).private void
logException
(Exception exception, File file) Helper method to log an exception.private void
logMissingTranslation
(String filePath, String fileName) Logs that translation file is missing.protected void
processFiltered
(File file, FileText fileText) Called to process a file that matches the specified file extensions.void
setBaseName
(Pattern baseName) Setter to specify Base name of resource bundles which contain message resources.void
setRequiredTranslations
(String... translationCodes) Setter to specify language codes of required translations which must exist in project.private void
validateUserSpecifiedLanguageCodes
(Set<String> languageCodes) Validates the correctness of user specified language codes for the check.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, destroy, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, 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 Details
-
MSG_KEY
A key is pointing to the warning message text for missing key in "messages.properties" file.- See Also:
-
MSG_KEY_MISSING_TRANSLATION_FILE
A key is pointing to the warning message text for missing translation file in "messages.properties" file.- See Also:
-
TRANSLATION_BUNDLE
Resource bundle which contains messages for TranslationCheck.- See Also:
-
WRONG_LANGUAGE_CODE_KEY
A key is pointing to the warning message text for wrong language code in "messages.properties" file.- See Also:
-
DEFAULT_TRANSLATION_REGEXP
Regexp string for default translation files. For example, messages.properties.- See Also:
-
LANGUAGE_COUNTRY_VARIANT_PATTERN
Regexp pattern for bundles names which end with language code, followed by country code and variant suffix. For example, messages_es_ES_UNIX.properties. -
LANGUAGE_COUNTRY_PATTERN
Regexp pattern for bundles names which end with language code, followed by country code suffix. For example, messages_es_ES.properties. -
LANGUAGE_PATTERN
Regexp pattern for bundles names which end with language code suffix. For example, messages_es.properties. -
DEFAULT_TRANSLATION_FILE_NAME_FORMATTER
File name format for default translation.- See Also:
-
FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER
File name format with language code.- See Also:
-
REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS
Formatting string to form regexp to validate required translations file names.- See Also:
-
REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS
Formatting string to form regexp to validate default translations file names.- See Also:
-
log
Logger for TranslationCheck. -
filesToProcess
The files to process. -
baseName
Specify Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources. -
requiredTranslations
Specify language codes of required translations which must exist in project.
-
-
Constructor Details
-
TranslationCheck
public TranslationCheck()Creates a newTranslationCheck
instance.
-
-
Method Details
-
setBaseName
Setter to specify Base name of resource bundles which contain message resources. It helps the check to distinguish config and localization resources.- Parameters:
baseName
- base name regexp.- Since:
- 6.17
-
setRequiredTranslations
Setter to specify language codes of required translations which must exist in project.- Parameters:
translationCodes
- language codes.- Since:
- 6.11
-
validateUserSpecifiedLanguageCodes
Validates the correctness of user specified language codes for the check.- Parameters:
languageCodes
- user specified language codes for the check.- Throws:
IllegalArgumentException
- when any item of languageCodes is not valid language code
-
isValidLanguageCode
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
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
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 processedfileText
- the contents of the file.
-
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 violations.- Specified by:
finishProcessing
in interfaceFileSetCheck
- Overrides:
finishProcessing
in classAbstractFileSetCheck
-
checkExistenceOfDefaultTranslation
Checks an existence of default translation file in the resource bundle.- Parameters:
bundle
- resource bundle.
-
checkExistenceOfRequiredTranslations
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 Optional<String> getMissingFileName(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.- 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
Logs that translation file is missing.- Parameters:
filePath
- file path.fileName
- file name.
-
groupFilesIntoBundles
private static Set<TranslationCheck.ResourceBundle> groupFilesIntoBundles(Set<File> files, 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 Optional<TranslationCheck.ResourceBundle> findBundle(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
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
Extracts path from a file name which contains the path. For example, if the file name is /xyz/messages.properties, then the method will return /xyz/.- Parameters:
fileNameWithPath
- file name which contains the path.- Returns:
- file path.
-
checkTranslationKeys
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.- Parameters:
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.- Parameters:
fileKeys
- a Map from translation files to their key sets.keysThatMustExist
- the set of keys to compare with.
-
getTranslationKeys
Loads the keys from the specified translation file into a set.- Parameters:
file
- translation file.- Returns:
- a Set object which holds the loaded keys.
-
logException
Helper method to log an exception.- Parameters:
exception
- the exception that occurredfile
- the file that could not be processed
-