Skip to content

Commit

Permalink
branding TinyTranslations
Browse files Browse the repository at this point in the history
  • Loading branch information
CubBossa committed Jan 4, 2024
1 parent 246c202 commit 2b347de
Show file tree
Hide file tree
Showing 58 changed files with 378 additions and 402 deletions.
147 changes: 74 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,42 +62,43 @@ Make sure to use the latest version.
### Shading
When shading, it is highly recommended to relocate the resource within your plugin.
This assures that no other plugin loads outdated Translations classes before your
plugin can load the latest classes. Occurring errors would potentially disable your plugin on startup.
plugin can load the latest classes. Occurring errors would potentially disable your plugin on startup.

```XML
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>de.cubbossa.translations</pattern>
<shadedPattern>[yourpluginpath].libs.translations</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>de.cubbossa.tinytranslationsde.cubbossa.tinytranslations</pattern>
<shadedPattern>[yourpluginpath].libs.translations</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```

or with gradle:
```groovy
tasks.shadowJar {
minimize()
relocate 'de.cubbossa.translations', '[yourpluginpath].libs.translations'
relocate 'de.cubbossa.tinytranslations', '[yourpluginpath].libs.translations'
}
```

Expand Down Expand Up @@ -179,58 +180,58 @@ embedded in other messages.
## Setup

```Java
import de.cubbossa.translations.MessageBuilder;
import de.cubbossa.tinytranslations.MessageBuilder;

class Messages {
public static final Message PREFIX = new MessageBuilder("prefix")
.withDefault("<gradient:#ff0000:#ffff00:#ff0000>My Awesome Plugin</gradient>: ")
.build();
public static final Message NO_PERM = new MessageBuilder("no_perm")
.withDefault("<red>No permissions!</red>")
.build();
public static final Message PREFIX = new MessageBuilder("prefix")
.withDefault("<gradient:#ff0000:#ffff00:#ff0000>My Awesome Plugin</gradient>: ")
.build();
public static final Message NO_PERM = new MessageBuilder("no_perm")
.withDefault("<red>No permissions!</red>")
.build();
}


class ExamplePlugin extends JavaPlugin {

Translations translations;

public void onEnable() {
// Enable Framework
TranslationsFramework.enable(new File(getDataFolder(), "/.."));
// create a Translations instance for your plugin
translations = TranslationsFramework.application("MyPlugin");

// define the storage types for your plugins locale
translations.setMessageStorage(new PropertiesMessageStorage(getLogger(), new File(getDataFolder(), "/lang/")));
translations.setStyleStorage(new PropertiesStyleStorage(new File(getDataFolder(), "/lang/styles.properties")));

// register all your messages to your Translations instance
// a message cannot be translated without a Translations instance, which works as
// translator.
translations.addMessages(messageA, messageB, messageC);
translations.addMessage(messageD);
// just load all public static final messages declared in Messages.class
translations.addMessages(TranslationsFramework.messageFieldsFromClass(Messages.class));

// They will not overwrite pre-existing values.
// You only need to save values that you assigned programmatically, like from a
// message builder. You can also create a de.properties resource and save it as file instead.
// Then there is no need to write the german defaults to file here.
translations.saveLocale(Locale.ENGLISH);
translations.saveLocale(Locale.GERMAN);

// load all styles and locales from file. This happens for all parent translations,
// so all changes to the global styles and translations will apply too.
translations.loadStyles();
translations.loadLocales();
}

public void onDisable() {
// close open Translations instance
translations.close();
TranslationsFramework.disable();
}
Translations translations;

public void onEnable() {
// Enable Framework
TranslationsFramework.enable(new File(getDataFolder(), "/.."));
// create a Translations instance for your plugin
translations = TranslationsFramework.application("MyPlugin");

// define the storage types for your plugins locale
translations.setMessageStorage(new PropertiesMessageStorage(getLogger(), new File(getDataFolder(), "/lang/")));
translations.setStyleStorage(new PropertiesStyleStorage(new File(getDataFolder(), "/lang/styles.properties")));

// register all your messages to your Translations instance
// a message cannot be translated without a Translations instance, which works as
// translator.
translations.addMessages(messageA, messageB, messageC);
translations.addMessage(messageD);
// just load all public static final messages declared in Messages.class
translations.addMessages(TranslationsFramework.messageFieldsFromClass(Messages.class));

// They will not overwrite pre-existing values.
// You only need to save values that you assigned programmatically, like from a
// message builder. You can also create a de.properties resource and save it as file instead.
// Then there is no need to write the german defaults to file here.
translations.saveLocale(Locale.ENGLISH);
translations.saveLocale(Locale.GERMAN);

// load all styles and locales from file. This happens for all parent translations,
// so all changes to the global styles and translations will apply too.
translations.loadStyles();
translations.loadLocales();
}

public void onDisable() {
// close open Translations instance
translations.close();
TranslationsFramework.disable();
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>de.cubbossa</groupId>
<artifactId>Translations</artifactId>
<artifactId>TinyTranslations</artifactId>
<version>4.0.0</version>

<name>Translations</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.cubbossa.translations;

import de.cubbossa.translations.annotation.AppPathPattern;
import de.cubbossa.translations.annotation.AppPattern;
import de.cubbossa.translations.persistent.MessageStorage;
import de.cubbossa.translations.persistent.StyleStorage;
import de.cubbossa.translations.util.DefaultResolvers;
package de.cubbossa.tinytranslations;

import de.cubbossa.tinytranslations.annotation.AppPathPattern;
import de.cubbossa.tinytranslations.annotation.AppPattern;
import de.cubbossa.tinytranslations.nanomessage.TranslationsPreprocessor;
import de.cubbossa.tinytranslations.persistent.MessageStorage;
import de.cubbossa.tinytranslations.persistent.StyleStorage;
import de.cubbossa.tinytranslations.nanomessage.DefaultResolvers;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.audience.Audience;
Expand All @@ -22,13 +23,13 @@

@Getter
@Setter
public class AppTranslations implements Translations {
public class AppTranslator implements Translator {

private static final MiniMessage MM = MiniMessage.miniMessage();

private final Translations parent;
private final Translator parent;
private final @AppPattern String name;
private final Map<String, Translations> children;
private final Map<String, Translator> children;

private TagResolver styleResolverCache = null;

Expand All @@ -44,7 +45,7 @@ public class AppTranslations implements Translations {
private TagResolver defaultResolvers;
private TranslationsPreprocessor preprocessor;

public AppTranslations(Translations parent, String name) {
public AppTranslator(Translator parent, String name) {
this.parent = parent;
this.name = name;

Expand All @@ -57,7 +58,7 @@ public AppTranslations(Translations parent, String name) {
this.messageSet = new HashMap<>() {
@Override
public Message put(String key, Message value) {
value.setTranslations(AppTranslations.this);
value.setTranslations(AppTranslator.this);
return super.put(key, value);
}
};
Expand Down Expand Up @@ -116,19 +117,19 @@ public void remove(String application) {
}

@Override
public Translations fork(String name) {
public Translator fork(String name) {
if (children.containsKey(name)) {
throw new IllegalArgumentException("Another fork with name '" + name + "' already exists.");
}

Translations child = new AppTranslations(this, name);
Translator child = new AppTranslator(this, name);
children.put(name, child);
return child;
}

@Override
public Translations forkWithStorage(String name) {
Translations child = fork(name);
public Translator forkWithStorage(String name) {
Translator child = fork(name);
child.setMessageStorage(messageStorage);
child.setStyleStorage(styleStorage);
return child;
Expand Down Expand Up @@ -165,7 +166,7 @@ public Component process(Message message, Locale locale) {
raw = message.getDictionary().get(new Locale(locale.getLanguage()));
}
if (raw == null) {
raw = message.getDictionary().get(TranslationsFramework.DEFAULT_LOCALE);
raw = message.getDictionary().get(TinyTranslations.DEFAULT_LOCALE);
}
if (raw == null) {
raw = "<no-translation-found:" + message.getNamespacedKey() + ">";
Expand Down Expand Up @@ -203,7 +204,7 @@ private TagResolver getStylesResolver() {
}
Map<String, TagResolver> styles = new HashMap<>();

Translations t = this;
Translator t = this;
while (t != null) {
t.getStyleSet().forEach((key, value) -> {
if (styles.containsKey(key)) return;
Expand Down Expand Up @@ -270,8 +271,8 @@ private TagResolver getMessageResolver(Locale locale) {
return parent.getMessageInParentTree(key);
}

private @Nullable Translations getTranslationsByNamespace(@AppPathPattern String namespace) {
Translations translations = this;
private @Nullable Translator getTranslationsByNamespace(@AppPathPattern String namespace) {
Translator translator = this;
String[] split = namespace.split("\\.");
Queue<String> path = new LinkedList<>(List.of(split));

Expand All @@ -280,30 +281,30 @@ private TagResolver getMessageResolver(Locale locale) {

while (!path.isEmpty()) {
String childName = path.poll();
translations = children.get(childName);
if (translations == null) {
translator = children.get(childName);
if (translator == null) {
return null;
}
}
return translations;
return translator;
}

@Override
public @Nullable Message getMessageByNamespace(@AppPathPattern String namespace, String key) {
if (parent != null) {
return parent.getMessageByNamespace(namespace, key);
}
Translations translations = getTranslationsByNamespace(namespace);
return translations == null ? null : translations.getMessageInParentTree(key);
Translator translator = getTranslationsByNamespace(namespace);
return translator == null ? null : translator.getMessageInParentTree(key);
}

@Override
public @Nullable MessageStyle getStyleByNamespace(@AppPathPattern String namespace, String key) {
if (parent != null) {
return parent.getStyleByNamespace(namespace, key);
}
Translations translations = getTranslationsByNamespace(namespace);
return translations == null ? null : translations.getStyleInParentTree(key);
Translator translator = getTranslationsByNamespace(namespace);
return translator == null ? null : translator.getStyleInParentTree(key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubbossa.translations;
package de.cubbossa.tinytranslations;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -36,8 +36,8 @@ public Collection<TagResolver> getResolvers() {
}

@Override
public void setTranslations(@NotNull Translations translations) {
message.setTranslations(translations);
public void setTranslations(@NotNull Translator translator) {
message.setTranslations(translator);
}

@Override
Expand Down Expand Up @@ -78,7 +78,7 @@ public String getNamespacedKey() {
}

@Override
public Translations getTranslations() {
public Translator getTranslations() {
return message.getTranslations();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubbossa.translations;
package de.cubbossa.tinytranslations;

public class GlobalMessages {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubbossa.translations;
package de.cubbossa.tinytranslations;

import java.util.LinkedList;
import java.util.List;
Expand Down
Loading

0 comments on commit 2b347de

Please sign in to comment.