feat: wip translations

This commit is contained in:
ineanto 2023-07-27 02:26:11 +02:00
parent 5d3a99f72d
commit c929e73b0d
17 changed files with 148 additions and 94 deletions

View file

@ -14,6 +14,8 @@ public class AdminGUI {
public AdminGUI(Player player) {
final HomeGUI parent = new HomeGUI(player);
final GoBackItem backItem = new GoBackItem(player);
this.gui = Gui.normal()
.setStructure(
"# # # # # # # # #",
@ -22,7 +24,7 @@ public class AdminGUI {
)
.addIngredient('S', new ManageCacheItem())
.addIngredient('C', new ManagePlayerItem())
.addIngredient('B', new GoBackItem(parent.getGUI(), parent.getTitle()))
.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()))
.build();
this.player = player;
}

View file

@ -37,6 +37,8 @@ public class CacheDetailedGUI {
.collect(Collectors.toList());
final CacheManagementGUI parent = new CacheManagementGUI(player);
final GoBackItem backItem = new GoBackItem(player);
gui = ScrollGui.items(guiItemBuilder -> {
guiItemBuilder.setStructure(
"x x x x x x x x U",
@ -48,7 +50,7 @@ public class CacheDetailedGUI {
guiItemBuilder.addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL);
guiItemBuilder.addIngredient('U', new ScrollUpItem());
guiItemBuilder.addIngredient('D', new ScrollDownItem());
guiItemBuilder.addIngredient('B', new GoBackItem(parent.getGUI(), parent.getTitle()));
guiItemBuilder.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()));
guiItemBuilder.setContent(items);
});

View file

@ -15,13 +15,15 @@ public class CacheManagementGUI {
public CacheManagementGUI(Player player) {
final AdminGUI parent = new AdminGUI(player);
final GoBackItem backItem = new GoBackItem(player);
this.gui = Gui.normal()
.setStructure(
"# # # # # # # # #",
"# # # S A D # # #",
"B # # # # # # # #"
)
.addIngredient('B', new GoBackItem(parent.getGUI(), parent.getTitle()))
.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()))
.addIngredient('S', new CacheStatisticsItem())
.addIngredient('A', new InvalidateCacheItem())
.addIngredient('D', new InvalidateEntryItem())

View file

@ -29,6 +29,8 @@ public class PlayerCheckGUI {
.collect(Collectors.toList());
final AdminGUI parent = new AdminGUI(player);
final GoBackItem backItem = new GoBackItem(player);
gui = ScrollGui.items(guiItemBuilder -> {
guiItemBuilder.setStructure(
"x x x x x x x x U",
@ -40,7 +42,7 @@ public class PlayerCheckGUI {
guiItemBuilder.addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL);
guiItemBuilder.addIngredient('U', new ScrollUpItem());
guiItemBuilder.addIngredient('D', new ScrollDownItem());
guiItemBuilder.addIngredient('B', new GoBackItem(parent.getGUI(), parent.getTitle()));
guiItemBuilder.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()));
guiItemBuilder.setContent(items);
});

View file

@ -24,11 +24,15 @@ public class SettingsGUI {
dynamicStructure[1] = dynamicStructure[1].replace("T", "U");
final HomeGUI parent = new HomeGUI(player);
final LanguageCyclingItem languageItem = new LanguageCyclingItem(player);
final BungeeCordCyclingItem bungeeCordItem = new BungeeCordCyclingItem(player);
final GoBackItem backItem = new GoBackItem(player);
this.gui = Gui.normal()
.setStructure(dynamicStructure)
.addIngredient('B', new GoBackItem(parent.getGUI(), parent.getTitle()))
.addIngredient('L', new LanguageCyclingItem().get(player))
.addIngredient('T', new BungeeCordCyclingItem().get(player))
.addIngredient('B', backItem.get(parent.getGUI(), parent.getTitle()))
.addIngredient('L', languageItem.get())
.addIngredient('T', bungeeCordItem.get())
.build();
this.player = player;
}

View file

@ -1,17 +1,30 @@
package xyz.atnrch.nicko.gui.items.common;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.xenondevs.invui.gui.Gui;
import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SuppliedItem;
import xyz.xenondevs.invui.window.Window;
public class GoBackItem extends SuppliedItem {
public GoBackItem(Gui gui, String parentTitle) {
super(() -> {
public class GoBackItem {
private final Player player;
private final I18N i18n;
public GoBackItem(Player player) {
this.player = player;
this.i18n = new I18N(player);
}
public SuppliedItem get(Gui gui, String parentTitle) {
final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.GO_BACK);
return new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.ARROW);
builder.setDisplayName("Go back");
builder.addLoreLines("§7Return to the previous window.");
builder.setDisplayName(translation.getName());
translation.getLore().forEach(builder::addLoreLines);
return builder;
}, click -> {
click.getEvent().getView().close();

View file

@ -14,8 +14,4 @@ public class ExitItem extends SimpleItem {
}
});
}
public ItemBuilder getItemBuilder() {
return null;
}
}

