libjf-translate-v1 api changes to allow new backends
This commit is contained in:
parent
71a382ee76
commit
8fe9477f79
@ -1,6 +0,0 @@
|
|||||||
# libjf-translate-v0
|
|
||||||
libjf-translate-v0 provides a utility class for translating strings through Google Translate.
|
|
||||||
|
|
||||||
To translate a string, just call `TranslateService.translate("Text to translate", Language.AUTO_DETECT, Language.ENGLISH)`
|
|
||||||
|
|
||||||
To configure languages, you can use the `Language` enum itself or `Language.byId("en")`
|
|
25
docs/libjf-translate-v1.md
Normal file
25
docs/libjf-translate-v1.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# libjf-translate-v0
|
||||||
|
libjf-translate-v0 provides a utility class for translating strings through Google Translate.
|
||||||
|
|
||||||
|
To use this, first obtain a TranslateService instance. You can use `TranslateService.getConfigured()` to do so.
|
||||||
|
Please be aware that due to the nature of java generics, a workaround as seen in the example may be needed for successful compilation.
|
||||||
|
You can also directly access implementations, however, this is not recommended.
|
||||||
|
The TranslateService interface exposes all relevant functionality.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```java
|
||||||
|
public void onInitialize() {
|
||||||
|
try {
|
||||||
|
runTest(TranslateService.getConfigured());
|
||||||
|
} catch (Throwable e) {
|
||||||
|
LibJf.LOGGER.error("Could not verify translation validity", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> void runTest(TranslateService<T> ts) throws TranslateException {
|
||||||
|
final String source = "Cogito, ergo sum";
|
||||||
|
final String expected = "I think, therefore I am";
|
||||||
|
assert expected.equals(ts.translate(source, ts.detect(source), ts.parseLang("en")));
|
||||||
|
assert expected.equals(ts.translate(source, ts.parseLang("la"), ts.parseLang("en")));
|
||||||
|
}
|
||||||
|
```
|
@ -1,8 +1,8 @@
|
|||||||
# https://fabricmc.net/develop/
|
# https://fabricmc.net/develop/
|
||||||
minecraft_version=22w06a
|
minecraft_version=1.18.2
|
||||||
yarn_mappings=build.8
|
yarn_mappings=build.1
|
||||||
loader_version=0.13.1
|
loader_version=0.13.3
|
||||||
fabric_version=0.47.1+1.18.2
|
fabric_version=0.47.8+1.18.2
|
||||||
maven_group=io.gitlab.jfronny.libjf
|
maven_group=io.gitlab.jfronny.libjf
|
||||||
archive_base_name=libjf
|
archive_base_name=libjf
|
||||||
dev_only_module=libjf-devutil-v0
|
dev_only_module=libjf-devutil-v0
|
||||||
|
@ -7,6 +7,6 @@ import net.minecraft.util.Identifier;
|
|||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
|
|
||||||
public class Tags {
|
public class Tags {
|
||||||
public static final TagKey<Item> SHULKER_ILLEGAL = TagKey.intern(Registry.ITEM_KEY, new Identifier(LibJf.MOD_ID, "shulker_boxes_illegal"));
|
public static final TagKey<Item> SHULKER_ILLEGAL = TagKey.of(Registry.ITEM_KEY, new Identifier(LibJf.MOD_ID, "shulker_boxes_illegal"));
|
||||||
public static final TagKey<Item> OVERPOWERED = TagKey.intern(Registry.ITEM_KEY, new Identifier(LibJf.MOD_ID, "overpowered"));
|
public static final TagKey<Item> OVERPOWERED = TagKey.of(Registry.ITEM_KEY, new Identifier(LibJf.MOD_ID, "overpowered"));
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
package io.gitlab.jfronny.libjf.translate.test;
|
|
||||||
|
|
||||||
import io.gitlab.jfronny.libjf.LibJf;
|
|
||||||
import io.gitlab.jfronny.libjf.translate.Language;
|
|
||||||
import io.gitlab.jfronny.libjf.translate.TranslateService;
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
|
||||||
|
|
||||||
public class TestEntrypoint implements ModInitializer {
|
|
||||||
@Override
|
|
||||||
public void onInitialize() {
|
|
||||||
try {
|
|
||||||
final String source = "Cogito, ergo sum";
|
|
||||||
final String expected = "I think, therefore I am";
|
|
||||||
assert expected.equals(TranslateService.translate(source, Language.AUTO_DETECT, Language.ENGLISH));
|
|
||||||
assert expected.equals(TranslateService.translate(source, Language.LATIN, Language.ENGLISH));
|
|
||||||
} catch (Throwable e) {
|
|
||||||
LibJf.LOGGER.error("Could not verify translation validity", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
archivesBaseName = "libjf-translate-v0"
|
archivesBaseName = "libjf-translate-v1"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
moduleDependencies(project, ["libjf-base"])
|
moduleDependencies(project, ["libjf-base"])
|
@ -1,4 +1,4 @@
|
|||||||
package io.gitlab.jfronny.libjf.translate;
|
package io.gitlab.jfronny.libjf.translate.api;
|
||||||
|
|
||||||
public class TranslateException extends Exception {
|
public class TranslateException extends Exception {
|
||||||
public TranslateException() {
|
public TranslateException() {
|
@ -0,0 +1,15 @@
|
|||||||
|
package io.gitlab.jfronny.libjf.translate.api;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.libjf.translate.impl.google.GoogleTranslateService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface TranslateService<T> {
|
||||||
|
static TranslateService<?> getConfigured() {
|
||||||
|
return new GoogleTranslateService();
|
||||||
|
}
|
||||||
|
String translate(String textToTranslate, T translateFrom, T translateTo) throws TranslateException;
|
||||||
|
T detect(String text) throws TranslateException;
|
||||||
|
T parseLang(String lang);
|
||||||
|
List<T> getAvailableLanguages();
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package io.gitlab.jfronny.libjf.translate;
|
package io.gitlab.jfronny.libjf.translate.impl.google;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
||||||
import org.apache.commons.lang3.StringEscapeUtils;
|
import org.apache.commons.lang3.StringEscapeUtils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -11,13 +13,17 @@ import java.net.URLEncoder;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class TranslateService {
|
public class GoogleTranslateService implements TranslateService<Language> {
|
||||||
private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE);
|
private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE);
|
||||||
|
|
||||||
public static String translate(String textToTranslate, Language translateFrom, Language translateTo) throws TranslateException {
|
@Override
|
||||||
|
public String translate(String textToTranslate, Language translateFrom, Language translateTo) throws TranslateException {
|
||||||
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null");
|
if (textToTranslate == null) throw new TranslateException("textToTranslate must not be null");
|
||||||
String pageSource = "";
|
String pageSource = "";
|
||||||
try {
|
try {
|
||||||
@ -41,6 +47,23 @@ public class TranslateService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Language detect(String text) throws TranslateException {
|
||||||
|
return Language.AUTO_DETECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Language parseLang(String lang) {
|
||||||
|
return Language.byId(lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Language> getAvailableLanguages() {
|
||||||
|
List<Language> langs = new ArrayList<>(Arrays.asList(Language.values()));
|
||||||
|
langs.remove(Language.AUTO_DETECT);
|
||||||
|
return langs;
|
||||||
|
}
|
||||||
|
|
||||||
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo)
|
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
if (textToTranslate == null)
|
if (textToTranslate == null)
|
@ -1,4 +1,4 @@
|
|||||||
package io.gitlab.jfronny.libjf.translate;
|
package io.gitlab.jfronny.libjf.translate.impl.google;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
@ -0,0 +1,28 @@
|
|||||||
|
package io.gitlab.jfronny.libjf.translate.impl.noop;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class NoopTranslateService implements TranslateService<Object> {
|
||||||
|
@Override
|
||||||
|
public String translate(String textToTranslate, Object translateFrom, Object translateTo) throws TranslateException {
|
||||||
|
return textToTranslate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object detect(String text) throws TranslateException {
|
||||||
|
return "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object parseLang(String lang) {
|
||||||
|
return "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Object> getAvailableLanguages() {
|
||||||
|
return List.of("none");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package io.gitlab.jfronny.libjf.translate.test;
|
||||||
|
|
||||||
|
import io.gitlab.jfronny.libjf.LibJf;
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
||||||
|
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
||||||
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
|
||||||
|
public class TestEntrypoint implements ModInitializer {
|
||||||
|
@Override
|
||||||
|
public void onInitialize() {
|
||||||
|
try {
|
||||||
|
runTest(TranslateService.getConfigured());
|
||||||
|
} catch (Throwable e) {
|
||||||
|
LibJf.LOGGER.error("Could not verify translation validity", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> void runTest(TranslateService<T> ts) throws TranslateException {
|
||||||
|
final String source = "Cogito, ergo sum";
|
||||||
|
final String expected = "I think, therefore I am";
|
||||||
|
assert expected.equals(ts.translate(source, ts.detect(source), ts.parseLang("en")));
|
||||||
|
assert expected.equals(ts.translate(source, ts.parseLang("la"), ts.parseLang("en")));
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,6 @@ nav:
|
|||||||
- 'libjf-devutil-v0.md'
|
- 'libjf-devutil-v0.md'
|
||||||
- 'libjf-data-v0.md'
|
- 'libjf-data-v0.md'
|
||||||
- 'libjf-data-manipulation-v0.md'
|
- 'libjf-data-manipulation-v0.md'
|
||||||
- 'libjf-translate-v0.md'
|
- 'libjf-translate-v1.md'
|
||||||
- 'libjf-unsafe-v0.md'
|
- 'libjf-unsafe-v0.md'
|
||||||
- 'libjf-web-v0.md'
|
- 'libjf-web-v0.md'
|
||||||
|
@ -16,6 +16,6 @@ include 'libjf-config-v0'
|
|||||||
include 'libjf-data-v0'
|
include 'libjf-data-v0'
|
||||||
include 'libjf-data-manipulation-v0'
|
include 'libjf-data-manipulation-v0'
|
||||||
include 'libjf-devutil-v0'
|
include 'libjf-devutil-v0'
|
||||||
include 'libjf-translate-v0'
|
include 'libjf-translate-v1'
|
||||||
include 'libjf-unsafe-v0'
|
include 'libjf-unsafe-v0'
|
||||||
include 'libjf-web-v0'
|
include 'libjf-web-v0'
|
Loading…
Reference in New Issue
Block a user