diff --git a/src/main/java/com/crowdin/cli/properties/FileBean.java b/src/main/java/com/crowdin/cli/properties/FileBean.java index eec195e6..fd9db8c6 100755 --- a/src/main/java/com/crowdin/cli/properties/FileBean.java +++ b/src/main/java/com/crowdin/cli/properties/FileBean.java @@ -9,9 +9,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; import static com.crowdin.cli.properties.PropertiesBuilder.*; +import static com.crowdin.cli.utils.language.LangCodesUtil.isValidCode; @Data public class FileBean { @@ -171,6 +173,18 @@ public List checkProperties(FileBean bean) { } } + Map> languagesMapping = bean.getLanguagesMapping(); + + if (languagesMapping != null) { + Optional invalidCode = languagesMapping.values().stream() + .flatMap(innerMap -> innerMap.keySet().stream()) + .filter(code -> !isValidCode(code)) + .findFirst(); + if (invalidCode.isPresent()) { + errors.add(RESOURCE_BUNDLE.getString("error.config.languages_mapping")); + } + } + String updateOption = bean.getUpdateOption(); if (updateOption != null && !(updateOption.equals("update_as_unapproved") || updateOption.equals("update_without_changes"))) { errors.add(RESOURCE_BUNDLE.getString("error.config.update_option")); diff --git a/src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java b/src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java new file mode 100644 index 00000000..f80b7f39 --- /dev/null +++ b/src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java @@ -0,0 +1,330 @@ +package com.crowdin.cli.utils.language; + +import java.util.Set; + +public class LangCodesUtil { + private static final Set LANGUAGE_CODES; + + public static boolean isValidCode(String code) { + return LANGUAGE_CODES.contains(code.toLowerCase()); + } + + static { + LANGUAGE_CODES = Set.of( + "ach", + "aa", + "af", + "ak", + "tw", + "sq", + "am", + "ar", + "ar-bh", + "ar-eg", + "ar-sa", + "ar-ye", + "an", + "hy-am", + "frp", + "as", + "ast", + "tay", + "av", + "ae", + "ay", + "az", + "ban", + "bal", + "bm", + "ba", + "eu", + "be", + "bn", + "bn-in", + "ber", + "bh", + "bfo", + "bi", + "bs", + "br-fr", + "bg", + "my", + "ca", + "ceb", + "ch", + "ce", + "chr", + "ny", + "zh-cn", + "zh-tw", + "zh-hk", + "zh-mo", + "zh-sg", + "cv", + "kw", + "co", + "cr", + "hr", + "cs", + "da", + "fa-af", + "dv", + "nl", + "nl-be", + "nl-sr", + "dz", + "en", + "en-ud", + "en-ar", + "en-au", + "en-bz", + "en-ca", + "en-cb", + "en-cn", + "en-dk", + "en-hk", + "en-in", + "en-id", + "en-ie", + "en-jm", + "en-ja", + "en-my", + "en-nz", + "en-no", + "en-ph", + "en-pr", + "en-sg", + "en-za", + "en-se", + "en-gb", + "en-us", + "en-zw", + "eo", + "et", + "ee", + "fo", + "fj", + "fil", + "fi", + "vls-be", + "fra-de", + "fr", + "fr-be", + "fr-ca", + "fr-lu", + "fr-qc", + "fr-ch", + "fy-nl", + "fur-it", + "ff", + "gaa", + "gl", + "ka", + "de", + "de-at", + "de-be", + "de-li", + "de-lu", + "de-ch", + "got", + "el", + "el-cy", + "kl", + "gn", + "gu-in", + "ht", + "ha", + "haw", + "he", + "hz", + "hil", + "hi", + "ho", + "hmn", + "hu", + "is", + "ido", + "ig", + "ilo", + "id", + "iu", + "ga-ie", + "it", + "it-ch", + "ja", + "jv", + "quc", + "kab", + "kn", + "pam", + "ks", + "ks-pk", + "csb", + "kk", + "km", + "rw", + "tlh-aa", + "kv", + "kg", + "kok", + "ko", + "ku", + "kmr", + "kj", + "ky", + "lol", + "lo", + "la-la", + "lv", + "lij", + "li", + "ln", + "lt", + "jbo", + "nds", + "dsb-de", + "lg", + "luy", + "lb", + "mk", + "mai", + "mg", + "ms", + "ms-bn", + "ml-in", + "mt", + "gv", + "mi", + "arn", + "mr", + "mh", + "moh", + "mn", + "sr-cyrl-me", + "me", + "mos", + "na", + "ng", + "ne-np", + "ne-in", + "pcm", + "se", + "nso", + "no", + "nb", + "nn-no", + "oc", + "or", + "oj", + "om", + "os", + "pi", + "pap", + "ps", + "fa", + "en-pt", + "pl", + "pt-pt", + "pt-br", + "pa-in", + "pa-pk", + "qu", + "qya-aa", + "ro", + "rm-ch", + "rn", + "ru", + "ru-by", + "ru-md", + "ry-ua", + "ru-ua", + "sah", + "sg", + "sa", + "sat", + "sc", + "sco", + "gd", + "sr", + "sr-cs", + "sh", + "crs", + "sn", + "ii", + "sd", + "si-lk", + "sk", + "sl", + "so", + "son", + "ckb", + "nr", + "sma", + "st", + "es-es", + "es-em", + "es-ar", + "es-bo", + "es-cl", + "es-co", + "es-cr", + "es-do", + "es-ec", + "es-sv", + "es-gt", + "es-hn", + "es-419", + "es-mx", + "es-ni", + "es-pa", + "es-py", + "es-pe", + "es-pr", + "es-us", + "es-uy", + "es-ve", + "su", + "sw", + "sw-ke", + "sw-tz", + "ss", + "sv-se", + "sv-fi", + "syc", + "tl", + "ty", + "tg", + "tzl", + "ta", + "tt-ru", + "te", + "kdh", + "th", + "bo-bt", + "ti", + "ts", + "tn", + "tr", + "tr-cy", + "tk", + "uk", + "hsb-de", + "ur-in", + "ur-pk", + "ug", + "uz", + "val-es", + "ve", + "vec", + "vi", + "wa", + "cy", + "wo", + "xh", + "yi", + "yo", + "zea", + "zu" + ); + } +} diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index cc17bf0d..5618c086 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -622,6 +622,7 @@ error.config.params_dest='dest' must be specified with both 'source' and 'transl error.config.enum_class_exception=Configuration file contains unexpected '%s' value type. The expected value is: %s error.config.enum_wrong_value=Configuration file contains unexpected '%s' value. The expected values are: %s error.config.pseudo_localization_length_correction_out_of_bounds=Acceptable value for 'length_correction' is from -50 to 100 +error.config.languages_mapping=The mapping format is the following: crowdin_language_code: code_you_use. Check the full list of Crowdin language codes that can be used for mapping: https://developer.crowdin.com/language-codes. error.init.project_id_is_not_number='%s' is not a number! (Enter the correct value or leave the field empty) error.init.skip_project_validation=Skipping project checking due to lack of parameters error.init.path_not_exist=Path '%s' doesn't exist diff --git a/src/test/java/com/crowdin/cli/utils/language/LangCodesUtilTest.java b/src/test/java/com/crowdin/cli/utils/language/LangCodesUtilTest.java new file mode 100644 index 00000000..33fe2283 --- /dev/null +++ b/src/test/java/com/crowdin/cli/utils/language/LangCodesUtilTest.java @@ -0,0 +1,40 @@ +package com.crowdin.cli.utils.language; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LangCodesUtilTest { + + @Test + public void testValidCode() { + assertTrue(LangCodesUtil.isValidCode("ach")); + assertTrue(LangCodesUtil.isValidCode("af")); + assertTrue(LangCodesUtil.isValidCode("ar-eg")); + } + @Test + public void testValidCodeCaseInsensitive() { + assertTrue(LangCodesUtil.isValidCode("ACH")); + assertTrue(LangCodesUtil.isValidCode("Af")); + assertTrue(LangCodesUtil.isValidCode("AR-EG")); + } + + @Test + public void testInvalidCode() { + assertFalse(LangCodesUtil.isValidCode("invalid")); + assertFalse(LangCodesUtil.isValidCode("english")); + assertFalse(LangCodesUtil.isValidCode("esus")); + } + + @Test + public void testNullCode() { + assertThrows(NullPointerException.class, () -> { + LangCodesUtil.isValidCode(null); + }); + } + + @Test + public void testEmptyCode() { + assertFalse(LangCodesUtil.isValidCode("")); + } +}