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:
#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.
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.
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.*$".
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"/> </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
| Modifier and Type | Class and Description |
|---|---|
private static class |
TranslationCheck.ResourceBundle
Class which represents a resource bundle.
|
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private java.util.regex.Pattern |
baseName
Specify
Base name of resource bundles which contain message resources.
|
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 which end with language code, followed by country code
suffix.
|
private static java.util.regex.Pattern |
LANGUAGE_COUNTRY_VARIANT_PATTERN
Regexp pattern for bundles names which end with language code, followed by country code and
variant suffix.
|
private static java.util.regex.Pattern |
LANGUAGE_PATTERN
Regexp pattern for bundles names which end with language code suffix.
|
private 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
Specify language codes of required translations which must exist in project.
|
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 and Description |
|---|
TranslationCheck()
Creates a new
TranslationCheck instance. |
| Modifier and Type | Method and 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(java.util.Map<java.io.File,java.util.Set<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 |
logException(java.lang.Exception exception,
java.io.File file)
Helper method to log an 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,
FileText fileText)
Called to process a file that matches the specified file extensions.
|
void |
setBaseName(java.util.regex.Pattern baseName)
Setter to specify
Base name of resource bundles which contain message resources.
|
void |
setRequiredTranslations(java.lang.String... translationCodes)
Setter to specify language codes of required translations which must exist in project.
|
private void |
validateUserSpecifiedLanguageCodes(java.util.Set<java.lang.String> languageCodes)
Validates the correctness of user specified language codes for the check.
|
addViolations, destroy, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidthfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconfigurecontextualizepublic static final java.lang.String MSG_KEY
public static final java.lang.String MSG_KEY_MISSING_TRANSLATION_FILE
private static final java.lang.String TRANSLATION_BUNDLE
private static final java.lang.String WRONG_LANGUAGE_CODE_KEY
private static final java.lang.String DEFAULT_TRANSLATION_REGEXP
private static final java.util.regex.Pattern LANGUAGE_COUNTRY_VARIANT_PATTERN
private static final java.util.regex.Pattern LANGUAGE_COUNTRY_PATTERN
private static final java.util.regex.Pattern LANGUAGE_PATTERN
private static final java.lang.String DEFAULT_TRANSLATION_FILE_NAME_FORMATTER
private static final java.lang.String FILE_NAME_WITH_LANGUAGE_CODE_FORMATTER
private static final java.lang.String REGEXP_FORMAT_TO_CHECK_REQUIRED_TRANSLATIONS
private static final java.lang.String REGEXP_FORMAT_TO_CHECK_DEFAULT_TRANSLATIONS
private final org.apache.commons.logging.Log log
private final java.util.Set<java.io.File> filesToProcess
private java.util.regex.Pattern baseName
private java.util.Set<java.lang.String> requiredTranslations
public TranslationCheck()
TranslationCheck instance.public void setBaseName(java.util.regex.Pattern baseName)
baseName - base name regexp.public void setRequiredTranslations(java.lang.String... translationCodes)
translationCodes - a comma separated list of language codes.private void validateUserSpecifiedLanguageCodes(java.util.Set<java.lang.String> languageCodes)
languageCodes - user specified language codes for the check.java.lang.IllegalArgumentException - when any item of languageCodes is not valid language codeprivate static boolean isValidLanguageCode(java.lang.String userSpecifiedLanguageCode)
userSpecifiedLanguageCode - user specified language code.public void beginProcessing(java.lang.String charset)
FileSetCheckbeginProcessing in interface FileSetCheckbeginProcessing in class AbstractFileSetCheckcharset - the character set used to read the files.protected void processFiltered(java.io.File file, FileText fileText)
AbstractFileSetCheckprocessFiltered in class AbstractFileSetCheckfile - the file to be processedfileText - the contents of the file.public void finishProcessing()
FileSetCheckfinishProcessing in interface FileSetCheckfinishProcessing in class AbstractFileSetCheckprivate void checkExistenceOfDefaultTranslation(TranslationCheck.ResourceBundle bundle)
bundle - resource bundle.private void checkExistenceOfRequiredTranslations(TranslationCheck.ResourceBundle bundle)
bundle - resource bundle.private static java.util.Optional<java.lang.String> getMissingFileName(TranslationCheck.ResourceBundle bundle, java.lang.String languageCode)
bundle - resource bundle.languageCode - language code.private void logMissingTranslation(java.lang.String filePath, java.lang.String fileName)
filePath - file path.fileName - file name.private static java.util.Set<TranslationCheck.ResourceBundle> groupFilesIntoBundles(java.util.Set<java.io.File> files, java.util.regex.Pattern baseNameRegexp)
files - set of files.baseNameRegexp - base name regexp pattern.private static java.util.Optional<TranslationCheck.ResourceBundle> findBundle(java.util.Set<TranslationCheck.ResourceBundle> bundles, TranslationCheck.ResourceBundle targetBundle)
bundles - set of resource bundles.targetBundle - target bundle to search for.private static java.lang.String extractBaseName(java.lang.String fileName)
fileName - the fully qualified name of the translation file.private static java.lang.String getPath(java.lang.String fileNameWithPath)
fileNameWithPath - file name which contains the path.private void checkTranslationKeys(TranslationCheck.ResourceBundle bundle)
bundle - resource bundle.private void checkFilesForConsistencyRegardingTheirKeys(java.util.Map<java.io.File,java.util.Set<java.lang.String>> fileKeys, java.util.Set<java.lang.String> keysThatMustExist)
fileKeys - a Map from translation files to their key sets.keysThatMustExist - the set of keys to compare with.private java.util.Set<java.lang.String> getTranslationKeys(java.io.File file)
file - translation file.private void logException(java.lang.Exception exception, java.io.File file)
exception - the exception that occurredfile - the file that could not be processedCopyright © 2001-2022. All Rights Reserved.