Skip to content

Commit

Permalink
fix: correction an escape of query text
Browse files Browse the repository at this point in the history
- Use Jackson serializer to make request json

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Dec 16, 2023
1 parent ab060d0 commit 1ed3a5f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@

import org.omegat.util.HttpConnectionUtils;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -56,10 +58,9 @@ protected String requestTranslate(String langFrom, String langTo, String text) t
Map<String, String> p = new TreeMap<>();
p.put("Ocp-Apim-Subscription-Key", parent.getKey());
p.put("Ocp-Apim-Subscription-Region", parent.getRegion());
StringBuilder urlBuilder = new StringBuilder(urlTranslate);
urlBuilder.append("&from=").append(langFrom).append("&to=").append(langTo);
String json = "[{\"Text\": \"" + text + "\"}]";
String res = HttpConnectionUtils.postJSON(urlBuilder.toString(), json, p);
String url = urlTranslate + "&from=" + langFrom + "&to=" + langTo;
String json = createJsonRequest(text);
String res = HttpConnectionUtils.postJSON(url, json, p);
JsonNode root = mapper.readTree(res);
JsonNode translations = root.get(0).get("translations");
if (translations == null) {
Expand All @@ -79,4 +80,13 @@ protected String requestTranslate(String langFrom, String langTo, String text) t
public void setUrl(String url) {
urlTranslate = url;
}

/**
* Create Watson request and return as json string.
*/
protected String createJsonRequest(String trText) throws JsonProcessingException {
Map<String, Object> param = new TreeMap<>();
param.put("text", trText);
return new ObjectMapper().writeValueAsString(Collections.singletonList(param));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* OmegaT - Computer Assisted Translation (CAT) tool
* with fuzzy matching, translation memory, keyword search,
* glossaries, and translation leveraging into updated projects.
*
* Copyright (C) 2023 miurahr
* Home page: https://www.omegat.org/
* Support center: https://omegat.org/support
*
* This file is part of OmegaT.
*
* OmegaT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OmegaT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.omegat.connectors.machinetranslators.azure;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestAzureTranslatorV3 {

@Test
public void testCreateJsonRequest() throws JsonProcessingException {
MicrosoftTranslatorAzure azure = new TestMicrosoftTranslatorAzure.MicrosoftTranslatorAzureMock();
AzureTranslatorV3 translator = new AzureTranslatorV3(azure);
String result = translator.createJsonRequest("\"foo\" boo");
String expected = "[{\"text\":\"\\\"foo\\\" boo\"}]";
Assertions.assertEquals(expected, result);
}
}

0 comments on commit 1ed3a5f

Please sign in to comment.