| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.common.i18n; |
| |
| import java.text.DecimalFormat; |
| import java.util.Locale; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class I18nUtil. |
| */ |
| public class I18nUtil { |
| |
| /** |
| * Formats the enum literal. |
| * |
| * @param i18nService the i18n service |
| * @param value the value |
| * @param locale the locale |
| * @return the string |
| */ |
| public static String translateEnum(II18nService i18nService, Object value, |
| Locale locale) { |
| Enum<?> enumx = (Enum<?>) value; |
| String i18nKey = getI18nKey(enumx.getClass().getName(), enumx); |
| String result = null; |
| if (i18nService != null) { |
| result = i18nService.getValue(i18nKey, locale); |
| } |
| |
| if (result == null || result.trim().equals("")) { |
| result = enumx.name(); |
| } |
| |
| return result; |
| } |
| |
| /** |
| * Formats the boolean value. |
| * |
| * @param i18nService the i18n service |
| * @param value the value |
| * @param locale the locale |
| * @return the string |
| */ |
| public static String translateBoolean(II18nService i18nService, |
| Boolean value, Locale locale) { |
| if (i18nService == null) { |
| return ""; |
| } |
| String result = i18nService.getValue(Boolean.class.getName() + "." |
| + value.toString(), locale); |
| |
| if (result == null || result.trim().equals("")) { |
| result = value.toString(); |
| } |
| |
| return result; |
| } |
| |
| /** |
| * Returns the i18n key for the enum and its literal. |
| * |
| * @param enumName the enum name |
| * @param literal the literal |
| * @return the i18n key |
| */ |
| public static String getI18nKey(String enumName, Enum<?> literal) { |
| return enumName + "." + literal.name(); |
| } |
| |
| /** |
| * Returns the i18n key for the enum and its literal. |
| * |
| * @param enumX the enum x |
| * @return the i18n key |
| */ |
| public static String getI18nKey(Enum<?> enumX) { |
| return getI18nKey(enumX.getClass().getName(), enumX); |
| } |
| |
| /** |
| * Returns the i18nImageKey for the Enum name and literal. |
| * |
| * @param enumName the enum name |
| * @param literal the literal |
| * @return the image i18n key |
| */ |
| public static String getImageI18nKey(String enumName, Enum<?> literal) { |
| return enumName + "." + literal.name() + ".image"; |
| } |
| |
| /** |
| * Returns the image key for the given label key. |
| * |
| * @param i18nLabelKey the i18n label key |
| * @return the image key |
| */ |
| public static String getImageKey(String i18nLabelKey) { |
| String iconKey = i18nLabelKey + ".image"; |
| return iconKey; |
| } |
| |
| /** |
| * Translates number values to their locale representation. |
| * |
| * @param i18nService the i18n service |
| * @param number the number |
| * @param locale the locale |
| * @return the string |
| */ |
| public static String translateNumber(II18nService i18nService, |
| Number number, Locale locale) { |
| |
| if (number instanceof Short || number instanceof Integer |
| || number instanceof Byte || number instanceof Long) { |
| return DecimalFormat.getIntegerInstance(locale).format(number); |
| } else { |
| return DecimalFormat.getNumberInstance(locale).format(number); |
| } |
| } |
| } |