Skip to content

Commit

Permalink
String improvements & use SQLite for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Alemiz112 committed Mar 25, 2023
1 parent 35f4a21 commit d8d3f12
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ bin/
service.properties

common/src/main/generated/
service/src/main/generated/
service/src/main/generated/
*.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package eu.mizerak.alemiz.translationlib.common.string;

import java.util.Locale;
import java.util.function.Function;

public class JoinLocalString<T> implements LocalString<T> {

private final LocalString<T> left;
private final LocalString<T> right;

private final String delimiter;

public JoinLocalString(LocalString<T> left, LocalString<T> right) {
this(left, right, "");
}

public JoinLocalString(LocalString<T> left, LocalString<T> right, String delimiter) {
this.left = left;
this.right = right;
this.delimiter = delimiter;
}

@Override
public String getKey() {
return this.left.getKey() + "_" + this.right.getKey();
}

@Override
public LocalString<T> reload() {
this.left.reload();
this.right.reload();
return this;
}

@Override
public LocalString<T> enableReload(boolean enable) {
this.left.enableReload(enable);
this.right.enableReload(enable);
return this;
}

@Override
public LocalString<T> withArgument(String name, Function<TranslationContext<T>, String> mapper) {
throw new UnsupportedOperationException("Joined string does not support arguments");
}

@Override
public LocalString<T> clearArguments() {
throw new UnsupportedOperationException("Joined string does not support arguments");
}

@Override
public String getFormatted() {
return this.left.getFormatted() + this.delimiter + this.right.getFormatted();
}

@Override
public String getFormatted(Locale locale) {
return this.left.getFormatted(locale) + this.delimiter + this.right.getFormatted(locale);
}

@Override
public String getText(T object) {
return this.left.getText(object) + this.delimiter + this.right.getText(object);
}

@Override
public void uploadFallbackMessage() {
this.left.uploadFallbackMessage();
this.right.uploadFallbackMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ static <T> LocalString<T> immutable(String text) {
return new StaticLocalString<>(term);
}

static <T> LocalString<T> wrapper(String text) {
return new StringWrapper<>(text);
}

static <T> LocalString<T> empty() {
return (LocalString<T>) StringWrapper.EMPTY;
}

String getKey();

LocalString<T> reload();
Expand All @@ -54,4 +62,16 @@ default LocalString<T> withArgument(String name, Object argument) {
String getText(T object);

void uploadFallbackMessage();

default LocalString<T> append(String string) {
return new JoinLocalString<>(this, LocalString.wrapper(string));
}

default LocalString<T> append(LocalString<T> string) {
return new JoinLocalString<>(this, string);
}

default LocalString<T> append(LocalString<T> string, String delimiter) {
return new JoinLocalString<>(this, string, delimiter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import java.util.Locale;

/**
* A implementation of a LocalString, which does support formatting, but does not support translations.
*/
public class StaticLocalString<T> extends LocalStringBase<T> {
public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package eu.mizerak.alemiz.translationlib.common.string;

import java.util.Locale;
import java.util.function.Function;

/**
* A very simple implementation of a LocalString, which does not do any translations or formatting
* nor does support language translations.
*/
public class StringWrapper<T> implements LocalString<T> {
public static StringWrapper<Object> EMPTY = new StringWrapper<>("");

private final String text;

protected StringWrapper(String text) {
this.text = text;
}

@Override
public String getKey() {
return "wrapper";
}

@Override
public LocalString<T> reload() {
throw new UnsupportedOperationException("Static string does not support reload");
}

@Override
public LocalString<T> enableReload(boolean b) {
throw new UnsupportedOperationException("Static string does not support reload");
}

@Override
public LocalString<T> withArgument(String s, Function<TranslationContext<T>, String> function) {
return this;
}

@Override
public LocalString<T> clearArguments() {
return this;
}

@Override
public String getFormatted() {
return this.text;
}

@Override
public String getFormatted(Locale locale) {
return this.text;
}

@Override
public String getText(T t) {
return this.text;
}

@Override
public void uploadFallbackMessage() {
throw new UnsupportedOperationException("Static can not be uploaded");
}

@Override
public String toString() {
return this.text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ public void testStatic() {

assertEquals("Hi " + User.ENGLISH.getName(), string1.getText(User.ENGLISH));
}

@Test
public void testJoinedStrings() {
LocalString<User> string = LocalString.<User>from("test_string4", "Hello {user}")
.withArgument("user", ctx -> ctx.getObject().getName());
LocalString<User> string2 = LocalString.immutable(" this is test");

String text = string.append(string2).getText(User.ENGLISH);
assertEquals("Hello " + User.ENGLISH.getName() + " this is test", text);
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ avaje-inject-generator = { module = "io.avaje:avaje-inject-generator", version.r
# Other dependencies
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }
gson = { module = "com.google.code.gson:gson", version = "2.10.1" }
mongo-driver = { module = "org.mongodb:mongodb-driver-sync", version = "4.7.0" }
sqlite-driver = { module = "org.xerial:sqlite-jdbc", version = "3.41.2.0" }
2 changes: 1 addition & 1 deletion service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group = "eu.mizerak.alemiz.translationlib.service"
dependencies {
implementation(project(":common"))
implementation(libs.slf4j.simple)
implementation(libs.mongo.driver)
implementation(libs.sqlite.driver)
implementation(libs.javalin)
implementation(libs.avaje.inject)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import eu.mizerak.alemiz.translationlib.common.gson.LocaleSerializer;
import eu.mizerak.alemiz.translationlib.common.structure.RestStatus;
import eu.mizerak.alemiz.translationlib.service.access.AccessRole;
Expand Down Expand Up @@ -79,27 +74,12 @@ public static Configuration loadConfiguration() throws IOException {
private final Javalin server;
private final Gson gson;

private final MongoClient mongoClient;
private final MongoDatabase mongoDatabase;

private final TranslationDataScrapper scrapper;
private final TermsManager termsManager;

public TranslationLibService(Configuration configuration) {
this.configuration = configuration;

MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(configuration.getMongoUrl()))
.applyToConnectionPoolSettings(builder -> builder
.minSize(2)
.maxSize(configuration.getMaxMongoPoolSize()))
.applyToSocketSettings(builder -> builder
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS))
.build();
this.mongoClient = MongoClients.create(settings);
this.mongoDatabase = mongoClient.getDatabase(configuration.getMongoDatabase());

this.gson = new GsonBuilder()
.registerTypeAdapter(Locale.class, new LocaleSerializer())
.registerTypeAdapterFactory(new DataCollectionTypeAdapterFactory())
Expand All @@ -110,8 +90,6 @@ public TranslationLibService(Configuration configuration) {
BeanScope scope = BeanScope.builder()
.bean(TranslationLibService.class, this)
.bean(Configuration.class, configuration)
.bean(MongoClient.class, this.mongoClient)
.bean(MongoDatabase.class, this.mongoDatabase)
.bean(Gson.class, this.gson)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TermsManager {
private final Map<String, TermsGroup> groups = new ConcurrentHashMap<>();

public void onInit() {
Collection<TranslationTerm> terms = this.repository.getAllTerms(null);
Collection<TranslationTerm> terms = this.repository.getAllTerms();
for (TranslationTerm term : terms) {
this.terms.put(term.getKey(), term);
for (String tag : term.getTags()) {
Expand Down
Loading

0 comments on commit d8d3f12

Please sign in to comment.