Skip to content

Commit

Permalink
feat: validate language code (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 committed Jun 11, 2024
1 parent a560618 commit 84f8410
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Language> languages = projectClient.listSupportedLanguages();

if (languages != null) {
Set<String> 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) {
Expand Down
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

0 comments on commit 84f8410

Please sign in to comment.