diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java index 6a53e7cd0..4e5681285 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java @@ -202,11 +202,9 @@ public class TranslationCheck */ private void checkExistenceOfRequiredTranslations(Set filesInResourceBundle) { final String fullBundleName = getFullBundleName(filesInResourceBundle); - final String extension = getFileExtensions()[0]; for (String languageCode : requiredTranslations) { - final String translationFileName = - fullBundleName + '_' + languageCode + extension; + final String translationFileName = fullBundleName + '_' + languageCode; final boolean missing = isMissing(translationFileName, filesInResourceBundle); if (missing) { @@ -255,7 +253,7 @@ public class TranslationCheck boolean missing = false; for (File file : filesInResourceBundle) { final String currentFileName = file.getPath(); - missing = !currentFileName.equals(fileName); + missing = !currentFileName.contains(fileName); if (!missing) { break; } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java index d8e2a4a85..e8e6b0017 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java @@ -230,4 +230,44 @@ public class TranslationCheckTest getPath("app-dev.properties"), expected); } + + @Test + public void testTranslationFileWithLanguageCountryVariantIsMissing() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(TranslationCheck.class); + checkConfig.addAttribute("requiredTranslations", "es, de"); + + final File[] propertyFiles = { + new File(getPath("messages_home.properties")), + new File(getPath("messages_home_es_US.properties")), + new File(getPath("messages_home_fr_CA_UNIX.properties")), + }; + + final String[] expected = { + "0: Properties file 'messages_home_de.properties' is missing.", + }; + verify( + createChecker(checkConfig), + propertyFiles, + getPath(""), + expected); + } + + @Test + public void testTranslationFileWithLanguageCountryVariantArePresent() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(TranslationCheck.class); + checkConfig.addAttribute("requiredTranslations", "es, fr"); + + final File[] propertyFiles = { + new File(getPath("messages_home.properties")), + new File(getPath("messages_home_es_US.properties")), + new File(getPath("messages_home_fr_CA_UNIX.properties")), + }; + + final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; + verify( + createChecker(checkConfig), + propertyFiles, + getPath(""), + expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home.properties b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home.properties new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home_es_US.properties b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home_es_US.properties new file mode 100644 index 000000000..e69de29bb diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home_fr_CA_UNIX.properties b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home_fr_CA_UNIX.properties new file mode 100644 index 000000000..e69de29bb diff --git a/src/xdocs/config_misc.xml b/src/xdocs/config_misc.xml index 37577d476..996f0123d 100755 --- a/src/xdocs/config_misc.xml +++ b/src/xdocs/config_misc.xml @@ -1615,11 +1615,15 @@ messages.properties: Key 'ok' missing. Allows to specify language codes of required translations which must exist in project. The check looks only for messages bundles which names contain the word 'messages'. Language code is composed of the lowercase, two-letter codes as defined by - ISO 639-1. + 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 not perform the validation of ISO codes if the option + is set to true. So, if you specify, for example, "mm" for language code, + TranslationCheck will not warn about the incorrect language code and will use it + for validation. String Set empty String Set @@ -1640,7 +1644,7 @@ messages.properties: Key 'ok' missing.

<module name="Translation"> - <property name="basenameSeparator" value="STRING_LITERAL"/> + <property name="basenameSeparator" value="-"/> </module> @@ -1649,9 +1653,34 @@ messages.properties: Key 'ok' missing.

<module name="Translation"> -<property name="requiredTranslations" value="ja, fr"/> + <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 for + 'es', 'fr' and 'de' translations. Lets assume that we have the resource bunbdle: +

+ +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." +