LocaleAPI - Locale API for Sponge plugins

This is a discussion topic for the Ore project, LocaleAPI. View the full project on Ore for downloads and more information.


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 - GitHub - SawFowl/LocaleTestPlugin: Test LocaleAPI.

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'
}

A new version has been released for LocaleAPI, it is available for download here.


Locale API for Sponge plugins.

Nice job.
I like the structure plugin->language->key (I’m using plugin->key->language).

Does src.getLocale() actually work now? Iirc when I wrote LangSwitch that was just returning the server locale. I haven’t looked at that in ages though… I think that was also the reason I added support for Nucleus Heisenberg (geo ip)

It was also important for me to be able to insert Sponge Text into the translations. That makes it easier to create a clickable word within a line of text in my opinion.

Keep up the good work :slight_smile:

See new version on GitHub
I think you will like it. I will make a release on the ORE later.
Demo plugin.

1 Like

A new version has been released for LocaleAPI, it is available for download here.


Release for Sponge API8.
Made a job with an API simpler. Using abstraction.
Removed the configuration file. The default language is determined by Sponge.
Now the plugin will track changes in localizations. You no longer need to reboot them yourself.
Added class for serialization ItemStack.