mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-21 05:50:18 +08:00
Fixed some inconsistencies in Locale implementation
This commit is contained in:
parent
52fddeac78
commit
64984c6ea8
@ -8,13 +8,14 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Represents loaded language information.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
// TODO Turn into TreeMap that sorts longest first
|
||||
public class Locale extends HashMap<Lang, Message> {
|
||||
|
||||
public Locale() {
|
||||
@ -42,10 +43,11 @@ public class Locale extends HashMap<Lang, Message> {
|
||||
|
||||
@Override
|
||||
public Message get(Object key) {
|
||||
if (key instanceof Lang) {
|
||||
return getOrDefault(key, new Message(((Lang) key).getDefault()));
|
||||
Message storedValue = super.get(key);
|
||||
if (key instanceof Lang && storedValue == null) {
|
||||
return new Message(((Lang) key).getDefault());
|
||||
} else {
|
||||
return super.get(key);
|
||||
return storedValue;
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,13 +71,20 @@ public class Locale extends HashMap<Lang, Message> {
|
||||
if (isEmpty()) {
|
||||
return from;
|
||||
}
|
||||
for (Entry<Lang, Message> entry : entrySet()) {
|
||||
|
||||
String replaced = from;
|
||||
|
||||
// Longest first so that entries that contain each other don't partially replace.
|
||||
List<Entry<Lang, Message>> entries = entrySet().stream().sorted(
|
||||
(one, two) -> Integer.compare(two.getKey().getIdentifier().length(), one.getKey().getIdentifier().length())
|
||||
).collect(Collectors.toList());
|
||||
|
||||
for (Entry<Lang, Message> entry : entries) {
|
||||
String defaultValue = entry.getKey().getDefault();
|
||||
String replacement = entry.getValue().toString();
|
||||
|
||||
from = from.replace(defaultValue, replacement);
|
||||
replaced = replaced.replace(defaultValue, replacement);
|
||||
}
|
||||
|
||||
return from;
|
||||
return replaced;
|
||||
}
|
||||
}
|
@ -1,18 +1,14 @@
|
||||
package com.djrapitops.plan.system.locale;
|
||||
|
||||
import com.djrapitops.plan.system.locale.lang.Lang;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigSystem;
|
||||
import com.djrapitops.plan.utilities.comparators.LocaleEntryComparator;
|
||||
import com.djrapitops.plan.utilities.comparators.StringLengthComparator;
|
||||
import com.djrapitops.plugin.api.config.Config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -22,43 +18,48 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class LocaleFileWriter {
|
||||
|
||||
private final Locale locale;
|
||||
private Locale locale;
|
||||
|
||||
public LocaleFileWriter(Locale locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public void writeToFile(File file) throws IOException {
|
||||
// Find longest identifier
|
||||
Optional<String> key = LocaleSystem.getIdentifiers().keySet().stream()
|
||||
.min(new StringLengthComparator());
|
||||
if (!key.isPresent()) {
|
||||
throw new IllegalStateException("LocaleSystem defines no Identifiers.");
|
||||
}
|
||||
// Find longest identifier length for spacing
|
||||
int length = LocaleSystem.getIdentifiers().keySet().stream()
|
||||
.min(new StringLengthComparator())
|
||||
.map(String::length).orElse(0) + 2;
|
||||
|
||||
for (Lang lang : LocaleSystem.getIdentifiers().values()) {
|
||||
if (!locale.containsKey(lang)) {
|
||||
locale.put(lang, new Message(lang.getDefault()));
|
||||
}
|
||||
}
|
||||
addMissingLang();
|
||||
|
||||
int length = key.get().length() + 2;
|
||||
List<String> lines = locale.entrySet().stream()
|
||||
List<String> lines = createLines(length);
|
||||
|
||||
write(file, lines);
|
||||
}
|
||||
|
||||
private void write(File file, List<String> lines) throws IOException {
|
||||
if (!file.exists()) {
|
||||
Files.createFile(file.toPath());
|
||||
}
|
||||
Files.write(file.toPath(), lines, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private List<String> createLines(int length) {
|
||||
return locale.entrySet().stream()
|
||||
.sorted(new LocaleEntryComparator())
|
||||
.map(entry -> {
|
||||
String value = entry.getValue() != null ? entry.getValue().toString() : entry.getKey().getDefault();
|
||||
return getSpacedIdentifier(entry.getKey().getIdentifier(), length) + "|| " + value;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
if (!file.exists()) {
|
||||
Files.createFile(file.toPath());
|
||||
}
|
||||
|
||||
private void addMissingLang() {
|
||||
for (Lang lang : LocaleSystem.getIdentifiers().values()) {
|
||||
if (!locale.containsKey(lang)) {
|
||||
locale.put(lang, new Message(lang.getDefault()));
|
||||
}
|
||||
}
|
||||
|
||||
Files.write(file.toPath(), lines, StandardCharsets.UTF_8);
|
||||
|
||||
Config config = ConfigSystem.getConfig();
|
||||
config.set(Settings.WRITE_NEW_LOCALE.getPath(), false);
|
||||
config.save();
|
||||
}
|
||||
|
||||
private String getSpacedIdentifier(String identifier, int length) {
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.djrapitops.plan.system.locale;
|
||||
|
||||
import com.djrapitops.plan.utilities.file.FileUtil;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Test class for {@link LocaleFileWriter}.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class LocaleFileWriterTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void writesAllIdentifiers() throws IOException {
|
||||
File file = temporaryFolder.newFile();
|
||||
new LocaleFileWriter(new Locale()).writeToFile(file);
|
||||
|
||||
long expected = LocaleSystem.getIdentifiers().size();
|
||||
int result = FileUtil.lines(file, Charset.forName("UTF-8")).size();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user