Skip to content

Commit

Permalink
Merge pull request #566 from deeDude/master
Browse files Browse the repository at this point in the history
Microsoft Translator API Regional Source Support
  • Loading branch information
surite authored Jun 4, 2020
2 parents 0bbb970 + bbb17b4 commit efad5f5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
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

0 comments on commit efad5f5

Please sign in to comment.