Skip to content

Commit

Permalink
More adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
MCMDEV committed Sep 19, 2021
1 parent a992fba commit b57ff5c
Show file tree
Hide file tree
Showing 32 changed files with 562 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .idea/artifacts/i18n_paper.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 71 additions & 22 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.translation.TranslationRegistry;

import java.util.Locale;
import java.util.Set;

public class AdventureTranslationConsumer implements TranslationConsumer {

private final GlobalTranslator globalTranslator = GlobalTranslator.get();
private final TranslationRegistry adventureRegistry = TranslationRegistry.create(Key.key("i18n", "main"));

@Override
public void register(Set<TranslationRepository> repositories) {
GlobalTranslator globalTranslator = GlobalTranslator.get();

repositories.forEach(translationRepository -> {
TranslationRegistry adventureRegistry = TranslationRegistry.create(Key.key("i18n", translationRepository.getIdentifier()));
translationRepository.getTranslations().forEach(translationEntry -> {
adventureRegistry.register(translationEntry.getKey(), translationEntry.getLocale(), translationEntry.getMessageFormat());
});
repositories.forEach(translationRepository ->
translationRepository.getTranslations().forEach(translationEntry ->
adventureRegistry.register(translationEntry.getKey(),
translationEntry.getLocale(),
translationEntry.getMessageFormat()
)
)
);
globalTranslator.addSource(adventureRegistry);
}

globalTranslator.addSource(adventureRegistry);
});
@Override
public void unregister(Set<TranslationRepository> repositories) {
repositories.forEach(translationRepository ->
translationRepository.getTranslations().forEach(translationEntry ->
adventureRegistry.unregister(translationEntry.getKey())));
globalTranslator.removeSource(adventureRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface TranslationConsumer {

void register(Set<TranslationRepository> repositories);

void unregister(Set<TranslationRepository> repositories);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface TranslationManager {

void registerToPlatforms();

void unregisterFromPlatforms();

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public Set<TranslationRepository> getTranslationRepositories() {

@Override
public void registerToPlatforms() {
translationConsumers.forEach(translationConsumer -> {
translationConsumer.register(translationRepositories);
});
translationConsumers.forEach(translationConsumer -> translationConsumer.register(translationRepositories));
}

@Override
public void unregisterFromPlatforms() {
translationConsumers.forEach(translationConsumer -> translationConsumer.unregister(translationRepositories));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.helixdevs.i18n.common.command;

import de.helixdevs.i18n.common.command.api.Command;
import de.helixdevs.i18n.common.command.api.Sender;
import de.helixdevs.i18n.common.platform.I18nPlatform;

public class CommandReloadtranslations extends Command {

private final I18nPlatform i18nPlatform;

public CommandReloadtranslations(I18nPlatform i18nPlatform) {
super("reloadtranslations", "i18n.reloadtranslations");
this.i18nPlatform = i18nPlatform;
}

@Override
public void execute(Sender sender, String[] arguments) {
i18nPlatform.reload();
sender.sendMessage("§aTranslations reloaded.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.helixdevs.i18n.common.command.api;

public abstract class Command {

private final String label;
private final String permission;

public Command(String label, String permission) {
this.label = label;
this.permission = permission;
}

public String getLabel() {
return label;
}

public String getPermission() {
return permission;
}

public abstract void execute(Sender sender, String[] arguments);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.helixdevs.i18n.common.command.api;

public interface Sender {

void sendMessage(String message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.helixdevs.i18n.api.TranslationManager;
import de.helixdevs.i18n.api.TranslationRepository;
import de.helixdevs.i18n.common.TranslationManagerImpl;
import de.helixdevs.i18n.common.command.CommandReloadtranslations;
import de.helixdevs.i18n.common.repositories.FilesTranslationRepository;

import java.nio.file.Paths;
Expand All @@ -18,6 +19,11 @@ public I18nPlatform(I18nPlugin plugin) {
}

public void enable() {
loadTranslations();
loadCommands();
}

private void loadTranslations() {
HashSet<TranslationRepository> repositories = new HashSet<>();
repositories.add(new FilesTranslationRepository(Paths.get("i18n_repo")));

Expand All @@ -31,6 +37,20 @@ public void enable() {
);
}

public void loadCommands() {
plugin.registerCommand(new CommandReloadtranslations(this));
}

public void unloadTranslations() {
this.translationManager.unregisterFromPlatforms();
}

public void reload() {
loadCommands();
unloadTranslations();
}

public void disable() {
unloadTranslations();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.helixdevs.i18n.common.platform;

import de.helixdevs.i18n.api.TranslationConsumer;
import de.helixdevs.i18n.common.command.api.Command;

import java.util.List;
import java.util.Set;
Expand All @@ -15,4 +16,6 @@ public interface I18nPlugin {
Set<TranslationConsumer> getConsumers();

Logger getLogger();

void registerCommand(Command command);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package de.helixdevs.i18n.common.repositories;

import com.sun.istack.internal.Nullable;
import de.helixdevs.i18n.api.TranslationEntry;
import de.helixdevs.i18n.api.TranslationRepository;

Expand Down Expand Up @@ -103,7 +102,7 @@ public Set<TranslationEntry> getTranslations() {
* @return a locale
* @since 4.0.0
*/
private static @Nullable Locale parseLocale(final String string) {
private static Locale parseLocale(final String string) {
final String[] segments = string.split("_", 3); // language_country_variant
final int length = segments.length;
if(length == 1) {
Expand Down
Loading

0 comments on commit b57ff5c

Please sign in to comment.