mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Fix a few bugs from last NIO2 commit in resource translation
This commit is contained in:
parent
dddc1c4e19
commit
783bba6b48
|
@ -88,8 +88,9 @@ public final class HtmlAssetTranslator {
|
||||||
DirectoryStream.Filter<Path> fileFilter = new DirectoryStream.Filter<Path>() {
|
DirectoryStream.Filter<Path> fileFilter = new DirectoryStream.Filter<Path>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(Path entry) {
|
public boolean accept(Path entry) {
|
||||||
|
String fileName = entry.getFileName().toString();
|
||||||
return Files.isDirectory(entry) && !Files.isSymbolicLink(entry) &&
|
return Files.isDirectory(entry) && !Files.isSymbolicLink(entry) &&
|
||||||
entry.getFileName().startsWith("html-") && !"html-en".equals(entry.getFileName().toString());
|
fileName.startsWith("html-") && !"html-en".equals(fileName);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
try (DirectoryStream<Path> dirs = Files.newDirectoryStream(assetsDir, fileFilter)) {
|
try (DirectoryStream<Path> dirs = Files.newDirectoryStream(assetsDir, fileFilter)) {
|
||||||
|
|
|
@ -16,9 +16,12 @@
|
||||||
|
|
||||||
package com.google.zxing;
|
package com.google.zxing;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.net.URLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.DirectoryStream;
|
import java.nio.file.DirectoryStream;
|
||||||
|
@ -180,7 +183,7 @@ public final class StringsResourceTranslator {
|
||||||
"https://www.googleapis.com/language/translate/v2?key=" + API_KEY + "&q=" +
|
"https://www.googleapis.com/language/translate/v2?key=" + API_KEY + "&q=" +
|
||||||
URLEncoder.encode(english, "UTF-8") +
|
URLEncoder.encode(english, "UTF-8") +
|
||||||
"&source=en&target=" + language);
|
"&source=en&target=" + language);
|
||||||
CharSequence translateResult = new String(Files.readAllBytes(Paths.get(translateURI)), StandardCharsets.UTF_8);
|
CharSequence translateResult = fetch(translateURI);
|
||||||
Matcher m = TRANSLATE_RESPONSE_PATTERN.matcher(translateResult);
|
Matcher m = TRANSLATE_RESPONSE_PATTERN.matcher(translateResult);
|
||||||
if (!m.find()) {
|
if (!m.find()) {
|
||||||
System.err.println("No translate result");
|
System.err.println("No translate result");
|
||||||
|
@ -199,6 +202,20 @@ public final class StringsResourceTranslator {
|
||||||
return translation;
|
return translation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CharSequence fetch(URI translateURI) throws IOException {
|
||||||
|
URLConnection connection = translateURI.toURL().openConnection();
|
||||||
|
connection.connect();
|
||||||
|
StringBuilder translateResult = new StringBuilder(200);
|
||||||
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||||
|
char[] buffer = new char[8192];
|
||||||
|
int charsRead;
|
||||||
|
while ((charsRead = in.read(buffer)) > 0) {
|
||||||
|
translateResult.append(buffer, 0, charsRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return translateResult;
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String,String> readLines(Path file) throws IOException {
|
private static Map<String,String> readLines(Path file) throws IOException {
|
||||||
if (Files.exists(file)) {
|
if (Files.exists(file)) {
|
||||||
Map<String,String> entries = new TreeMap<>();
|
Map<String,String> entries = new TreeMap<>();
|
||||||
|
|
Loading…
Reference in a new issue