Skip to content

Commit

Permalink
feat: implement plugin I18n
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 8, 2024
1 parent e556d8f commit 836d5ef
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 18 deletions.
3 changes: 3 additions & 0 deletions @Allay-ExamplePlugin-JS/lang/en_US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ejp:example_js_plugin_i18n_test": "JS plugin i18n test text"
}
1 change: 1 addition & 0 deletions @Allay-ExamplePlugin-JS/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function onLoad() {

export function onEnable() {
console.info("Example-Plugin-Js has been enabled!");
console.info(plugin.getPluginI18n().tr("ejp:example_js_plugin_i18n_test"));
a();
}

Expand Down
5 changes: 5 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Getter;
import lombok.Setter;
import org.allaymc.api.command.CommandRegistry;
import org.allaymc.api.i18n.I18n;
import org.allaymc.api.scheduler.Scheduler;
import org.allaymc.api.scheduler.TaskCreator;
import org.allaymc.api.server.Server;
Expand Down Expand Up @@ -79,6 +80,10 @@ public CommandRegistry getCommandRegistry() {
return this.getServer().getCommandRegistry();
}

public I18n getPluginI18n() {
return pluginContainer.i18n();
}

@Override
public boolean isValid() {
return enabled;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.allaymc.api.plugin;

import org.allaymc.api.i18n.I18n;

import java.nio.file.Path;

/**
Expand All @@ -11,11 +13,17 @@ public record PluginContainer(
Plugin plugin,
PluginDescriptor descriptor,
PluginLoader loader,
Path dataFolder
Path dataFolder,
I18n i18n
) {

public static PluginContainer createPluginContainer(Plugin plugin, PluginDescriptor descriptor, PluginLoader loader, Path dataFolder) {
var pluginContainer = new PluginContainer(plugin, descriptor, loader, dataFolder);
public static PluginContainer createPluginContainer(
Plugin plugin,
PluginDescriptor descriptor,
PluginLoader loader,
Path dataFolder,
I18n i18n) {
var pluginContainer = new PluginContainer(plugin, descriptor, loader, dataFolder, i18n);
plugin.setPluginContainer(pluginContainer);
return pluginContainer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void onLoad() {
@Override
public void onEnable() {
log.info("ExamplePlugin enabled!");
log.info(getPluginI18n().tr("ep:example_plugin_i18n_test"));
var server = Server.getInstance();
server.getEventBus().registerListener(serverEventListener);
server.getEventBus().registerListener(worldEventListener);
Expand Down
3 changes: 3 additions & 0 deletions Allay-ExamplePlugin/src/main/resources/lang/en_US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ep:example_plugin_i18n_test": "Plugin i18n test text"
}
4 changes: 2 additions & 2 deletions Allay-Server/src/main/java/org/allaymc/server/Allay.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import org.allaymc.server.entity.type.AllayEntityType;
import org.allaymc.server.eventbus.AllayEventBus;
import org.allaymc.server.gui.Dashboard;
import org.allaymc.server.i18n.AllayI18N;
import org.allaymc.server.i18n.AllayI18n;
import org.allaymc.server.i18n.AllayI18nLoader;
import org.allaymc.server.item.attribute.AllayVanillaItemAttributeRegistry;
import org.allaymc.server.item.enchantment.AllayEnchantmentRegistry;
Expand Down Expand Up @@ -194,7 +194,7 @@ public static void initAllayAPI() throws MissingImplementationException {
@VisibleForTesting
public static void initI18n() {
if (I18n.get() == null) {
AllayAPI.getInstance().bindI18n(new AllayI18N(new AllayI18nLoader(), Server.SETTINGS.genericSettings().language()));
AllayAPI.getInstance().bindI18n(new AllayI18n(new AllayI18nLoader(), Server.SETTINGS.genericSettings().language()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
* @author daoge_cmd
*/
@Slf4j
public class AllayI18N implements I18n {
public class AllayI18n implements I18n {

protected EnumMap<LangCode, Map<String, String>> langMap = new EnumMap<>(LangCode.class);
protected LangCode defaultLangCode;
protected I18nLoader i18NLoader;

public AllayI18N(I18nLoader i18NLoader, LangCode defaultLangCode) {
public AllayI18n(I18nLoader i18NLoader, LangCode defaultLangCode) {
this.i18NLoader = i18NLoader;
this.defaultLangCode = defaultLangCode;
setDefaultLangCode(defaultLangCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
public class AllayI18nLoader implements I18nLoader {
@Override
public Map<String, String> getLangMap(LangCode langCode) {
try (var input = Objects.requireNonNull(AllayI18nLoader.class.getResourceAsStream("/lang/" + langCode.name() + ".json"));) {
TypeToken<HashMap<String, String>> typeToken = new TypeToken<>() {
};
try (var input = Objects.requireNonNull(AllayI18nLoader.class.getResourceAsStream("/lang/" + langCode.name() + ".json"))) {
TypeToken<HashMap<String, String>> typeToken = new TypeToken<>() {};
byte[] bytes = input.readAllBytes();
return JSONUtils.fromLenient(new String(bytes, StandardCharsets.UTF_8),typeToken);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package org.allaymc.server.plugin.jar;

import com.google.gson.reflect.TypeToken;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.i18n.I18n;
import org.allaymc.api.i18n.I18nLoader;
import org.allaymc.api.i18n.LangCode;
import org.allaymc.api.i18n.TrKeys;
import org.allaymc.api.plugin.Plugin;
import org.allaymc.api.plugin.PluginContainer;
import org.allaymc.api.plugin.PluginDescriptor;
import org.allaymc.api.plugin.PluginException;
import org.allaymc.api.plugin.PluginLoader;
import org.allaymc.api.server.Server;
import org.allaymc.api.utils.JSONUtils;
import org.allaymc.server.i18n.AllayI18n;
import org.allaymc.server.i18n.AllayI18nLoader;
import org.allaymc.server.plugin.SimplePluginDescriptor;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static org.allaymc.api.plugin.PluginContainer.createPluginContainer;
import static org.allaymc.server.plugin.DefaultPluginSource.getOrCreateDataFolder;
Expand Down Expand Up @@ -69,7 +77,8 @@ public PluginContainer loadPlugin() {
return createPluginContainer(
pluginInstance,
descriptor, this,
getOrCreateDataFolder(descriptor.getName())
getOrCreateDataFolder(descriptor.getName()),
new AllayI18n(new JarPluginI18nLoader(), Server.SETTINGS.genericSettings().language())
);
}

Expand All @@ -85,6 +94,23 @@ protected Class<?> findMainClass() {
}
}

public class JarPluginI18nLoader implements I18nLoader {

@Override
public Map<String, String> getLangMap(LangCode langCode) {
try {
var str = Files.readString(jarFileSystem.getPath("lang/" + langCode.name() + ".json"));
TypeToken<HashMap<String, String>> typeToken = new TypeToken<>() {};
return JSONUtils.fromLenient(str,typeToken);
} catch (NoSuchFileException e) {
return Collections.emptyMap();
} catch (IOException e) {
log.error("Error while loading plugin language file", e);
return Collections.emptyMap();
}
}
}

public static class JarPluginLoaderFactory implements PluginLoaderFactory {

protected static final PathMatcher PATH_MATCHER = FileSystems.getDefault().getPathMatcher("glob:**.jar");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package org.allaymc.server.plugin.js;

import com.google.gson.reflect.TypeToken;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.i18n.I18nLoader;
import org.allaymc.api.i18n.LangCode;
import org.allaymc.api.plugin.PluginContainer;
import org.allaymc.api.plugin.PluginDescriptor;
import org.allaymc.api.plugin.PluginException;
import org.allaymc.api.plugin.PluginLoader;
import org.allaymc.api.server.Server;
import org.allaymc.api.utils.JSONUtils;
import org.allaymc.server.i18n.AllayI18n;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.allaymc.api.plugin.PluginContainer.createPluginContainer;
import static org.allaymc.server.plugin.DefaultPluginSource.getOrCreateDataFolder;
Expand All @@ -19,6 +30,7 @@
*
* @author daoge_cmd
*/
@Slf4j
public class JsPluginLoader implements PluginLoader {

@Getter
Expand Down Expand Up @@ -49,10 +61,27 @@ public PluginContainer loadPlugin() {
return createPluginContainer(
new JsPlugin(),
descriptor, this,
getOrCreateDataFolder(descriptor.getName())
getOrCreateDataFolder(descriptor.getName()),
new AllayI18n(new JsPluginI18nLoader(), Server.SETTINGS.genericSettings().language())
);
}

public class JsPluginI18nLoader implements I18nLoader {
@Override
public Map<String, String> getLangMap(LangCode langCode) {
try {
var str = Files.readString(pluginPath.resolve("lang").resolve(langCode.name() + ".json"));
TypeToken<HashMap<String, String>> typeToken = new TypeToken<>() {};
return JSONUtils.fromLenient(str,typeToken);
} catch (NoSuchFileException e) {
return Collections.emptyMap();
} catch (IOException e) {
log.error("Error while loading plugin language file", e);
return Collections.emptyMap();
}
}
}

public static class JsPluginLoaderFactory implements PluginLoaderFactory {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AllayI18nTest {
public static final String TEST_VANILLA_KEY = "minecraft:" + TEST_VANILLA_KEY_NO_NAMESPACE;
public static final String TEST_ALLAY_KEY_NO_NAMESPACE = "allay.starting";
public static final String TEST_ALLAY_KEY = "allay:" + TEST_ALLAY_KEY_NO_NAMESPACE;
static I18n translator = new AllayI18N(new AllayI18nLoader(), LangCode.en_US);
static I18n translator = new AllayI18n(new AllayI18nLoader(), LangCode.en_US);

@Test
void testTr() {
Expand Down

0 comments on commit 836d5ef

Please sign in to comment.