From dea6dab124d3db9508da7c86bc331a9516cbb6e9 Mon Sep 17 00:00:00 2001
From: Andrei Selkin
Date: Tue, 16 Feb 2016 22:37:32 +0300
Subject: [PATCH] Issue #2251: Fix wrong support for resources with language,
country, variant
---
.../checkstyle/checks/TranslationCheck.java | 6 +--
.../checks/TranslationCheckTest.java | 40 +++++++++++++++++++
.../checks/messages_home.properties | 0
.../checks/messages_home_es_US.properties | 0
.../messages_home_fr_CA_UNIX.properties | 0
src/xdocs/config_misc.xml | 35 ++++++++++++++--
6 files changed, 74 insertions(+), 7 deletions(-)
create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home.properties
create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home_es_US.properties
create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/messages_home_fr_CA_UNIX.properties
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.
+ 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:
+
+ 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:
+