Welcome to the Hangar Open Beta. Please report any issue you encounter on GitHub!
Avatar for AlphaOmega-IT

Localization System (API) One YAML File, endless Languages

Report AO18n?

Add the following dependency to your project's pom.xml file:


<dependencies>
   <dependency>
     <groupId>de.alphaomega-it.ao18n</groupId>
     <artifactId>AO18n</artifactId>
     <version>2.1</version>
   </dependency>
</dependencies>

Step 2: Initialize AO18n in Your JavaPlugin class Add the following method 'onLoad' to your class


import com.alphaomegait.ao18n.AO18n;

public class Plugin extends JavaPlugin {

  //Called when the plugin is being loaded.
  @Override
  public void onLoad() {

   // argument 1: class which does extend the JavaPlugin
   // argument 2: if the language file should be replaced in the plugin folder
   new AO18n(this, false);
  }
}

Step 2.1: Use it for Internationalization Since we initialized the Dependency you can use the classes as long the .yml format of the i18n.yml file is correct.


import de.alphaomegait.ao18n.I18n;
import org.bukkit.entity.Player;

// can be whatever class you want
public class Example {

  //Send the player a message based on the message-key in your i18n.yml file
  //It takes if available the player client language, if the language does not
  //exist. It will take the fallback/default language key.
  //.build().sendMessage() will send a message as string.
  //.build().sendMessageAsComponent() will send a message using the https://docs.advntr.dev/minimessage/index.html dependency as a Text Component
  public void sendHelloWorldMessage(
   final Player player
  ) {
   new I18n.Builder(
     "message-key",
     player
   ).hasPrefix(true)
    .build()
    .sendMessageAsComponent();
  }
}

Step 3: Configure Translations Create a translation file in the appropriate directory. By default, AO18n looks for the translation file in the src/main/resources/translations directory. Here's an example structure:

src └── main └── resources └── translations ├── i18n.yml

Step 3.1: Fill the Translation File Now you have to fill in the missing Language keys and Locales in the right format like below:


# This file contains the configuration settings for internationalization (i18n).
# Please make sure to update the following values according to your requirements.

# See https://wiki.atwoo.eu for more information.

defaultLocale: 'en'
translations:
  #Prefix has to be a key to make the function #showPrefix work
  prefix:
   de:
     - '<gray>Dies ist eine Nachricht</gray>'
   en:
     - '<gray>This is a message</gray>'
  message_key_2:
   de:
     - '<gray>Dies ist eine Nachricht 2</gray>'
     - '<gray>Dies ist eine Nachricht 3</gray>'
   en:
     - '<gray>This is a message 2</gray>'
     - '<gray>This is a message 3</gray>'
  message_key_3:
   de:
     - '<gray>Dies ist eine Nachricht 4</gray>'
     - '<gray>Dies ist eine Nachricht mit einem Platzhalter {0}</gray>'
   en:
     - '<gray>This is a message 4</gray>'
     - '<gray>This is a message with a placeholder {0}</gray>'
  message_key_4:
   de:
     - '<gray>{0}, {1}, {2} etc. Platzhalter...</gray>'
   en:
     - '<gray>{0}, {1}, {2} etc. Placeholder...</gray>'

Information

CategoryDeveloper Tools
Published onJuly 2, 2024
LicenseUnspecified
Download1
Stars0
Watchers0

Pinned Versions

Members