Skip to content

Commit

Permalink
Fix up LocaleMappingHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
aurambaj committed Jun 13, 2024
1 parent a00009e commit 72cbf0e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 63 deletions.
24 changes: 15 additions & 9 deletions cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.box.l10n.mojito.rest.entity.RepositoryLocaleStatistic;
import com.box.l10n.mojito.rest.entity.RepositoryStatistic;
import java.nio.file.Path;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -201,7 +201,10 @@ enum LocaleMappingType {

CommandDirectories commandDirectories;

/** Contains a map of locale for generating localized file a locales defined in the repository. */
/**
* Contains a map of locale for generating localized file. Key: file output tag, value: the tag in
* the repository
*/
Map<String, String> localeMappings;

/** Map of locale and the boolean value for fully translated status of the locale */
Expand Down Expand Up @@ -292,7 +295,14 @@ void generateLocalizedFiles(
throws CommandException {

logger.debug("Generate localized files");
Map<String, RepositoryLocale> outputToRepo = getMapOutputTagToRepositoryLocale();

for (Map.Entry<String, RepositoryLocale> e : outputToRepo.entrySet()) {
generateLocalizedFile(repository, sourceFileMatch, filterOptions, e.getKey(), e.getValue());
}
}

Map<String, RepositoryLocale> getMapOutputTagToRepositoryLocale() {
Map<String, RepositoryLocale> outputToRepo;

if (LocaleMappingType.MAP_ONLY.equals(localeMappingTypeParam)) {
Expand All @@ -315,17 +325,13 @@ void generateLocalizedFiles(
}
}
}

for (Map.Entry<String, RepositoryLocale> e : outputToRepo.entrySet()) {
generateLocalizedFile(repository, sourceFileMatch, filterOptions, e.getKey(), e.getValue());
}
return outputToRepo;
}

private Map<String, RepositoryLocale> getLocaleMapFromLocaleMappings() {
return localeMappings.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getRepositoryLocaleByTag(e.getValue())))
.collect(
Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
.map(e -> new SimpleEntry<>(e.getKey(), getRepositoryLocaleByTag(e.getValue())))
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
}

private RepositoryLocale getRepositoryLocaleByTag(String tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ public void pull() throws CommandException {

private void sendContentForLocalizedGeneration(
FileMatch sourceFileMatch, List<String> filterOptions) {
if (localeMappingParam != null) {
pollableTaskIdToFileMatchMap.put(
generateLocalizedFilesWithLocaleMappingParallel(
repository, sourceFileMatch, filterOptions)
.getId(),
sourceFileMatch);
} else {
pollableTaskIdToFileMatchMap.put(
generateLocalizedFilesWithoutLocaleMappingParallel(
repository, sourceFileMatch, filterOptions)
.getId(),
sourceFileMatch);
}

List<RepositoryLocale> repositoryLocales =
getMapOutputTagToRepositoryLocale().values().stream().distinct().toList();
pollableTaskIdToFileMatchMap.put(
generateLocalizedFiles(repository, sourceFileMatch, filterOptions, repositoryLocales)
.getId(),
sourceFileMatch);
}

private void pollForLocalizedFiles() {
Expand All @@ -119,31 +113,6 @@ private void pollForLocalizedFiles() {
});
}

private PollableTask generateLocalizedFilesWithLocaleMappingParallel(
Repository repository, FileMatch sourceFileMatch, List<String> filterOptions)
throws CommandException {

List<RepositoryLocale> repositoryLocales =
localeMappings.entrySet().stream()
.map(entry -> getRepositoryLocaleForOutputBcp47Tag(entry.getKey()))
.distinct()
.collect(Collectors.toList());
return generateLocalizedFiles(repository, sourceFileMatch, filterOptions, repositoryLocales);
}

private PollableTask generateLocalizedFilesWithoutLocaleMappingParallel(
Repository repository, FileMatch sourceFileMatch, List<String> filterOptions)
throws CommandException {

logger.debug("Generate localized files (without locale mapping)");

return generateLocalizedFiles(
repository,
sourceFileMatch,
filterOptions,
Lists.newArrayList(repositoryLocalesWithoutRootLocale.values()));
}

void writeLocalizedAssetToTargetDirectory(
LocalizedAssetBody localizedAsset, FileMatch sourceFileMatch) throws CommandException {

Expand Down Expand Up @@ -174,19 +143,6 @@ private synchronized void printFileGeneratedToConsole(
.println();
}

void generateLocalizedFiles(
Repository repository, FileMatch sourceFileMatch, List<String> filterOptions)
throws CommandException {

logger.debug("Generate localized files (without locale mapping)");

generateLocalizedFiles(
repository,
sourceFileMatch,
filterOptions,
Lists.newArrayList(repositoryLocalesWithoutRootLocale.values()));
}

private PollableTask generateLocalizedFiles(
Repository repository,
FileMatch sourceFileMatch,
Expand Down Expand Up @@ -223,6 +179,7 @@ private PollableTask generateLocalizedFiles(
private Map<RepositoryLocale, List<String>> getRepoLocaleToOutputTagsMap() {
Map<RepositoryLocale, List<String>> localeIdToOutputTagsMap = new HashMap<>();

// TODO(ja) need to change this becased on the type
if (localeMappings != null) {
for (Map.Entry<String, String> mapping : localeMappings.entrySet()) {
String outputBcp47tag = mapping.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
public class LocaleMappingHelper {

/**
* Gets the locale mapping given the locale mapping param
* Gets the locale mapping given the locale mapping param.
*
* @param localeMapppingParam locale mapping param coming from the CLI
* @return A map containing the locale mapping
* @return A map containing the locale mapping (key: output tag, value: the tag in the repository)
*/
public Map<String, String> getLocaleMapping(String localeMapppingParam) {

Expand All @@ -28,7 +28,7 @@ public Map<String, String> getLocaleMapping(String localeMapppingParam) {
* Gets the inverse locale mapping given the locale mapping param
*
* @param localeMapppingParam locale mapping param coming from the CLI
* @return A map containing the inverse locale mapping
* @return A map containing the inverse locale mapping (key: the tag in the repository, value: file output tag)
*/
public Map<String, String> getInverseLocaleMapping(String localeMapppingParam) {

Expand Down

0 comments on commit 72cbf0e

Please sign in to comment.