java.lang.Object
eu.cloudnetservice.common.language.I18n

public final class I18n extends Object
The main entry point for localization made in the CloudNet system. Language files can be registered in multiple ways to this registry. Multiple language files for the same language will be combined to one entry and can be translated. If multiple language files with the same language key are registered, the first registered language file will be used to translate the requested key.

Unregistering language files can be done via the class loader which must be given for message loading.

Since:
4.0
  • Field Details

    • MESSAGE_FORMAT

      private static final Pattern MESSAGE_FORMAT
    • LOGGER

      private static final Logger LOGGER
    • REGISTERED_ENTRIES

      private static final com.google.common.collect.SetMultimap<String,I18n.Entry> REGISTERED_ENTRIES
    • CURRENT_LANGUAGE

      private static final AtomicReference<String> CURRENT_LANGUAGE
  • Constructor Details

    • I18n

      private I18n()
  • Method Details

    • loadFromLangPath

      public static void loadFromLangPath(@NonNull @NonNull Class<?> clazzSource)
      Loads all language files which are located in the jar at given class source and registers them to the loader of the given class. All language files in the jar must be located in the lang/ directory and their extension must be properties. Subdirectories are ignored by this method.
      Parameters:
      clazzSource - the source which tries to register all language files.
      Throws:
      NullPointerException - if the given class source is null.
    • addLanguageFile

      public static void addLanguageFile(@NonNull @NonNull String lang, @NonNull @NonNull Path file, @NonNull @NonNull ClassLoader source)
      Registers the language properties file at the given path to the given language and loader source.
      Parameters:
      lang - the language to register the file to.
      file - the location of the language file to load.
      source - the class loader to which the requester belongs.
      Throws:
      NullPointerException - if either the given language, file or loader is null.
    • addLanguageFile

      public static void addLanguageFile(@NonNull @NonNull String lang, @NonNull @NonNull InputStream stream, @NonNull @NonNull ClassLoader source)
      Registers the language properties file which must be loadable from the given stream to the given language and loader source. This method uses utf-8 to decode the stream.
      Parameters:
      lang - the language to register the file to.
      stream - the stream from which the properties should get loaded.
      source - the class loader to which the requester belongs.
      Throws:
      NullPointerException - if either the given language, stream or loader is null.
    • addLanguageFile

      public static void addLanguageFile(@NonNull @NonNull String lang, @NonNull @NonNull Properties entries, @NonNull @NonNull ClassLoader source)
      Registers the language properties file to the given language and loader source.
      Parameters:
      lang - the language to register the file to.
      entries - the entries of the language file to register.
      source - the class loader to which the requester belongs.
      Throws:
      NullPointerException - if either the given language, entries or loader is null.
    • unregisterLanguageFiles

      public static void unregisterLanguageFiles(@NonNull @NonNull ClassLoader loader)
      Unregisters all language files which were registered by providing the given class loader.
      Parameters:
      loader - the loader to unregister the language files of.
      Throws:
      NullPointerException - if the given loader is null.
    • trans

      public static String trans(@NonNull @NonNull String messageKey, @NonNull @NonNull Object... args)
      Tries to translate the given message key using all currently registered language files for the language this registry currently uses. This method uses the first language entry which can translate the given key, ignoring all duplicate keys.

      This method will never return null. However, it does return a string which either indicates that no language files are registered for the current language, or that no registered entry is able to translate the given key.

      Parameters:
      messageKey - the key of the message to translate.
      args - the arguments for the translation.
      Throws:
      NullPointerException - if either the given key or argument array is null.
    • language

      @NonNull public static @NonNull String language()
      Get the current language to which each message will be translated.
      Returns:
      the current message of this manager.
    • knownLanguages

      @NonNull public static @NonNull Collection<String> knownLanguages()
      Gets all the names of the known languages to this translation manager.
      Returns:
      the names of all known languages.
    • language

      public static void language(@NonNull @NonNull String language)
      Sets the current message to which all messages should get translated. There is no check made if any message is registered for the given language.

      This method doesn't change to the given language silently if the language is not associated with a translation file.

      Parameters:
      language - the language this manager should use.
      Throws:
      NullPointerException - if the given language is null.