Skip to content

Commit

Permalink
feat: validate language code
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 committed May 27, 2024
1 parent df576ef commit febb955
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/crowdin/cli/properties/FileBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -171,6 +173,18 @@ public List<String> checkProperties(FileBean bean) {
}
}

Map<String, Map<String, String>> languagesMapping = bean.getLanguagesMapping();

if (languagesMapping != null) {
Optional<String> 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"));
Expand Down
330 changes: 330 additions & 0 deletions src/main/java/com/crowdin/cli/utils/language/LangCodesUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
package com.crowdin.cli.utils.language;

import java.util.Set;

public class LangCodesUtil {
private static final Set<String> 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"
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit febb955

Please sign in to comment.