Skip to content

Commit

Permalink
wip phrase connector
Browse files Browse the repository at this point in the history
  • Loading branch information
aurambaj committed Jun 10, 2024
1 parent 834ae24 commit 26df13a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public AndroidStringDocument readFromTextUnits(List<TextUnitDTO> textUnits, bool

if (OTHER.name().equalsIgnoreCase(textUnit.getPluralForm())) {
String name = pluralNameParser.getPrefix(textUnit.getName(), pluralSeparator);
builder.setName((addTextUnitIdInName ? textUnit.getTmTextUnitId() + "#@#" : "") + textUnit.getAssetPath() + assetDelimiter + name);
builder.setName(
(addTextUnitIdInName ? textUnit.getTmTextUnitId() + "#@#" : "")
+ textUnit.getAssetPath()
+ assetDelimiter
+ name);
builder.setComment(textUnit.getComment());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ public PollableFuture<Void> pull(
String schedulerName,
PollableTask currentTask) {

// where to get the list of locale from - doing only FR now
Set<RepositoryLocale> repositoryLocales = repository.getRepositoryLocales();
Set<RepositoryLocale> repositoryLocalesWithoutRootLocale =
repositoryService.getRepositoryLocalesWithoutRootLocale(repository);

for (RepositoryLocale repositoryLocale : repositoryLocales) {
for (RepositoryLocale repositoryLocale : repositoryLocalesWithoutRootLocale) {
String localeTag = repositoryLocale.getLocale().getBcp47Tag();
logger.info("Downloading locale: {} from Phrase", localeTag);
String fileContent = phraseClient.localeDownload(projectId, localeTag, "xml");

AndroidStringDocumentMapper mapper =
new AndroidStringDocumentMapper(pluralSeparator, null, localeTag, repository.getName());
new AndroidStringDocumentMapper(pluralSeparator, null, localeTag, repository.getName(), true);

List<TextUnitDTO> textUnitDTOS =
mapper.mapToTextUnits(AndroidStringDocumentReader.fromText(fileContent));
Expand Down Expand Up @@ -204,7 +205,7 @@ private static String getFileContent(
String pluralSeparator, List<TextUnitDTO> textUnitDTOS, boolean useSource) {

AndroidStringDocumentMapper androidStringDocumentMapper =
new AndroidStringDocumentMapper(pluralSeparator, null);
new AndroidStringDocumentMapper(pluralSeparator, null, null, null, true);

AndroidStringDocument androidStringDocument =
androidStringDocumentMapper.readFromTextUnits(textUnitDTOS, useSource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.box.l10n.mojito.service.thirdparty.phrase;

import static com.box.l10n.mojito.io.Files.createDirectories;
import static com.box.l10n.mojito.io.Files.createTempDirectory;
import static com.box.l10n.mojito.io.Files.write;

Expand Down Expand Up @@ -51,7 +52,8 @@ Upload waitForUploadToFinish(String projectId, String uploadId) {
logger.debug("Waiting for upload to finish: {}", uploadId);

Upload upload = uploadsApi.uploadShow(projectId, uploadId, null, null);
logger.debug("Upload info, first fetch: {}", new ObjectMapper().writeValueAsStringUnchecked(upload));
logger.debug(
"Upload info, first fetch: {}", new ObjectMapper().writeValueAsStringUnchecked(upload));

while (!ImmutableSet.of("success", "error").contains(upload.getState())) {
try {
Expand Down Expand Up @@ -92,9 +94,16 @@ public String uploadCreateFile(
try {
tmpWorkingDirectory = createTempDirectory("phrase-integration");

if (tmpWorkingDirectory.toFile().exists()) {
logger.info("Created temporary working directory: {}", tmpWorkingDirectory);
}

Path fileToUpload = tmpWorkingDirectory.resolve(fileName);
logger.debug("Create and upload file to Phrase: {}", fileToUpload);

logger.info("Create and upload file to Phrase: {}", fileToUpload);
createDirectories(fileToUpload.getParent());
write(fileToUpload, fileContent);

Upload upload =
uploadsApi.uploadCreate(
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ List<TextUnitForBatchMatcherImport> skipInvalidAndConvertToTextUnitForBatchImpor
textUnitForBatchImport.setRepository(
repositoriesCache.getUnchecked(t.getRepositoryName()));
if (textUnitForBatchImport.getRepository() != null) {
logger.info("ja- asset path: {}", t.getAssetPath());
textUnitForBatchImport.setAsset(
assetsCache.getUnchecked(
new SimpleEntry<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,29 @@ public void testReadFromSourceTextUnitsWithPluralsAndWithTmTextUnitIdInName() {
textUnits.add(sourceTextUnitDTO(123L, "name0", "content0", "comment0", "my/path0", null, null));

textUnits.add(
sourceTextUnitDTO(
100L, "name1 _other", "content1_zero", "comment1", "my/path1", "zero", "name1_other"));
sourceTextUnitDTO(
100L, "name1 _other", "content1_zero", "comment1", "my/path1", "zero", "name1_other"));
textUnits.add(
sourceTextUnitDTO(
101L, "name1 _other", "content1_one", "comment1", "my/path1", "one", "name1_other"));
sourceTextUnitDTO(
101L, "name1 _other", "content1_one", "comment1", "my/path1", "one", "name1_other"));
textUnits.add(
sourceTextUnitDTO(
102L, "name1 _other", "content1_two", "comment1", "my/path1", "two", "name1_other"));
sourceTextUnitDTO(
102L, "name1 _other", "content1_two", "comment1", "my/path1", "two", "name1_other"));
textUnits.add(
sourceTextUnitDTO(
103L, "name1 _other", "content1_few", "comment1", "my/path1", "few", "name1_other"));
sourceTextUnitDTO(
103L, "name1 _other", "content1_few", "comment1", "my/path1", "few", "name1_other"));
textUnits.add(
sourceTextUnitDTO(
104L, "name1 _other", "content1_many", "comment1", "my/path1", "many", "name1_other"));
sourceTextUnitDTO(
104L, "name1 _other", "content1_many", "comment1", "my/path1", "many", "name1_other"));
textUnits.add(
sourceTextUnitDTO(
105L,
"name1 _other",
"content1_other",
"comment1",
"my/path1",
"other",
"name1_other"));
sourceTextUnitDTO(
105L,
"name1 _other",
"content1_other",
"comment1",
"my/path1",
"other",
"name1_other"));

document = mapper.readFromSourceTextUnits(textUnits);

Expand Down Expand Up @@ -216,8 +216,6 @@ public void testReadFromSourceTextUnitsWithPluralsAndWithTmTextUnitIdInName() {
assertThat(plural.getItems().get(OTHER).getContent()).isEqualTo("content1_other");
}



@Test
public void testReadFromSourceTextUnitsWithDuplicatePlurals() {
mapper = new AndroidStringDocumentMapper(" _", assetDelimiter);
Expand Down Expand Up @@ -751,7 +749,7 @@ public void testReadingTextUnitsFromFile() throws Exception {

assertThat(textUnits)
.filteredOn(tu -> tu.getName().equalsIgnoreCase("some_string"))
.extracting("name", "target", "comment")
.extracting("name", "target", "comment")
.containsOnly(tuple("some_string", "Dela...", "Some string"));

assertThat(textUnits)
Expand Down Expand Up @@ -825,13 +823,13 @@ public void testAddTextUnitDTOAttributesTextUnitIdAndAssetPathAndName() {

textUnitDTO.setName("156151#@#asset_path#@#name_part1");
assertThat(mapper.addTextUnitDTOAttributes(textUnitDTO))
.extracting(TextUnitDTO::getTmTextUnitId, TextUnitDTO::getAssetPath, TextUnitDTO::getName)
.containsExactly(156151L,"asset_path", "name_part1");
.extracting(TextUnitDTO::getTmTextUnitId, TextUnitDTO::getAssetPath, TextUnitDTO::getName)
.containsExactly(156151L, "asset_path", "name_part1");

textUnitDTO.setName("156152#@#asset_path#@#name_part1#@#name_part2");
assertThat(mapper.addTextUnitDTOAttributes(textUnitDTO))
.extracting(TextUnitDTO::getTmTextUnitId, TextUnitDTO::getAssetPath, TextUnitDTO::getName)
.containsExactly(156152L,"asset_path", "name_part1#@#name_part2");
.extracting(TextUnitDTO::getTmTextUnitId, TextUnitDTO::getAssetPath, TextUnitDTO::getName)
.containsExactly(156152L, "asset_path", "name_part1#@#name_part2");
}

private TextUnitDTO sourceTextUnitDTO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,8 @@ public ThirdPartyServiceTestData init() throws Exception {
logger.debug("Finished init of ThirdPartyServiceTestData");
return this;
}

public String getPluralSeparator() {
return "_";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.box.l10n.mojito.service.thirdparty;

import com.box.l10n.mojito.entity.Repository;
import com.box.l10n.mojito.service.assetExtraction.ServiceTestBase;
import com.box.l10n.mojito.test.TestIdWatcher;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class ThirdPartyTMSPhraseTest extends ServiceTestBase {

static Logger logger = LoggerFactory.getLogger(ThirdPartyTMSPhraseTest.class);

@Autowired(required = false)
ThirdPartyTMSPhrase thirdPartyTMSPhrase;

@Value("${test.phrase-client.projectId}")
String testProjectId;

@Rule public TestIdWatcher testIdWatcher = new TestIdWatcher();

@Test
public void testBasics() {
Assume.assumeNotNull(thirdPartyTMSPhrase);
Assume.assumeNotNull(testProjectId);

ThirdPartyServiceTestData thirdPartyServiceTestData =
new ThirdPartyServiceTestData(testIdWatcher);

Repository repository = thirdPartyServiceTestData.repository;

thirdPartyTMSPhrase.push(
repository,
testProjectId,
thirdPartyServiceTestData.getPluralSeparator(),
null,
null,
null);
thirdPartyTMSPhrase.pull(
repository,
testProjectId,
thirdPartyServiceTestData.getPluralSeparator(),
null,
null,
null,
null,
null,
null);

// List<ThirdPartyTextUnit> thirdPartyTextUnits =
// thirdPartyTMSPhrase.getThirdPartyTextUnits(repository, testProjectId, null);
// logger.info("Get")
// thirdPartyTextUnits.stream().forEach(t -> logger.info("third party text unit: {}",
// t));

}
}

0 comments on commit 26df13a

Please sign in to comment.