LocaleAPI

What is it for? Developers using this API can provide the ability to simultaneously use multiple languages. For example. On your server there are players from different countries and they understand only their language. If there is a localization file in which there will be messages corresponding to the client language chosen by them, they will see the messages of your plugin in that language.
Suggestions for improving the code on GitHub are also accepted.

For server admin

All localization files are saved in the following path, where “pluginid” is the directory for the localization files of a particular plugin. ./{ConfigDir}/localeapi/{pluginid}/[LocaleFiles]
This only applies to plugins that use LocaleAPI.
You can add translation files you need to the plugin localization directory.

For developers

The localization file name must be in the format en-US, ru-RU, etc. Select the file type depending on which configuration you prefer to choose.

How to use.

Demo plugin - https://github.com/SawFowl/LocaleTestPlugin

Plugin code example.
@Plugin(id = "pluginid",
	name = "PluginName",
	version = "version",
	authors = "authors",
	dependencies = {
		@Dependency(id = "localeapi", optional = true)
	})
public class PluginName {
	// Get API
	private static LocaleAPI localeAPI;
	public LocaleAPI getLocaleAPI() {
		return localeAPI;
	}

	@Listener
	public void onPostInitialization(GamePostInitializationEvent event) {
		localeAPI = LocaleAPIMain.getInstance().getAPI();
		localeAPI.saveLocales(this);
		//		       ^^ Main class or pluginid
		// Send message
		CommandSpec command = CommandSpec.builder()
    	        .executor((src, args) -> {
    	        	src.sendMessage(getOrDefaultLocale(src.getLocale()).getString("message-key"));
    	            return CommandResult.success();
    	        })
    	        .build();
		Sponge.getCommandManager().register(this, command, "command");
	}

	// Get locale. Similarly for Sponge configurations(HoconLocaleUtil,  JsonLocaleUtil,  YamlLocaleUtil). These methods are optional. Used to simplify access to the localization API.
	public Map<Locale, LocaleUtil> getLocales() {
		return localeAPI.getLocalesMap("pluginid");
		//				   ^^ Main class or pluginid
	}
	public LocaleUtil getLocale(Locale locale) {
		return getLocales().get(locale);
	}
	public LocaleUtil getDefaultLocale() {
		return getLocales().get(localeAPI.getDefaultLocale());
	}
	public LocaleUtil getOrDefaultLocale(Locale locale) {
		return getLocales().getOrDefault(locale, getDefaultLocale());
	}
}
Gradle
repositories {
    ...
    maven { 
        name = "jitpack"
        url = 'https://jitpack.io' 
    }
}
dependencies {
    ...
    implementation 'com.github.Mr-Krab:LocaleAPI:1.0'
}

Category: Developer Tools

Published on Dec 15, 2019

views

stars

watchers

total downloads

Licensed under GNU General Public License (GPL)

Promoted Versions

Pages

Members