blob: c54c886e3a2a314710d21562dd1a470aad41d8e2 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.xtext.messagedsl.internal;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
*/
public class BasicTranslator {
@SuppressWarnings("unused")
private static BasicTranslator INSTANCE = new BasicTranslator();
/**
* get the translator key for the class and item
* @param clazz
* @param item
* @return translator key
*/
public static String getTranslatorKey(String object, String item) {
return object+"."+item;
}
/**
* get the translator key for the class and item
* @param clazz
* @param item
* @return translator key
*/
public static String getTranslatorKey(Object object, String item) {
return getTranslatorCanonicalName(object)+"."+item;
}
/**
* get the translator key for the class and item
* @param clazz
* @param item
* @return translator key
*/
public static String getTranslatorKey(Class<?> clazz, String item) {
return getTranslatorCanonicalName(clazz)+"."+item;
}
/**
* @param object
* @return get the canonical class name used for the translator key
*/
private static String getTranslatorCanonicalName(Object object) {
return getTranslatorCanonicalName(object.getClass());
}
private final static String IGNORE_IMPL_PREFIX = "Impl";
private final static String IGNORE_IMPL_PACKAGE = ".impl.";
private final static String IGNORE_MESSAGE_PACKAGE_NAME = "Message.";
/**
* <ul>
* <li>Impl-prefix and impl-package part is removed</li>
* <li>Inner classes are handled with "normal" package separator</li>
* </ul>
* @param clazz the class
* @return get the canonical class name used for the translator key
*/
private static String getTranslatorCanonicalName(Class<?> clazz) {
String result = clazz.getCanonicalName();
if (result.endsWith(IGNORE_IMPL_PREFIX)) {
result = result.substring(0, result.length()-IGNORE_IMPL_PREFIX.length());
}
return result
.replace('$','.')
.replace(IGNORE_IMPL_PACKAGE,".")
.replace(IGNORE_MESSAGE_PACKAGE_NAME,".");
}
/**
* one locale definition for all DSL-UI instances running in this Eclipse instance
*/
private static Locale sLocale = Locale.getDefault();
private static ResourceBundle sResourceBundle = null;
/**
* @return the resource bundle with the translation property files of the DSL-UI-Bundle
*/
protected static ResourceBundle getResourceBundle() {
if ((sResourceBundle == null) || !sLocale.equals(Locale.getDefault())) {
sLocale = Locale.getDefault();
sResourceBundle = java.util.ResourceBundle.getBundle("i18n.I18N", sLocale, BasicTranslator.class.getClassLoader());
}
return sResourceBundle;
}
/**
* Get the translated documentation for the key
* @param i18nKey
* @param i18nKeyFallBack
* @param warning true, if the information about missing translation or documentation should be shown
* @return
*/
public final static String translate(String i18nKey, String i18nKeyFallBack, String defaultTranslation) {
i18nKey = i18nKey.replace('_', '.');
// --- get the translated documentation
try {
String result = getResourceBundle().getString(i18nKey);
if ((result instanceof String) && !result.isEmpty()) {
return result;
}
}
catch (MissingResourceException mre) {
}
if (i18nKeyFallBack != null) {
i18nKeyFallBack = i18nKeyFallBack.replace('_', '.');
}
try {
String result = getResourceBundle().getString(i18nKeyFallBack);
if ((result instanceof String) && !result.isEmpty()) {
return result;
}
}
catch (MissingResourceException mre) {
}
System.err.println("- missing translation: "+sLocale.toString()+" / "+i18nKey+" "+i18nKeyFallBack);
return defaultTranslation;
}
}