Table of Contents

Velocity Example

Please note that velocity is handled slightly different due to how their plugin API works. E.g. you need to implement the `Languaged` interface.

Maven Artifact

snippet.xml
<dependency>
    <groupId>de.themoep.utils</groupId>
    <artifactId>lang-velocity</artifactId>
    <version>check-for-latest</version>
    <scope>compile</scope>
</dependency>

Example

snippet.java
@Plugin(id = "myplugin", name = "MyPlugin", version = "1.0", authors = {"You"})
public class MyPlugin implements Languaged {
    private final Logger logger;
    private final File dataFolder;
    private LanguageManager lang;
 
    @Inject
    public MyPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataFolder) {
        this.logger = logger;
        this.dataFolder = dataFolder.toFile();
    }
 
    @Subscribe
    public void onProxyInitialization(ProxyInitializeEvent event) {    
        # Load LanguageManager. Either call that in onEnable or your config loading method.
        # Will automatically save and load any lang.<locale>.yml configs in the languages folder
        # Gets and saves language files to the folder "languages".
        # Default locale is "en"
        lang = new LanguageManager(this, "languages", "en");
 
        # Set the provider for the language of a certain sender
        # If null is returned then it will use the default locale
        # (This provider is the same as which is used by default)
        lang.setProvider(sender -> {
            if (sender instanceof ProxiedPlayer) {
                return ((ProxiedPlayer) sender).getLocale().getLanguage().replace('-', '_');
            }
            return null;
        });
    }
 
    @Override
    public String getName() {
        return getClass().getAnnotation(Plugin.class).name();
    }
 
    @Override
    public File getDataFolder() {
        return dataFolder;
    }
 
    @Override
    public Logger getLogger() {
        return logger;
    }
 
    /**
     * Get a message from a language config for a certain sender
     * @param sender        The sender to get the string for. (Language is based on this)
     * @param key           The language key in the config
     * @param replacements  An option array for replacements.
     *                (2n)-th will be the placeholder, (2n+1)-th the value. 
     *                Placeholders have to be surrounded by percentage signs: %placeholder%
     * @return The string from the config which matches the sender's language (or the default one) with the replacements replaced (or an error message, never null)
     */
    public String getMessage(CommandSender sender, String key, String... replacements) {
        return lang.getConfig(sender).get(key, replacements);
    }
}