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.

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. When you add a localization file, it will be downloaded automatically. If you make changes to existing files, they will also be automatically reloaded. The load/reload operation cannot be performed more than once every 10 seconds. This time limit has been added to eliminate the possibility of server performance degradation.

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. For the correct work of the plugin is necessary to generate a default localization file - en-US.

Test plugin -> https://github.com/SawFowl/LocaleTestPlugin/tree/API8
javadoc -> https://sawfowl.github.io/LocaleAPI/

@Plugin("pluginid")
public class Main {
	private Main instance;
	private Logger logger;
	private static LocaleService localeService;
	public LocaleService getLocaleService() {
		return localeService;
	}

	// Get API. Variant 1. This happens in event `ConstructPluginEvent`.
	// It's recommended if LocaleAPI is mandatory.
	@Listener
	public void onLocaleServisePostEvent(LocaleServiseEvent.Construct event) {
		instance = this;
		logger = LogManager.getLogger("PluginName");
		localeService = event.getLocaleService();
		testLocales();
	}

	// Get API. Variant 2. This happens in event `StartedEngineEvent<Server>`. 
	// It's recommended if LocaleAPI is optional.
	// In this case, you can specify another class as the event listener.
	@Listener
	public void onLocaleServisePostEvent(LocaleServiseEvent.Started event) {
		localeService = event.getLocaleService();
		testLocales();
	}

	public void testLocales() {
		if(!localeService.localesExist(instance)) {
			localeService.createPluginLocale(instance, ConfigTypes.HOCON, Locales.DEFAULT);
			//		                  ^^ Main class or "pluginid".
			getLocaleUtil(Locales.DEFAULT)
				.checkString("Your string for localization.", "Optional comment", "Path");
		}
		// Get message from locale. You can get and use the player's localization 'player.locale();'.
		// The boolean parameter defines what type of string serializer will be used.
		logger.info(getLocaleUtil(Locales.DEFAULT).getComponent(false, "Path"));
	}

	public AbstractLocaleUtil getLocaleUtil(Locale locale) {
		return localeService.getOrDefaultLocale(instance, locale);
	}
}
Gradle
repositories {
	...
	maven { 
		name = "JitPack"
		url 'https://jitpack.io' 
	}
}
dependencies {
	...
	implementation 'com.github.SawFowl:LocaleAPI:2.2.0'
}

Category: Developer Tools

Published on Feb 14, 2022

views

stars

watchers

total downloads

Promoted Versions

Pages

Members