feat wip: item lore translation
This commit is contained in:
parent
77a2623013
commit
f157831641
5 changed files with 110 additions and 25 deletions
|
@ -22,10 +22,15 @@ public class I18N {
|
||||||
this.playerLocale = getPlayerLocale();
|
this.playerLocale = getPlayerLocale();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public I18N(Locale locale) {
|
||||||
|
this.player = null;
|
||||||
|
this.playerLocale = locale;
|
||||||
|
}
|
||||||
|
|
||||||
public List<String> translateItem(String key, Object... arguments) {
|
public List<String> translateItem(String key, Object... arguments) {
|
||||||
final ArrayList<String> lines = new ArrayList<>();
|
final ArrayList<String> lines = new ArrayList<>();
|
||||||
final String itemNameKey = readString(key + ".name");
|
final String itemNameKey = readString(key + ".name");
|
||||||
final List<String> itemLoreKey = readString(key + ".lore");
|
final ArrayList<String> itemLoreKey = readList(key + ".lore");
|
||||||
try {
|
try {
|
||||||
// Item Name
|
// Item Name
|
||||||
formatter.applyPattern(itemNameKey);
|
formatter.applyPattern(itemNameKey);
|
||||||
|
@ -59,29 +64,35 @@ public class I18N {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readString(String key) {
|
private String readString(String key) {
|
||||||
String string;
|
YamlConfig yamlFile;
|
||||||
if (playerLocale == Locale.CUSTOM) {
|
if (playerLocale == Locale.CUSTOM) {
|
||||||
string = instance.getLocaleFileManager().getString(key);
|
yamlFile = instance.getLocaleFileManager().getYamlFile();
|
||||||
} else {
|
} else {
|
||||||
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
||||||
final YamlConfig yamlConfig = YamlConfig.load(resource);
|
yamlFile = YamlConfig.load(resource);
|
||||||
string = yamlConfig.getString(key);
|
}
|
||||||
|
return yamlFile.getString(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
return string;
|
private ArrayList<String> readList(String key) {
|
||||||
}
|
final ArrayList<String> lines = new ArrayList<>();
|
||||||
|
YamlConfig yamlFile;
|
||||||
private List<String> readList(String key) {
|
|
||||||
String string;
|
|
||||||
if (playerLocale == Locale.CUSTOM) {
|
if (playerLocale == Locale.CUSTOM) {
|
||||||
string = instance.getLocaleFileManager().getString(key);
|
yamlFile = instance.getLocaleFileManager().getYamlFile();
|
||||||
} else {
|
} else {
|
||||||
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
final InputStream resource = instance.getResource(playerLocale.getCode() + ".yml");
|
||||||
final YamlConfig yamlConfig = YamlConfig.load(resource);
|
yamlFile = YamlConfig.load(resource);
|
||||||
string = yamlConfig.getString(key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return string;
|
// 9 is a magic number
|
||||||
|
for (int i = 0; i < yamlFile.getInt(key + ".length"); i++) {
|
||||||
|
final String line = yamlFile.getString(key + ".content[" + i + "]");
|
||||||
|
System.out.println("line = " + line);
|
||||||
|
if (line != null && !line.equals("{" + i + "}")) {
|
||||||
|
lines.add(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Locale getPlayerLocale() {
|
private Locale getPlayerLocale() {
|
||||||
|
|
|
@ -1,6 +1,17 @@
|
||||||
package xyz.atnrch.nicko.i18n;
|
package xyz.atnrch.nicko.i18n;
|
||||||
|
|
||||||
public class I18NDict {
|
public class I18NDict {
|
||||||
|
public static class Error {
|
||||||
|
public static final String GENERIC = "error.generic";
|
||||||
|
public static final String PERMISSION = "error.permission";
|
||||||
|
public static final String CACHE = "error.cache";
|
||||||
|
public static final String MOJANG_NAME = "error.mojang_name";
|
||||||
|
public static final String MOJANG_SKIN = "error.mojang_skin";
|
||||||
|
public static final String INVALID_USERNAME = "error.invalid_username";
|
||||||
|
public static final String SQL_ERROR = "error.sql";
|
||||||
|
public static final String JSON_ERROR = "error.json";
|
||||||
|
}
|
||||||
|
|
||||||
public static class Event {
|
public static class Event {
|
||||||
private static final String EVENT_KEY = "event.";
|
private static final String EVENT_KEY = "event.";
|
||||||
|
|
||||||
|
@ -49,14 +60,15 @@ public class I18NDict {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Error {
|
public static class GUI {
|
||||||
public static final String GENERIC = "error.generic";
|
private static final String GUI_KEY = "gui.";
|
||||||
public static final String PERMISSION = "error.permission";
|
|
||||||
public static final String CACHE = "error.cache";
|
public static final String EXIT = GUI_KEY + "exit";
|
||||||
public static final String MOJANG_NAME = "error.mojang_name";
|
|
||||||
public static final String MOJANG_SKIN = "error.mojang_skin";
|
public static class Home {
|
||||||
public static final String INVALID_USERNAME = "error.invalid_username";
|
private static final String HOME_KEY = GUI_KEY + "home.";
|
||||||
public static final String SQL_ERROR = "error.sql";
|
|
||||||
public static final String JSON_ERROR = "error.json";
|
public static final String ADMIN = HOME_KEY + "admin";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.nio.file.Files;
|
||||||
public class LocaleFileManager {
|
public class LocaleFileManager {
|
||||||
private final File folder = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
|
private final File folder = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
|
||||||
private final File file = new File(folder, "lang.yml");
|
private final File file = new File(folder, "lang.yml");
|
||||||
|
private YamlConfig yamlFile;
|
||||||
|
|
||||||
public String getString(String key) {
|
public String getString(String key) {
|
||||||
if (!file.exists()) return key;
|
if (!file.exists()) return key;
|
||||||
|
@ -39,4 +40,15 @@ public class LocaleFileManager {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public YamlConfig getYamlFile() {
|
||||||
|
if (yamlFile == null) {
|
||||||
|
try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
|
||||||
|
yamlFile = YamlConfig.load(inputStream);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return yamlFile;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,13 +29,17 @@ gui:
|
||||||
exit: "Quitter"
|
exit: "Quitter"
|
||||||
home:
|
home:
|
||||||
admin:
|
admin:
|
||||||
name: "Panel d'administration"
|
name: "Panel d''administration"
|
||||||
lore:
|
lore:
|
||||||
|
length: 1
|
||||||
|
content:
|
||||||
- "Configurez et gérez Nicko."
|
- "Configurez et gérez Nicko."
|
||||||
settings:
|
settings:
|
||||||
name: "Paramètres"
|
name: "Paramètres"
|
||||||
lore:
|
lore:
|
||||||
- "Configurez votre expérience."
|
length: 1
|
||||||
|
content:
|
||||||
|
"Configurez votre expérience."
|
||||||
change_name:
|
change_name:
|
||||||
name: "Changer le §6pseudo"
|
name: "Changer le §6pseudo"
|
||||||
change_skin:
|
change_skin:
|
||||||
|
|
46
src/test/java/xyz/atnrch/nicko/test/i18n/I18NLoreTest.java
Normal file
46
src/test/java/xyz/atnrch/nicko/test/i18n/I18NLoreTest.java
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package xyz.atnrch.nicko.test.i18n;
|
||||||
|
|
||||||
|
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||||
|
import be.seeseemelk.mockbukkit.ServerMock;
|
||||||
|
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import xyz.atnrch.nicko.NickoBukkit;
|
||||||
|
import xyz.atnrch.nicko.config.Configuration;
|
||||||
|
import xyz.atnrch.nicko.config.DataSourceConfiguration;
|
||||||
|
import xyz.atnrch.nicko.i18n.I18N;
|
||||||
|
import xyz.atnrch.nicko.i18n.I18NDict;
|
||||||
|
import xyz.atnrch.nicko.i18n.Locale;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class I18NLoreTest {
|
||||||
|
private static NickoBukkit plugin;
|
||||||
|
private static PlayerMock player;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void setup() {
|
||||||
|
final Configuration config = new Configuration(
|
||||||
|
DataSourceConfiguration.SQL_EMPTY,
|
||||||
|
DataSourceConfiguration.REDIS_EMPTY,
|
||||||
|
"",
|
||||||
|
false);
|
||||||
|
final ServerMock server = MockBukkit.mock();
|
||||||
|
plugin = MockBukkit.load(NickoBukkit.class, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Translate Item Lore")
|
||||||
|
public void translateItemLore() {
|
||||||
|
final I18N i18n = new I18N(Locale.FRENCH);
|
||||||
|
List<String> strings = i18n.translateItem(I18NDict.GUI.Home.ADMIN);
|
||||||
|
System.out.println("strings = " + strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void shutdown() {
|
||||||
|
MockBukkit.unmock();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue