Skip to content

Commit

Permalink
Merge pull request #24 from Paul2708/development
Browse files Browse the repository at this point in the history
Add language loading by file, ...
  • Loading branch information
Paul2708 committed Sep 28, 2019
2 parents 2d64dde + fd31b69 commit 06267f4
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _Make sure that you completed the following actions to submitting this PR._
* [ ] I have created a feature branch.
* [ ] My changes have been committed.
* [ ] I have pushed my changes to the branch.
* [ ] Running `mvn checkstyle:checkstyle` doesn't fail.
* [ ] Running `mvn checkstyle:check` doesn't fail.

## Title
_Give your PR a short title summarizing the patch, bug fix or feature._
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ jdk: openjdk8

script:
- mvn clean install
- mvn checkstyle:checkstyle
- mvn checkstyle:check
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Just add the following repository and dependency to your Maven project.
<dependency>
<groupId>com.github.Paul2708</groupId>
<artifactId>simple-commands</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
```

Expand All @@ -51,7 +51,7 @@ If you don't use a build tool like Maven or Gradle, you can download the latest
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-core</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
```

Expand Down Expand Up @@ -141,7 +141,7 @@ public void test(Player sender, Player target, int yHeight) {
I think, you can spot the differences and the boilerplate by spigot.

## Contribution
[Here](https://github.com/Paul2708/simple-commands/issues/2) is a list of TODOs, ideas and planned features.
[Here](https://github.com/Paul2708/simple-commands/issues) is a list of TODOs, ideas and planned features.
Maybe you can help and implement a new feature.

If you want to contribute, just clone the project, switch to a feature branch and open a [pull request](https://github.com/Paul2708/simple-commands/pulls).
Expand Down
4 changes: 2 additions & 2 deletions arguments/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>simple-commands</artifactId>
<groupId>de.paul2708</groupId>
<version>0.2.1</version>
<version>0.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-language</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
<!-- Spigot -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
public final class BooleanArgument implements CommandArgument<Boolean> {

/**
* Valid arguments that will be evaluated to <ocde>true</ocde>.
* Valid arguments that will be evaluated to <code>true</code>.
*/
public static final String[] TRUE_KEYS = new String[] {
"true", "yes", "y", "ja", "wahr", "correct"
};

/**
* Valid arguments that will be evaluated to <ocde>false</ocde>.
* Valid arguments that will be evaluated to <code>false</code>.
*/
public static final String[] FALSE_KEYS = new String[] {
"false", "no", "n", "nein", "falsch", "negative"
Expand Down
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>simple-commands</artifactId>
<groupId>de.paul2708</groupId>
<version>0.2.1</version>
<version>0.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-arguments</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
<!-- Spigot -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.UUID;

/**
Expand All @@ -17,7 +18,7 @@
*/
public final class DefaultLanguageSelector implements LanguageSelector {

// TODO: Check non-existing locales
private String directory;

private final Map<UUID, Locale> languages;
private Locale consoleLanguage;
Expand All @@ -30,6 +31,20 @@ public DefaultLanguageSelector() {
this.consoleLanguage = LanguageProvider.DEFAULT_LOCALE;
}

/**
* Load the language files from a certain path.
*
* @param path directory of all <code>message_XX.properties</code>, relative to spigot.jar
*/
@Override
public void loadFromFile(String path) throws MissingResourceException {
if (directory != null) {
throw new IllegalStateException("File aready set.");
}

this.directory = path;
}

/**
* Select the language for a given command sender.
* The language will be used to sent translated command information like invalid usage.
Expand All @@ -43,9 +58,9 @@ public void select(CommandSender sender, Locale locale) {
if (sender instanceof Player) {
Player player = (Player) sender;

languages.put(player.getUniqueId(), locale);
languages.put(player.getUniqueId(), provideLanguage(locale).getLocale());
} else {
this.consoleLanguage = locale;
this.consoleLanguage = provideLanguage(locale).getLocale();
}
}

Expand All @@ -57,7 +72,7 @@ public void select(CommandSender sender, Locale locale) {
*/
@Override
public void sendMessage(CommandSender sender, MessageResource resource) {
sender.sendMessage(LanguageProvider.of(get(sender)).translate(resource));
sender.sendMessage(provideLanguage(get(sender)).translate(resource));
}

/**
Expand All @@ -69,7 +84,7 @@ public void sendMessage(CommandSender sender, MessageResource resource) {
*/
@Override
public String translate(CommandSender sender, MessageResource resource) {
return LanguageProvider.of(get(sender)).translate(resource);
return provideLanguage(get(sender)).translate(resource);
}

/**
Expand All @@ -90,4 +105,12 @@ public Locale get(CommandSender sender) {
return consoleLanguage;
}
}

private LanguageProvider provideLanguage(Locale locale) {
if (directory == null) {
return LanguageProvider.of(locale);
} else {
return LanguageProvider.of(directory, locale);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.paul2708.commands.core.language;

import de.paul2708.commands.core.annotation.Command;
import de.paul2708.commands.language.MessageResource;
import org.bukkit.command.CommandSender;

Expand All @@ -14,6 +13,14 @@
*/
public interface LanguageSelector {

/**
* Load the language files from a certain path.
* If the directory doesn't exist, the default resources will be created and loaded there.
*
* @param path directory of all <code>message_XX.properties</code>, relative to spigot.jar
*/
void loadFromFile(String path);

/**
* Select the language for a given command sender.
* The language will be used to sent translated command information like invalid usage.
Expand Down
4 changes: 2 additions & 2 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>simple-commands</artifactId>
<groupId>de.paul2708</groupId>
<version>0.2.1</version>
<version>0.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-core</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
<!-- Spigot -->
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion language/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>simple-commands</artifactId>
<groupId>de.paul2708</groupId>
<version>0.2.1</version>
<version>0.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ public interface LanguageProvider {
*/
String translate(MessageResource resource);

/**
* Get the locale used in {@link #of(String, Locale)}.
* If the locale doesn't exist, the default locale will be used.
*
* @return locale
*/
Locale getLocale();

/**
* Create a new instance to the given locale.
* If there is no resource bundle for the given locale, the {@link #DEFAULT_LOCALE} will be used instead.
*
* @param directory file path to the directory of all message properties
* @param locale locale
* @return new language provider instance
*/
static LanguageProvider of(String directory, Locale locale) {
return new LocaleLanguageProvider(directory, locale);
}

/**
* Create a new instance to the given locale.
* If there is no resource bundle for the given locale, the {@link #DEFAULT_LOCALE} will be used instead.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package de.paul2708.commands.language;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
Expand All @@ -11,15 +17,42 @@
*/
public final class LocaleLanguageProvider implements LanguageProvider {

private static final String PATH = "messages";
private static final String BUNDLE = "messages";

private static final String PREFIX_KEY = "prefix";

private static final String REPLACE_CHAR = "%";

private final ResourceBundle resourceBundle;
private ResourceBundle resourceBundle;
private final String prefix;

private Locale locale;

/**
* Create a new language provider based on a locale.
*
* @param directory absolute file path to the directory of all message properties
* @param locale locale
*/
LocaleLanguageProvider(String directory, Locale locale) {
Locale.setDefault(LanguageProvider.DEFAULT_LOCALE);

File file = new File(directory + "/" + LocaleLanguageProvider.BUNDLE + "_"
+ locale.getLanguage() + ".properties");

try (InputStream inputStream = new FileInputStream(file)) {
this.resourceBundle = new PropertyResourceBundle(inputStream);

this.locale = locale;
} catch (MissingResourceException | IOException e) {
this.resourceBundle = ResourceBundle.getBundle(LocaleLanguageProvider.BUNDLE, locale);

this.locale = resourceBundle.getLocale();
}

this.prefix = replaceColorCodes(resourceBundle.getString(LocaleLanguageProvider.PREFIX_KEY));
}

/**
* Create a new language provider based on a locale.
*
Expand All @@ -28,7 +61,9 @@ public final class LocaleLanguageProvider implements LanguageProvider {
LocaleLanguageProvider(Locale locale) {
Locale.setDefault(LanguageProvider.DEFAULT_LOCALE);

this.resourceBundle = ResourceBundle.getBundle(LocaleLanguageProvider.PATH, locale);
this.resourceBundle = ResourceBundle.getBundle(LocaleLanguageProvider.BUNDLE, locale);
this.locale = resourceBundle.getLocale();

this.prefix = replaceColorCodes(resourceBundle.getString(LocaleLanguageProvider.PREFIX_KEY));
}

Expand All @@ -54,6 +89,17 @@ public String translate(MessageResource resource) {
return MessageFormat.format(replaceColorCodes(message), arguments);
}

/**
* Get the locale used in {@link #of(String, Locale)}.
* If the locale doesn't exist, the default locale will be used.
*
* @return locale
*/
@Override
public Locale getLocale() {
return locale;
}

/**
* Replace all color codes to display it.
*
Expand Down

0 comments on commit 06267f4

Please sign in to comment.