Issue #2969: Add the ability to validate language codes by TranslationCheck
This commit is contained in:
parent
5416e767af
commit
7eb6d92bc7
|
|
@ -91,7 +91,9 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
|
|||
* Default value is <b>empty String Set</b> 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.
|
||||
* <br>
|
||||
*
|
||||
* @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<String> 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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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\".
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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\".
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ upperEll=大文字の ''L'' を使用すべきです。
|
|||
|
||||
translation.missingKey=キー ''{0}'' がありません。
|
||||
translation.missingTranslationFile=プロパティファイル ''{0}'' がありません。
|
||||
translation.wrongLanguageCode=指定した言語コードは ''{0}'' が正しくありません。
|
||||
|
||||
missing.switch.default="default" 節の無い switch 文です。
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ upperEll=请使用大写''L'' 。
|
|||
|
||||
translation.missingKey=找不到 Key : ''{0}''。
|
||||
translation.missingTranslationFile=找不到配置文件: ''{0}'' 。
|
||||
translation.wrongLanguageCode=指定的語言代碼 ''{0}'' 是不正確。
|
||||
|
||||
missing.switch.default=switch 没有 \"default\" 从句。
|
||||
|
||||
|
|
|
|||
|
|
@ -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]"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
</td>
|
||||
<td><a href="property_types.html#stringSet">String Set</a></td>
|
||||
<td><code>empty String Set</code></td>
|
||||
|
|
|
|||
Loading…
Reference in New Issue