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.StringBUNDLE_NAMEResource bundle name pattern.static java.lang.StringDELIMITERResource key part delimiter.static java.lang.StringREFERENCE_PREFIXReference prefix. -
Constructor Summary
Constructors Constructor Description I18N() -
Method Summary
Modifier and Type Method Description static booleancontainsKey(java.lang.String theKey)Determines whether the givenkeyis contained in thisResourceBundleor its parent bundles.static java.lang.StringcreateKey(java.lang.String... theKeyParts)Creates resource key from parts, delimited byDELIMITER.static java.lang.StringgetKey(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.StringgetViewNodeKey(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 voidinit(java.lang.Class<?> theRootClass)Initialize class with root class and default locale.static voidinit(java.lang.Class<?> theRootClass, java.util.Locale theLocale)Initialize class with root class and locale.static voidinit(java.lang.Class<?> theRootClass, java.util.Locale theLocale, org.apache.logging.log4j.Logger theLogger)Initialize class with root class, locale, and logger.static voidsetLocale(java.util.Locale theLocale)Set locale.
-
Field Details
-
DELIMITER
public static final java.lang.String DELIMITERResource key part delimiter.- See Also:
- Constant Field Values
-
REFERENCE_PREFIX
public static final java.lang.String REFERENCE_PREFIXReference prefix.- See Also:
- Constant Field Values
-
BUNDLE_NAME
public static final java.lang.String BUNDLE_NAMEResource bundle name pattern.- See Also:
- Constant Field Values
-
-
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 classtheLocale- 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 classtheLocale- 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 givenkeyis contained in thisResourceBundleor its parent bundles.- Parameters:
theKey- the resourcekey- Returns:
trueif the givenkeyis contained in thisResourceBundleor its parent bundles;falseotherwise.- Throws:
java.lang.NullPointerException- ifkeyisnull
-
createKey
public static java.lang.String createKey(java.lang.String... theKeyParts)Creates resource key from parts, delimited byDELIMITER.- 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 texttheArguments- 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 valuetheArguments- 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 callingNode.getId()here, but the method should be usable forMenuItems as well which are no nodes for whatever reasons. TODO: If this should change in the future, use a node instead.- Parameters:
theController- view controllertheNodeID- nodetheResourceType- 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 controllertheNodeID- node idtheResourceType- resource typestheArguments- arguments for the text- Returns:
- text
-