Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validate language code #810

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading