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

Microsoft Translator API Regional Source Support #566

Merged
merged 3 commits into from
Jun 4, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MTConfig {
public static String KEY;
public static String TRANSLATED_MAX;
public static String TRANSLATECOUNT;
public static String REGION;

private MTConfig() {}

Expand Down Expand Up @@ -67,4 +68,8 @@ public synchronized static void setCurrentMills(long c) {
public static boolean isTimeOut() {
return (System.currentTimeMillis() - getCurrentMills()) > hr24;
}

public static void setREGION(String rEGION) {
REGION = rEGION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ public class MTConfigContent {
@Value("${mt.translatedCount:20}")
private String translatedCount;

@Value("${mt.region:}")
private String region;

@PostConstruct
public void initConfig() {
MTConfig.setMTSERVER(this.getMtServer());
MTConfig.setKEY(this.getKey());
MTConfig.setTRANSLATED_MAX(this.getTranslatedMax());
MTConfig.setTRANSLATECOUNT(this.getTranslatedCount());
MTConfig.setREGION(this.region);
}

public String getMtServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,7 +55,10 @@ public List<String> translateArray(String fromLang, String toLang,
logger.info(MTConfig.MTSERVER);
Map<String, String> headers = new HashMap<String, String>();
headers.put("Ocp-Apim-Subscription-Key", MTConfig.KEY);
String response = HTTPRequester.postData(requestJson, urlStr,
if(StringUtils.isNotBlank(MTConfig.REGION)) {
headers.put("Ocp-Apim-Subscription-Region", MTConfig.REGION);
}
String response = HTTPRequester.postData(requestJson, urlStr,
"application/json", "POST", headers);
if(response == null || response.equalsIgnoreCase("")) {
throw new MTException("Request failed: respond empty from MT server!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ swagger-ui.enable=true

#microsoftstore mt config
mt.server=https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=
#mt.region=
#mt.key=
mt.translatedMax=500
mt.translatedCount=20
Expand Down