Class TranslationCheck
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
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.
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 isjava.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 isjava.util.regex.Pattern
. Default value is"^messages.*$"
. -
Property
requiredTranslations
- Specify language codes of required translations which must exist in project. Type isjava.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"/> </module>
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.*$"/> </module>
To configure the check to check existence of Japanese and French translations:
<module name="Translation"> <property name="requiredTranslations" value="ja, fr"/> </module>
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"/> </module>
As we can see from the configuration, the TranslationCheck was configured to check an existence of 'es', 'fr' and 'de' translations. Lets assume that we have the resource bundle:
messages_home.properties messages_home_es_US.properties messages_home_fr_CA_UNIX.properties
Than the check will rise the following violation: "0: Properties file 'messages_home_de.properties' is missing."
Parent is com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
translation.missingKey
-
translation.missingTranslationFile
- Since:
- 3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
beginProcessing
(String charset) Called when about to be called to process a set of files.void
Called when all the files have been processed.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.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addMessages, destroy, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getMessages, getTabWidth, 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.api.AutomaticBean
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:
-
-
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.
-
setRequiredTranslations
Setter to specify language codes of required translations which must exist in project.- Parameters:
translationCodes
- a comma separated list of language codes.
-
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
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
-