View file

@ -1,6 +1,9 @@
package xyz.atnrch.nicko.gui.items.settings;
import xyz.atnrch.nicko.NickoBukkit;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.atnrch.nicko.profile.NickoProfile;
import org.bukkit.Material;
import org.bukkit.Sound;
@ -14,12 +17,20 @@ import xyz.xenondevs.invui.item.impl.SimpleItem;
import java.util.Optional;
public class BungeeCordCyclingItem {
private final ItemProvider[] providers = new ItemProvider[]{
getItemProviderForValue(true),
getItemProviderForValue(false)
};
private final Player player;
private final I18N i18n;
private final ItemProvider[] providers;
public AbstractItem get(Player player) {
public BungeeCordCyclingItem(Player player) {
this.player = player;
this.i18n = new I18N(player);
this.providers = new ItemProvider[]{
getItemProviderForValue(true),
getItemProviderForValue(false)
};
}
public AbstractItem get() {
final Optional<NickoProfile> profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId());
if (profile.isPresent()) {
final NickoProfile nickoProfile = profile.get();
@ -35,7 +46,9 @@ public class BungeeCordCyclingItem {
private ItemProvider getItemProviderForValue(boolean enabled) {
final ItemBuilder builder = new ItemBuilder(Material.COMPASS);
builder.setDisplayName("BungeeCord transfer");
final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Settings.BUNGEECORD);
builder.setDisplayName(translation.getName());
if (enabled) {
builder.addLoreLines("§7> §cDisabled");
builder.addLoreLines("§6§l> §a§lEnabled");

View file

@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import xyz.atnrch.nicko.NickoBukkit;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.atnrch.nicko.i18n.Locale;
import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.storage.PlayerDataStore;
@ -21,9 +22,17 @@ import java.util.List;
import java.util.Optional;
public class LanguageCyclingItem {
private final ItemProvider[] providers = getItems();
private final Player player;
private final ItemProvider[] providers;
private final I18N i18n;
public AbstractItem get(Player player) {
public LanguageCyclingItem(Player player) {
this.player = player;
this.i18n = new I18N(player);
this.providers = getItems();
}
public AbstractItem get() {
final PlayerDataStore dataStore = NickoBukkit.getInstance().getDataStore();
final Optional<NickoProfile> profile = dataStore.getData(player.getUniqueId());
if (profile.isPresent()) {
@ -46,7 +55,9 @@ public class LanguageCyclingItem {
private ItemProvider generateItem(Locale locale, List<Locale> locales) {
final ItemBuilder builder = new ItemBuilder(Material.OAK_SIGN);
builder.setDisplayName("Language");
final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Settings.LANGUAGE);
builder.setDisplayName(translation.getName());
for (Locale value : locales) {
if (locale != value) {
builder.addLoreLines("§7> " + value.getName());
@ -61,8 +72,8 @@ public class LanguageCyclingItem {
private ItemProvider[] getItems() {
final NickoBukkit instance = NickoBukkit.getInstance();
final ArrayList<ItemProvider> items = new ArrayList<>();
final ArrayList<Locale> localesToGenerate = new ArrayList<>();
Collections.addAll(localesToGenerate, Locale.values());
if (!instance.getNickoConfig().isCustomLocale()) {
localesToGenerate.remove(Locale.CUSTOM);

View file

@ -8,8 +8,7 @@ import xyz.atnrch.nicko.appearance.AppearanceManager;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
public class I18N {
private final MessageFormat formatter = new MessageFormat("");
@ -30,19 +29,31 @@ public class I18N {
this.yamlConfig = getYamlConfig();
}
public List<String> translateItem(String key, Object... arguments) {
final ArrayList<String> lines = new ArrayList<>();
final String itemNameKey = readString(key + ".name");
final ArrayList<String> itemLoreKey = readList(key + ".lore");
try {
// Item Name
formatter.applyPattern(itemNameKey);
final String itemNameTranslated = formatter.format(arguments);
lines.add(itemNameTranslated);
return lines;
} catch (Exception e) {
return Collections.singletonList(key);
public ItemTranslation translateItem(String key, String... args) {
final String name = readString(key + ".name");
final ArrayList<String> lore = readList(key + ".lore");
// Add all elements to a list
final ArrayList<String> toTranslate = new ArrayList<>();
toTranslate.add(name);
toTranslate.addAll(lore);
// Set starting index to 0
int index = 0;
// While iterator next value exists/isn't null
final Iterator<String> iterator = toTranslate.iterator();
while (!iterator.hasNext() || iterator.next() == null) {
// Get the current line
final String currentLine = toTranslate.get(index);
// Replace with the corresponding varargs index
toTranslate.set(index, currentLine.replace("{" + index + "}", args[index]));
// Increment the index
index++;
}
return new ItemTranslation(toTranslate.get(0), toTranslate.subList(1, toTranslate.size()));
}
public String translate(String key, Object... arguments) {

View file

@ -64,11 +64,21 @@ public class I18NDict {
private static final String GUI_KEY = "gui.";
public static final String EXIT = GUI_KEY + "exit";
public static final String GO_BACK = GUI_KEY + "go_back";
public static class Home {
private static final String HOME_KEY = GUI_KEY + "home.";
public static final String ADMIN = HOME_KEY + "admin";
}
public static class Settings {
private static final String SETTINGS_KEY = GUI_KEY + "settings.";
public static final String LANGUAGE = SETTINGS_KEY + "language";
public static final String BUNGEECORD = SETTINGS_KEY + "bungeecord";
}
}
}

View file

@ -0,0 +1,21 @@
package xyz.atnrch.nicko.i18n;
import java.util.List;
public class ItemTranslation {
private final String name;
private final List<String> lore;
public ItemTranslation(String name, List<String> lore) {
this.name = name;
this.lore = lore;
}
public String getName() {
return name;
}
public List<String> getLore() {
return lore;
}
}

View file

@ -1,38 +0,0 @@
package xyz.atnrch.nicko.i18n;
import java.util.ArrayList;
import java.util.Iterator;
public class ItemTranslator {
private final String name;
private final ArrayList<String> lore;
public ItemTranslator(String name, ArrayList<String> lore) {
this.name = name;
this.lore = lore;
}
public Object[] translate(String... args) {
// Add all elements to a list
final ArrayList<String> toTranslate = new ArrayList<>();
toTranslate.add(name);
toTranslate.addAll(lore);
// Set starting index to 0
int index = 0;
// While iterator next value exists/isn't null
final Iterator<String> iterator = toTranslate.iterator();
while (!iterator.hasNext() || iterator.next() == null) {
// Get the current line
final String currentLine = toTranslate.get(index);
// Replace with the corresponding varargs index
toTranslate.set(index, currentLine.replace("{" + index + "}", args[index]));
// Increment the index
index++;
}
return new Object[]{toTranslate.get(0), toTranslate.subList(1, toTranslate.size())};
}
}

View file

@ -15,7 +15,7 @@ public class LocaleFileManager {
public String getString(String key) {
if (!file.exists()) return key;
try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
final YamlConfig yamlConfig = YamlConfig.load(inputStream);
final YamlConfig yamlConfig = new YamlConfig(inputStream);
return yamlConfig.getString(key);
} catch (IOException e) {
return key;
@ -44,7 +44,7 @@ public class LocaleFileManager {
public YamlConfig getYamlFile() {
if (yamlFile == null) {
try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
yamlFile = YamlConfig.load(inputStream);
yamlFile = new YamlConfig(inputStream);
} catch (IOException ignored) {
return null;
}

View file

@ -55,3 +55,8 @@ gui:
- "{0}"
- "§7§oGet through the values"
- "§7§oby left or right clicking."
bungeecord:
name: "Bungeecord Transfer"
lore:
- "§7§oGet through the values"
- "§7§oby left or right clicking."

View file

@ -31,15 +31,11 @@ gui:
admin:
name: "Panel d''administration"
lore:
length: 1
content:
- "Configurez et gérez Nicko."
- "Configurez et gérez Nicko."
settings:
name: "Paramètres"
lore:
length: 1
content:
"Configurez votre expérience."
- "Configurez votre expérience."
change_name:
name: "Changer le §6pseudo"
change_skin:
@ -52,9 +48,13 @@ gui:
- "Supprime complètement votre déguisement."
settings:
language:
name: "Language"
name: "Langage"
lore:
# The language values will be filled at {0}
- "{0}"
- "§7§oParcourez les valeurs"
- "§7§oavec un clique gauche/droit."
bungeecord:
name: "Bungeecord Transfer"
lore:
- "§7§oGet through the values"
- "§7§oby left or right clicking."

View file

@ -12,10 +12,9 @@ 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.ItemTranslation;
import xyz.atnrch.nicko.i18n.Locale;
import java.util.List;
public class I18NLoreTest {
private static NickoBukkit plugin;
private static PlayerMock player;
@ -35,8 +34,9 @@ public class I18NLoreTest {
@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);
final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.Home.ADMIN);
System.out.println("name = " + translation.getName());
System.out.println("lore = " + translation.getLore());
}
@AfterAll