From 72cbf0e7232067c9361dbfacd8526f7cd719920c Mon Sep 17 00:00:00 2001 From: Jean Aurambault Date: Thu, 13 Jun 2024 15:57:35 -0700 Subject: [PATCH] Fix up LocaleMappingHelper --- .../l10n/mojito/cli/command/PullCommand.java | 24 +++++--- .../cli/command/PullCommandParallel.java | 59 +++---------------- .../box/l10n/mojito/LocaleMappingHelper.java | 6 +- 3 files changed, 26 insertions(+), 63 deletions(-) diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommand.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommand.java index c5c66e3dc0..b6ce622d74 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommand.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommand.java @@ -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; @@ -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 localeMappings; /** Map of locale and the boolean value for fully translated status of the locale */ @@ -292,7 +295,14 @@ void generateLocalizedFiles( throws CommandException { logger.debug("Generate localized files"); + Map outputToRepo = getMapOutputTagToRepositoryLocale(); + for (Map.Entry e : outputToRepo.entrySet()) { + generateLocalizedFile(repository, sourceFileMatch, filterOptions, e.getKey(), e.getValue()); + } + } + + Map getMapOutputTagToRepositoryLocale() { Map outputToRepo; if (LocaleMappingType.MAP_ONLY.equals(localeMappingTypeParam)) { @@ -315,17 +325,13 @@ void generateLocalizedFiles( } } } - - for (Map.Entry e : outputToRepo.entrySet()) { - generateLocalizedFile(repository, sourceFileMatch, filterOptions, e.getKey(), e.getValue()); - } + return outputToRepo; } private Map 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) { diff --git a/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommandParallel.java b/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommandParallel.java index d635385fa9..9879b44b6e 100644 --- a/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommandParallel.java +++ b/cli/src/main/java/com/box/l10n/mojito/cli/command/PullCommandParallel.java @@ -83,19 +83,13 @@ public void pull() throws CommandException { private void sendContentForLocalizedGeneration( FileMatch sourceFileMatch, List filterOptions) { - if (localeMappingParam != null) { - pollableTaskIdToFileMatchMap.put( - generateLocalizedFilesWithLocaleMappingParallel( - repository, sourceFileMatch, filterOptions) - .getId(), - sourceFileMatch); - } else { - pollableTaskIdToFileMatchMap.put( - generateLocalizedFilesWithoutLocaleMappingParallel( - repository, sourceFileMatch, filterOptions) - .getId(), - sourceFileMatch); - } + + List repositoryLocales = + getMapOutputTagToRepositoryLocale().values().stream().distinct().toList(); + pollableTaskIdToFileMatchMap.put( + generateLocalizedFiles(repository, sourceFileMatch, filterOptions, repositoryLocales) + .getId(), + sourceFileMatch); } private void pollForLocalizedFiles() { @@ -119,31 +113,6 @@ private void pollForLocalizedFiles() { }); } - private PollableTask generateLocalizedFilesWithLocaleMappingParallel( - Repository repository, FileMatch sourceFileMatch, List filterOptions) - throws CommandException { - - List 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 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 { @@ -174,19 +143,6 @@ private synchronized void printFileGeneratedToConsole( .println(); } - void generateLocalizedFiles( - Repository repository, FileMatch sourceFileMatch, List 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, @@ -223,6 +179,7 @@ private PollableTask generateLocalizedFiles( private Map> getRepoLocaleToOutputTagsMap() { Map> localeIdToOutputTagsMap = new HashMap<>(); + // TODO(ja) need to change this becased on the type if (localeMappings != null) { for (Map.Entry mapping : localeMappings.entrySet()) { String outputBcp47tag = mapping.getKey(); diff --git a/common/src/main/java/com/box/l10n/mojito/LocaleMappingHelper.java b/common/src/main/java/com/box/l10n/mojito/LocaleMappingHelper.java index 8601ea642c..08b3507c18 100644 --- a/common/src/main/java/com/box/l10n/mojito/LocaleMappingHelper.java +++ b/common/src/main/java/com/box/l10n/mojito/LocaleMappingHelper.java @@ -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 getLocaleMapping(String localeMapppingParam) { @@ -28,7 +28,7 @@ public Map 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 getInverseLocaleMapping(String localeMapppingParam) {