diff --git a/src/main/java/com/crowdin/cli/properties/FileBean.java b/src/main/java/com/crowdin/cli/properties/FileBean.java index fd9db8c6..eec195e6 100755 --- a/src/main/java/com/crowdin/cli/properties/FileBean.java +++ b/src/main/java/com/crowdin/cli/properties/FileBean.java @@ -9,11 +9,9 @@ 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 { @@ -173,18 +171,6 @@ 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/properties/PropertiesWithFiles.java b/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java index 62f64fd4..80b34ca7 100644 --- a/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java +++ b/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java @@ -1,12 +1,13 @@ package com.crowdin.cli.properties; +import com.crowdin.cli.client.Clients; +import com.crowdin.cli.client.ProjectClient; +import com.crowdin.client.languages.model.Language; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.lang3.StringUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import static com.crowdin.cli.BaseCli.IGNORE_HIDDEN_FILES_PATTERN; @@ -77,6 +78,21 @@ public PropertiesBuilder.Messages checkProperties(PropertiesWithFiles props, Che if (StringUtils.isNotEmpty(fileBean.getDest()) && !props.getPreserveHierarchy()) { messages.addError(RESOURCE_BUNDLE.getString("error.dest_and_preserve_hierarchy")); } + if (fileBean.getLanguagesMapping() != null) { + ProjectClient projectClient = Clients.getProjectClient(props.getApiToken(), props.getBaseUrl(), Long.parseLong(props.getProjectId())); + List languages = projectClient.listSupportedLanguages(); + + if (languages != null) { + Set langCodes = languages.stream().map(lang -> lang.getId().toLowerCase()).collect(Collectors.toSet()); + + boolean hasInvalidCode = fileBean.getLanguagesMapping().values().stream() + .flatMap(innerMap -> innerMap.keySet().stream()) + .anyMatch(langCode -> !langCodes.contains(langCode.toLowerCase())); + if (hasInvalidCode) { + messages.addError(RESOURCE_BUNDLE.getString("error.config.languages_mapping")); + } + } + } } } if (props.getPseudoLocalization() != null) { diff --git a/src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java b/src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java deleted file mode 100644 index f80b7f39..00000000 --- a/src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java +++ /dev/null @@ -1,330 +0,0 @@ -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/test/java/com/crowdin/cli/utils/language/LangCodesUtilTest.java b/src/test/java/com/crowdin/cli/utils/language/LangCodesUtilTest.java deleted file mode 100644 index 33fe2283..00000000 --- a/src/test/java/com/crowdin/cli/utils/language/LangCodesUtilTest.java +++ /dev/null @@ -1,40 +0,0 @@ -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("")); - } -}