mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-08 17:37:34 +01:00
Optimized Locale replacement
This commit is contained in:
parent
074289c239
commit
27d436ce7f
@ -19,16 +19,12 @@ package com.djrapitops.plan.system.settings.locale;
|
|||||||
import com.djrapitops.plan.system.settings.locale.lang.*;
|
import com.djrapitops.plan.system.settings.locale.lang.*;
|
||||||
import com.djrapitops.plan.system.storage.file.FileResource;
|
import com.djrapitops.plan.system.storage.file.FileResource;
|
||||||
import com.djrapitops.plan.system.storage.file.PlanFiles;
|
import com.djrapitops.plan.system.storage.file.PlanFiles;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents loaded language information.
|
* Represents loaded language information.
|
||||||
@ -73,6 +69,15 @@ public class Locale extends HashMap<Lang, Message> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<Message> getNonDefault(Object key) {
|
||||||
|
Message storedValue = super.get(key);
|
||||||
|
if (key instanceof Lang && storedValue == null) {
|
||||||
|
return Optional.empty();
|
||||||
|
} else {
|
||||||
|
return Optional.of(storedValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String getString(Lang key) {
|
public String getString(Lang key) {
|
||||||
return get(key).toString();
|
return get(key).toString();
|
||||||
}
|
}
|
||||||
@ -99,8 +104,6 @@ public class Locale extends HashMap<Lang, Message> {
|
|||||||
return from;
|
return from;
|
||||||
}
|
}
|
||||||
|
|
||||||
String replaced = from;
|
|
||||||
|
|
||||||
Lang[][] langs = new Lang[][]{
|
Lang[][] langs = new Lang[][]{
|
||||||
NetworkPageLang.values(),
|
NetworkPageLang.values(),
|
||||||
PlayerPageLang.values(),
|
PlayerPageLang.values(),
|
||||||
@ -108,23 +111,21 @@ public class Locale extends HashMap<Lang, Message> {
|
|||||||
CommonHtmlLang.values()
|
CommonHtmlLang.values()
|
||||||
};
|
};
|
||||||
|
|
||||||
List<Entry<Lang, Message>> entries = Arrays.stream(langs)
|
List<String> replace = new ArrayList<>();
|
||||||
.flatMap(Arrays::stream)
|
List<String> with = new ArrayList<>();
|
||||||
.collect(Collectors.toMap(Function.identity(), this::get))
|
|
||||||
.entrySet().stream()
|
Arrays.stream(langs).flatMap(Arrays::stream)
|
||||||
// Longest first so that entries that contain each other don't partially replace.
|
// Longest first so that entries that contain each other don't partially replace.
|
||||||
.sorted((one, two) -> Integer.compare(
|
.sorted((one, two) -> Integer.compare(
|
||||||
two.getKey().getIdentifier().length(),
|
two.getIdentifier().length(),
|
||||||
one.getKey().getIdentifier().length()
|
one.getIdentifier().length()
|
||||||
)).collect(Collectors.toList());
|
))
|
||||||
|
.forEach(lang -> getNonDefault(lang).ifPresent(replacement -> {
|
||||||
|
replace.add(lang.getDefault());
|
||||||
|
with.add(replacement.toString());
|
||||||
|
}));
|
||||||
|
|
||||||
for (Entry<Lang, Message> entry : entries) {
|
return StringUtils.replaceEach(from, replace.toArray(new String[0]), with.toArray(new String[0]));
|
||||||
String defaultValue = entry.getKey().getDefault();
|
|
||||||
String replacement = entry.getValue().toString();
|
|
||||||
|
|
||||||
replaced = replaced.replace(defaultValue, replacement);
|
|
||||||
}
|
|
||||||
return replaced;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package com.djrapitops.plan.system.settings.locale;
|
package com.djrapitops.plan.system.settings.locale;
|
||||||
|
|
||||||
import com.djrapitops.plugin.utilities.Verify;
|
import com.djrapitops.plugin.utilities.Verify;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.text.StringSubstitutor;
|
import org.apache.commons.text.StringSubstitutor;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -51,11 +52,11 @@ public class Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String[] toArray() {
|
public String[] toArray() {
|
||||||
return content.split("\\\\");
|
return StringUtils.split(content, '\\');
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] toArray(Serializable... p) {
|
public String[] toArray(Serializable... p) {
|
||||||
return parse(p).split("\\\\");
|
return parse(p).split(content, '\\');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user