Skip to content
This repository has been archived by the owner on Jul 4, 2022. It is now read-only.

Localization

Leonhard edited this page Sep 23, 2020 · 21 revisions

Localization

The SimplixCore localization system is designed to be as versatile as possible. With it, you can easily achieve a per player localization on your server (e.g. English messages for people from English speaking countries, Spanish for people from Spanish speaking countries, etc.) In opposition to the common YAML based localization techniques, SimplixCore uses property-based localization files which comes with great support for IDEs such as IntelliJ IDEA: IntelliJ

Dependencies required

The API of the localization system used in SimplixCore can be found in the simplixcore-common-api module and its implementation in the simplixcore-common-implementation module. No other dependencies than described in getting started are required.

Creating a LocalizationManager

First you have to create an instance of LocalizationManager using the LocalizationManagerFactory. You can obtain an instance of that using dependency injection:

@ApplicationModule("Example")
public final class LocalizationModule extends AbstractSimplixModule {  
  
  @Provides  
  @Private
  public LocalizationManager localizationManager(LocalizationManagerFactory factory) {  
    return factory.create(new File("plugins/Example/lang"));  
  }
   
}

The create method requires an instance of java.io.File which points to the directory where all properties files are located. Our new LocalizationManager will also be available for dependency injection since we created a method provider binding by using @Provides (this will only work in modules).

Notice: Don't forget to set the privacy level since you only want to let your own application use your LocalizationManager.

Usage

To localize a given String you need to call the localized method of LocalizationManager:

String localized = localizationManager.localized("test-string", Locale.GERMAN);

As you can see, you also need to pass the targeted Locale to the method.

Fallback strategies

Sometimes your translation may be incomplete. In such a case, SimplixCore has a fallback strategy:

  • Check if a translation is available for the fallback locale
  • If yes, use this translation otherwise return the fallback string

You can set your fallback locale using the mutator method fallbackLocale. The default fallback locale is Locale.ENGLISH. You can set your fallback string using the mutator method fallbackString. The default fallback string is N/A.