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 e87d341b4..8965e6abe 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java @@ -91,7 +91,9 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils; * 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. + * 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. *
* * @author Alexandra Bunge @@ -113,6 +115,16 @@ public class TranslationCheck extends AbstractFileSetCheck { public static final String MSG_KEY_MISSING_TRANSLATION_FILE = "translation.missingTranslationFile"; + /** Resource bundle which contains messages for TranslationCheck. */ + private static final String TRANSLATION_BUNDLE = + "com.puppycrawl.tools.checkstyle.checks.messages"; + + /** + * A key is pointing to the warning message text for wrong language code + * in "messages.properties" file. + */ + private static final String WRONG_LANGUAGE_CODE_KEY = "translation.wrongLanguageCode"; + /** Logger for TranslationCheck. */ private static final Log LOG = LogFactory.getLog(TranslationCheck.class); @@ -186,6 +198,7 @@ public class TranslationCheck extends AbstractFileSetCheck { public void setRequiredTranslations(String translationCodes) { requiredTranslations = Sets.newTreeSet(Splitter.on(',') .trimResults().omitEmptyStrings().split(translationCodes)); + validateUserSpecifiedLanguageCodes(requiredTranslations); } @Override @@ -212,6 +225,39 @@ public class TranslationCheck extends AbstractFileSetCheck { } } + /** + * Validates the correctness of user specififed language codes for the check. + * @param languageCodes user specified language codes for the check. + */ + private void validateUserSpecifiedLanguageCodes(SortedSet languageCodes) { + for (String code : languageCodes) { + if (!isValidLanguageCode(code)) { + final LocalizedMessage msg = new LocalizedMessage(0, TRANSLATION_BUNDLE, + WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null); + final String exceptionMessage = String.format(Locale.ROOT, + "%s [%s]", msg.getMessage(), TranslationCheck.class.getSimpleName()); + throw new IllegalArgumentException(exceptionMessage); + } + } + } + + /** + * Checks whether user specified language code is correct (is contained in available locales). + * @param userSpecifiedLanguageCode user specified language code. + * @return true if user specified language code is correct. + */ + private static boolean isValidLanguageCode(final String userSpecifiedLanguageCode) { + boolean valid = false; + final Locale[] locales = Locale.getAvailableLocales(); + for (Locale locale : locales) { + if (userSpecifiedLanguageCode.equals(locale.toString())) { + valid = true; + break; + } + } + return valid; + } + /** * Groups a set of files into bundles. * Only files, which names match base name regexp pattern will be grouped. diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties index d80905bb5..99699cc2a 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -6,6 +6,7 @@ todo.match=Comment matches to-do format ''{0}''. upperEll=Should use uppercase ''L''. translation.missingKey=Key ''{0}'' missing. +translation.wrongLanguageCode=Specified language code ''{0}'' is incorrect. translation.missingTranslationFile=Properties file ''{0}'' is missing. missing.switch.default=switch without \"default\" clause. diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_de.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_de.properties index c2889d4e8..0339eb3d0 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_de.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_de.properties @@ -6,6 +6,7 @@ todo.match=Kommentar entspricht to-do-Format ''{0}''. upperEll=Zur besseren Lesbarkeit sollte ein großes ''L'' verwendet werden. translation.missingKey=Übersetzung für Schlüssel ''{0}'' fehlt. +translation.wrongLanguageCode=Spezifizierte Sprachcode ''{0}'' ist nicht korrekt. translation.missingTranslationFile=Properties-Datei ''{0}'' fehlt. missing.switch.default=switch ohne \"default\". diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_es.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_es.properties index 3d03de3fe..bd9e9ef35 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_es.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_es.properties @@ -9,6 +9,7 @@ translation.missingKey=La clave ''{0}'' falta. missing.switch.default=switch sin etiqueta \"default\". translation.missingTranslationFile=Archivo de propiedades ''{0}'' no se encuentra. +translation.wrongLanguageCode=Código de idioma especificado ''{0}'' no es correcta. uncommented.main=Se encotró un método main sin comentar. diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fi.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fi.properties index ed61272c3..6d7733b3e 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fi.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fi.properties @@ -7,6 +7,7 @@ upperEll=Pitää olla iso ''L''. translation.missingKey=Käännösavain ''{0}'' puuttuu. translation.missingTranslationFile=Ominaisuudet tiedosto ''{0}'' puuttuu. +translation.wrongLanguageCode=Määritetty kielikoodi ''{0}'' on virheellinen. missing.switch.default = vaihtaa ilman \"default\" lauseke. diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fr.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fr.properties index f2d47b932..ecab6e589 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fr.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_fr.properties @@ -7,6 +7,7 @@ upperEll=Utilisez un ''L'' majuscule pour une meilleure lisibilité. translation.missingKey=La traduction du message ''{0}'' est manquante. translation.missingTranslationFile=Fichier de propriétés ''{0}'' est manquant. +translation.wrongLanguageCode=Spécifié code de langue ''{0}'' est incorrect. missing.switch.default=Il manque le cas \"default\" dans le bloc \"switch\". diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_ja.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_ja.properties index 2aa315a33..71bd0a56b 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_ja.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_ja.properties @@ -7,6 +7,7 @@ upperEll=大文字の ''L'' を使用すべきです。 translation.missingKey=キー ''{0}'' がありません。 translation.missingTranslationFile=プロパティファイル ''{0}'' がありません。 +translation.wrongLanguageCode=指定した言語コードは ''{0}'' が正しくありません。 missing.switch.default="default" 節の無い switch 文です。 diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_pt.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_pt.properties index e13d68141..783a7cd65 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_pt.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_pt.properties @@ -7,6 +7,7 @@ todo.match=O comentário condiz com o padrão de tarefa pendente ''{0}''. translation.missingKey=Falta a chave ''{0}''. translation.missingTranslationFile=Arquivo de propriedades ''{0}'' está faltando. +translation.wrongLanguageCode=Código de idioma especificado ''{0}'' está incorreto. uncommented.main=Método main não comentado encontrado. diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_tr.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_tr.properties index f61769d2e..f8422cf82 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_tr.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_tr.properties @@ -7,6 +7,7 @@ upperEll = Büyük harf ''L'' kullanılmalı. translation.missingKey = ''{0}'' anahtarı eksik. translation.missingTranslationFile=Özellikler dosyası ''{0}'' eksik. +translation.wrongLanguageCode=Belirtilen dil kodu ''{0}'' 'yanlıştır. missing.switch.default = ''default'' durumu olmayan bir ''switch'' mevcut. diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_zh.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_zh.properties index 1b97530b3..793f0840a 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_zh.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages_zh.properties @@ -7,6 +7,7 @@ upperEll=请使用大写''L'' 。 translation.missingKey=找不到 Key : ''{0}''。 translation.missingTranslationFile=找不到配置文件: ''{0}'' 。 +translation.wrongLanguageCode=指定的語言代碼 ''{0}'' 是不正確。 missing.switch.default=switch 没有 \"default\" 从句。 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 8dc1bd0ba..dc6bc6506 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java @@ -20,6 +20,10 @@ package com.puppycrawl.tools.checkstyle.checks; import static com.puppycrawl.tools.checkstyle.checks.TranslationCheck.MSG_KEY; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.endsWith; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; @@ -388,4 +392,18 @@ public class TranslationCheckTest extends BaseCheckTestSupport { getPath(""), expected); } + + @Test + public void testWrongUserSpecifiedLanguageCodes() throws Exception { + final TranslationCheck check = new TranslationCheck(); + try { + check.setRequiredTranslations("11"); + fail("IllegalArgumentException is expected. Specified language code is incorrect."); + } + catch (IllegalArgumentException ex) { + final String exceptionMessage = ex.getMessage(); + assertThat(exceptionMessage, containsString("11")); + assertThat(exceptionMessage, endsWith("[TranslationCheck]")); + } + } } diff --git a/src/xdocs/config_misc.xml b/src/xdocs/config_misc.xml index a15dbb5a5..0b9acf0d8 100755 --- a/src/xdocs/config_misc.xml +++ b/src/xdocs/config_misc.xml @@ -1629,10 +1629,9 @@ messages.properties: Key 'ok' missing. 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. + 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. String Set empty String Set