@@ -9,6 +9,7 @@ import net.minecraft.util.random.Weight
99import net.minecraft.util.random.WeightedEntry
1010import xyz.bluspring.unitytranslate.Language
1111import xyz.bluspring.unitytranslate.UnityTranslate
12+ import xyz.bluspring.unitytranslate.util.HttpHelper
1213import java.io.OutputStreamWriter
1314import java.net.HttpURLConnection
1415import java.net.URL
@@ -72,101 +73,38 @@ open class LibreTranslateInstance(val url: String, private var weight: Int, val
7273 }
7374
7475 open fun detectLanguage (text : String ): Language ? {
75- val url = URL (" $url /detect" )
76- val httpConn = url.openConnection() as HttpURLConnection
77- httpConn.requestMethod = " POST"
78- httpConn.setRequestProperty(" accept" , " application/json" )
79- httpConn.setRequestProperty(" Content-Type" , " application/json" )
80- httpConn.doOutput = true
81-
82- val writer = OutputStreamWriter (httpConn.outputStream, " UTF-8" )
83-
84- writer.write(JsonObject ().apply {
76+ val detected = HttpHelper .post(" $url /detect" , JsonObject ().apply {
8577 addProperty(" q" , text)
8678
8779 if (authKey?.isNotBlank() == true )
8880 addProperty(" api_key" , authKey)
89- }.toString())
90-
91- writer.flush()
92- writer.close()
93- httpConn.outputStream.close()
94- if (httpConn.responseCode / 100 != 2 ) {
95- throw Exception (" Failed to load ${this .url} /detect (code ${httpConn.responseCode} )" )
96- } else {
97- val responseStream = httpConn.inputStream
98- val s = Scanner (responseStream, " UTF-8" ).useDelimiter(" \\ A" )
99- val response = if (s.hasNext()) s.next() else " "
100-
101- val detected = JsonParser .parseString(response).asJsonArray.sortedByDescending { it.asJsonObject.get(" confidence" ).asDouble }
102- val langCode = detected.firstOrNull()?.asJsonObject?.get(" language" )?.asString ? : return null
103- val lang = Language .findLibreLang(langCode)
104-
105- if (lang == null ) {
106- UnityTranslate .logger.error(" Failed to find language for LibreTranslate code $langCode !" )
107- }
81+ }).asJsonArray.sortedByDescending { it.asJsonObject.get(" confidence" ).asDouble }
82+
83+ val langCode = detected.firstOrNull()?.asJsonObject?.get(" language" )?.asString ? : return null
84+ val lang = Language .findLibreLang(langCode)
10885
109- httpConn.disconnect()
110- s.close()
111- return lang
86+ if (lang == null ) {
87+ UnityTranslate .logger.error(" Failed to find language for LibreTranslate code $langCode !" )
11288 }
89+
90+ return lang
11391 }
11492
11593 open fun batchTranslate (from : String , to : String , request : List <String >): List <String > {
116- val url = URL (" $url /translate" )
117- val httpConn = url.openConnection() as HttpURLConnection
118- httpConn.requestMethod = " POST"
119- httpConn.setRequestProperty(" accept" , " application/json" )
120- httpConn.setRequestProperty(" Content-Type" , " application/json" )
121- httpConn.doOutput = true
122-
123- val writer = OutputStreamWriter (httpConn.outputStream, " UTF-8" )
124-
125- try {
126- writer.write(JsonObject ().apply {
127- addProperty(" source" , from)
128- addProperty(" target" , to)
129- add(" q" , JsonArray ().apply {
130- for (s in request) {
131- this .add(s)
132- }
133- })
134-
135- if (authKey?.isNotBlank() == true )
136- addProperty(" api_key" , authKey)
137- }.toString())
138-
139- writer.flush()
140- writer.close()
141- httpConn.outputStream.close()
142- if (httpConn.responseCode / 100 != 2 ) {
143- httpConn.disconnect()
144- throw Exception (" Failed to load ${this .url} /translate (code ${httpConn.responseCode} )" )
145- } else {
146- val responseStream = httpConn.inputStream
147- val s = Scanner (responseStream, " UTF-8" ).useDelimiter(" \\ A" )
148-
149- try {
150- val response = if (s.hasNext()) s.next() else " "
151-
152- val translated = JsonParser .parseString(response).asJsonObject.get(" translatedText" ).asJsonArray
153-
154- httpConn.disconnect()
155- s.close()
156- return translated.map { it.asString }
157- } catch (e: Exception ) {
158- httpConn.disconnect()
159- s.close()
160-
161- throw e
94+ val translated = HttpHelper .post(" $url /translate" , JsonObject ().apply {
95+ addProperty(" source" , from)
96+ addProperty(" target" , to)
97+ add(" q" , JsonArray ().apply {
98+ for (s in request) {
99+ this .add(s)
162100 }
163- }
164- } catch (e: Exception ) {
165- writer.close()
166- httpConn.disconnect()
101+ })
167102
168- throw e
169- }
103+ if (authKey?.isNotBlank() == true )
104+ addProperty(" api_key" , authKey)
105+ }).asJsonObject.get(" translatedText" ).asJsonArray
106+
107+ return translated.map { it.asString }
170108 }
171109
172110 fun translate (text : String , from : Language , to : Language ): String? {
@@ -180,44 +118,24 @@ open class LibreTranslateInstance(val url: String, private var weight: Int, val
180118 }
181119 }
182120
183- // Copied from LibreTranslate-Java, there were race conditions everywhere.
184121 open fun translate (from : String , to : String , request : String ): String {
185- val url = URL (" $url /translate" )
186- val httpConn = url.openConnection() as HttpURLConnection
187- httpConn.requestMethod = " POST"
188- httpConn.setRequestProperty(" accept" , " application/json" )
189- httpConn.setRequestProperty(" Content-Type" , " application/x-www-form-urlencoded" )
190- httpConn.doOutput = true
191-
192- val writer = OutputStreamWriter (httpConn.outputStream)
193- writer.write(
194- " q=" + URLEncoder .encode(
195- request,
196- " UTF-8"
197- ) + " &source=" + from + " &target=" + to + " &format=text" +
198- if (authKey?.isNotBlank() == true ) " &api_key=$authKey " else " "
199- )
200-
201- writer.flush()
202- writer.close()
203- httpConn.outputStream.close()
204- if (httpConn.responseCode / 100 != 2 ) {
205- throw Exception (" Failed to load ${this .url} /translate (code ${httpConn.responseCode} )" )
206- } else {
207- val responseStream = httpConn.inputStream
208- val s = Scanner (responseStream, " UTF-8" ).useDelimiter(" \\ A" )
209- val response = if (s.hasNext()) s.next() else " "
210-
211- s.close()
212- return JsonParser .parseString(response).asJsonObject.get(" translatedText" ).asString
213- }
122+ return HttpHelper .post(" $url /translate" , JsonObject ().apply {
123+ addProperty(" source" , from)
124+ addProperty(" target" , to)
125+ addProperty(" q" , request)
126+ addProperty(" format" , " text" )
127+
128+ if (authKey?.isNotBlank() == true )
129+ addProperty(" api_key" , authKey)
130+ })
131+ .asJsonObject.get(" translatedText" ).asString
214132 }
215133
216134 override fun getWeight (): Weight {
217135 return Weight .of(weight)
218136 }
219137
220138 companion object {
221- const val MAX_CONCURRENT_TRANSLATIONS = 5
139+ const val MAX_CONCURRENT_TRANSLATIONS = 15
222140 }
223141}
0 commit comments