Class I18N

java.lang.Object
de.edgesoft.edgeutils.i18n.I18N

public abstract class I18N
extends java.lang.Object
i18n class with methods for everyday internationalization. Inspired by https://softwareengineering.stackexchange.com/questions/256806/best-approach-for-multilingual-java-enum

Legal stuff

Copyright 2010-2020 Ekkart Kleinod ekleinod@edgesoft.de

This file is part of edgeutils.

edgeutils is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

edgeutils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with edgeutils. If not, see http://www.gnu.org/licenses/.

Since:
0.13.0
Author:
Ekkart Kleinod
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String BUNDLE_NAME
    Resource bundle name pattern.
    static java.lang.String DELIMITER
    Resource key part delimiter.
    static java.lang.String REFERENCE_PREFIX
    Reference prefix.
  • Constructor Summary

    Constructors 
    Constructor Description
    I18N()  
  • Method Summary

    Modifier and Type Method Description
    static boolean containsKey​(java.lang.String theKey)
    Determines whether the given key is contained in this ResourceBundle or its parent bundles.
    static java.lang.String createKey​(java.lang.String... theKeyParts)
    Creates resource key from parts, delimited by DELIMITER.
    static java.lang.String getKey​(java.lang.Enum<?> theEnum)
    Returns the key of the given enum value.
    static java.util.Optional<java.lang.String> getText​(java.lang.Enum<?> theEnum, java.lang.Object... theArguments)
    Returns the text for the given enum value formatted with the text arguments.
    static java.util.Optional<java.lang.String> getText​(java.lang.String theKey, java.lang.Object... theArguments)
    Returns a text for the given key formatted with the text arguments.
    static java.lang.String getViewNodeKey​(java.lang.Object theController, java.lang.String theNodeID, ResourceType theResourceType)
    Returns the key for the view indicated by the view controller.
    static java.util.Optional<java.lang.String> getViewNodeText​(java.lang.Object theController, java.lang.String theNodeID, ResourceType theResourceType, java.lang.Object... theArguments)
    Returns the text for the view indicated by the view controller.
    static void init​(java.lang.Class<?> theRootClass)
    Initialize class with root class and default locale.
    static void init​(java.lang.Class<?> theRootClass, java.util.Locale theLocale)
    Initialize class with root class and locale.
    static void init​(java.lang.Class<?> theRootClass, java.util.Locale theLocale, org.apache.logging.log4j.Logger theLogger)
    Initialize class with root class, locale, and logger.
    static void setLocale​(java.util.Locale theLocale)
    Set locale.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • I18N

      public I18N()
  • Method Details

    • init

      public static void init​(java.lang.Class<?> theRootClass)
      Initialize class with root class and default locale.
      Parameters:
      theRootClass - root class
    • init

      public static void init​(java.lang.Class<?> theRootClass, java.util.Locale theLocale)
      Initialize class with root class and locale.
      Parameters:
      theRootClass - root class
      theLocale - current locale (null for default)
    • init

      public static void init​(java.lang.Class<?> theRootClass, java.util.Locale theLocale, org.apache.logging.log4j.Logger theLogger)
      Initialize class with root class, locale, and logger.
      Parameters:
      theRootClass - root class
      theLocale - current locale (null for default)
      theLogger - logger (null for none)
    • setLocale

      public static void setLocale​(java.util.Locale theLocale)
      Set locale.
      Parameters:
      theLocale - new locale (null for default)
    • containsKey

      public static boolean containsKey​(java.lang.String theKey)
      Determines whether the given key is contained in this ResourceBundle or its parent bundles.
      Parameters:
      theKey - the resource key
      Returns:
      true if the given key is contained in this ResourceBundle or its parent bundles; false otherwise.
      Throws:
      java.lang.NullPointerException - if key is null
    • createKey

      public static java.lang.String createKey​(java.lang.String... theKeyParts)
      Creates resource key from parts, delimited by DELIMITER.
      Parameters:
      theKeyParts - parts of the key
      Returns:
      resource key
    • getText

      public static java.util.Optional<java.lang.String> getText​(java.lang.String theKey, java.lang.Object... theArguments)
      Returns a text for the given key formatted with the text arguments.
      Parameters:
      theKey - key of the text
      theArguments - arguments for the text
      Returns:
      formatted text
    • getKey

      public static java.lang.String getKey​(java.lang.Enum<?> theEnum)
      Returns the key of the given enum value.
      Parameters:
      theEnum - enum value
      Returns:
      key
    • getText

      public static java.util.Optional<java.lang.String> getText​(java.lang.Enum<?> theEnum, java.lang.Object... theArguments)
      Returns the text for the given enum value formatted with the text arguments.
      Parameters:
      theEnum - enum value
      theArguments - arguments for the text
      Returns:
      text for the enum value
    • getViewNodeKey

      public static java.lang.String getViewNodeKey​(java.lang.Object theController, java.lang.String theNodeID, ResourceType theResourceType)
      Returns the key for the view indicated by the view controller. Originally, the node id was used by calling Node.getId() here, but the method should be usable for MenuItems as well which are no nodes for whatever reasons. TODO: If this should change in the future, use a node instead.
      Parameters:
      theController - view controller
      theNodeID - node
      theResourceType - resource types
      Returns:
      key
    • getViewNodeText

      public static java.util.Optional<java.lang.String> getViewNodeText​(java.lang.Object theController, java.lang.String theNodeID, ResourceType theResourceType, java.lang.Object... theArguments)
      Returns the text for the view indicated by the view controller.
      Parameters:
      theController - view controller
      theNodeID - node id
      theResourceType - resource types
      theArguments - arguments for the text
      Returns:
      text