Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9487d8a2aba84366ab0be1995c3e4ffdd8ff2c09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2011, 2012 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.e4.ui.model;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.translation.TranslationService;
import org.eclipse.e4.ui.model.application.MApplicationElement;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.MUILabel;
import org.eclipse.e4.ui.model.internal.ModelUtils;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

/**
 * This class provides set of utility method that can be useful in typical
 * localization scenarios.
 */
final public class LocalizationHelper {

	private LocalizationHelper() {
		// prevents instantiation
	}

	public static String getLocalizedFeature(EStructuralFeature feature,
			MApplicationElement element) {
		Object o = ((EObject) element).eGet(feature);
		if (o instanceof String) {
			return getLocalized((String) o, element);
		}
		return null;
	}

	/**
	 * Returns localized accessibilityPhrase for the specified element using
	 * locale information from its context.
	 * 
	 * @param element
	 *            the element
	 * @return localized element's accessibilityPhrase, or <code>null</code> if
	 *         no label can be found
	 */
	public static String getLocalizedAccessibilityPhrase(MUIElement element) {
		String key = element.getAccessibilityPhrase();
		if (key == null)
			return null;
		return getLocalized(key, element);
	}

	/**
	 * Returns localized label for the specified element using locale
	 * information from its context.
	 * 
	 * @param element
	 *            the element
	 * @return localized element's label, or <code>null</code> if no label can
	 *         be found
	 */
	public static String getLocalizedLabel(MUIElement element) {
		if (!(element instanceof MUILabel))
			return null;
		String key = ((MUILabel) element).getLabel();
		if (key == null)
			return null;
		return getLocalized(key, element);
	}

	/**
	 * Returns localized tooltip for the specified element using locale
	 * information from its context.
	 * 
	 * @param element
	 *            the element
	 * @return localized element's tooltip, or <code>null</code> if no tooltip
	 *         can be found
	 */
	public static String getLocalizedTooltip(MUIElement element) {
		if (!(element instanceof MUILabel))
			return null;
		String key = ((MUILabel) element).getTooltip();
		if (key == null)
			return null;
		return getLocalized(key, element);
	}

	/**
	 * Returns localized string for the key using locale information specified
	 * in the element's context.
	 * <p>
	 * This method will return the key itself if the context can not be found
	 * for the model element or there is no translation service registered in 
	 * that context.
	 * </p>
	 * 
	 * @param key
	 *            the key
	 * @param element
	 *            the model element
	 * @return localized key
	 */
	public static String getLocalized(String key, MApplicationElement element) {
		IEclipseContext context = ModelUtils.getContainingContext(element);
		return getLocalized(key, element, context);
	}

	/**
	 * Returns localized string for the key from the application element using
	 * translation service from the context.
	 * <p>
	 * This method will return the key itself if the context is <code>null</code> 
	 * or there is no translation service registered in the given context.
	 * </p>
	 * 
	 * @param key
	 *            the key
	 * @param element
	 *            the model element
	 * @param context
	 *            the context
	 * @return localized key
	 */
	public static String getLocalized(String key, MApplicationElement element,
			IEclipseContext context) {
		if (key == null || context == null)
			return key;
		TranslationService translation = context.get(TranslationService.class);
		if (translation == null)
			return key;
		return translation.translate(key, element.getContributorURI());
	}

}

Back to the top