|
Lines 13-51
Link Here
|
| 13 |
import java.util.*; |
13 |
import java.util.*; |
| 14 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
14 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| 15 |
import org.eclipse.equinox.internal.p2.ui.ProvUIActivator; |
15 |
import org.eclipse.equinox.internal.p2.ui.ProvUIActivator; |
| 16 |
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; |
16 |
import org.eclipse.equinox.internal.provisional.p2.metadata.*; |
| 17 |
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery; |
17 |
import org.eclipse.equinox.internal.provisional.p2.metadata.query.IUPropertyQuery; |
| 18 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
18 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
| 19 |
import org.eclipse.equinox.internal.provisional.p2.query.Collector; |
19 |
import org.eclipse.equinox.internal.provisional.p2.query.Collector; |
| 20 |
|
20 |
|
| 21 |
public class IUPropertyUtils { |
21 |
public class IUPropertyUtils { |
| 22 |
|
22 |
|
|
|
23 |
// TODO: these constants should come from API, eg. IInstallableUnit or ??? |
| 23 |
static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$ |
24 |
static final Locale DEFAULT_LOCALE = new Locale("df", "LT"); //$NON-NLS-1$//$NON-NLS-2$ |
|
|
25 |
static final String NAMESPACE_IU_LOCALIZATION = "org.eclipse.equinox.p2.localization"; //$NON-NLS-1$ |
| 24 |
|
26 |
|
| 25 |
public static String getIUProperty(IInstallableUnit iu, String propertyKey, Locale locale) { |
27 |
public static String getIUProperty(IInstallableUnit iu, String propertyKey, Locale locale) { |
| 26 |
String value = iu.getProperty(propertyKey); |
28 |
String value = iu.getProperty(propertyKey); |
| 27 |
if (value == null || value.length() <= 1 || value.charAt(0) != '%') |
29 |
if (value == null || value.length() <= 1 || value.charAt(0) != '%') |
| 28 |
return value; |
30 |
return value; |
| 29 |
// else have a localizable property |
31 |
// else have a localizable property |
| 30 |
String actualKey = value.substring(1); |
32 |
final String actualKey = value.substring(1); // Strip off the % |
| 31 |
String localizationBundles[] = buildLocalizationVariants(iu, locale); |
33 |
final List locales = buildLocaleVariants(locale); |
|
|
34 |
final IInstallableUnit theUnit = iu; |
| 35 |
|
| 36 |
Collector hostLocalizationCollector = new Collector() { |
| 37 |
public boolean accept(Object object) { |
| 38 |
boolean haveHost = false; |
| 39 |
boolean haveLocale = false; |
| 40 |
if (object instanceof IInstallableUnitFragment) { |
| 41 |
IInstallableUnitFragment fragment = (IInstallableUnitFragment) object; |
| 42 |
RequiredCapability[] hosts = fragment.getHost(); |
| 43 |
for (int i = 0; i < hosts.length; i++) { |
| 44 |
RequiredCapability nextHost = hosts[i]; |
| 45 |
if (IInstallableUnit.NAMESPACE_IU_ID.equals(nextHost.getNamespace()) && // |
| 46 |
theUnit.getId().equals(nextHost.getName()) && // |
| 47 |
nextHost.getRange() != null && // |
| 48 |
nextHost.getRange().isIncluded(theUnit.getVersion())) { |
| 49 |
haveHost = true; |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (haveHost) { |
| 55 |
ProvidedCapability[] provides = fragment.getProvidedCapabilities(); |
| 56 |
for (int j = 0; j < provides.length && !haveLocale; j++) { |
| 57 |
ProvidedCapability nextProvide = provides[j]; |
| 58 |
if (NAMESPACE_IU_LOCALIZATION.equals(nextProvide.getNamespace())) { |
| 59 |
String providedLocale = nextProvide.getName(); |
| 60 |
if (providedLocale != null) { |
| 61 |
for (Iterator iter = locales.iterator(); iter.hasNext();) { |
| 62 |
if (providedLocale.equals(iter.next())) { |
| 63 |
haveLocale = true; |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
return (haveHost && haveLocale ? super.accept(object) : false); |
| 73 |
} |
| 74 |
}; |
| 32 |
|
75 |
|
| 33 |
IMetadataRepositoryManager repoMgr = (IMetadataRepositoryManager) ServiceHelper.getService(ProvUIActivator.getContext(), IMetadataRepositoryManager.class.getName()); |
76 |
IMetadataRepositoryManager repoMgr = (IMetadataRepositoryManager) ServiceHelper.getService(ProvUIActivator.getContext(), IMetadataRepositoryManager.class.getName()); |
| 34 |
|
77 |
IUPropertyQuery iuQuery = new IUPropertyQuery(IInstallableUnit.PROP_TYPE_FRAGMENT, "true"); //$NON-NLS-1$ |
| 35 |
for (int i = 0; i < localizationBundles.length; i++) { |
78 |
Collector collected = repoMgr.query(iuQuery, hostLocalizationCollector, null); |
| 36 |
InstallableUnitQuery iuQuery = new InstallableUnitQuery(localizationBundles[i]); |
79 |
if (!collected.isEmpty()) { |
| 37 |
Collector collected = repoMgr.query(iuQuery, new Collector(), null); |
80 |
String translation = null; |
| 38 |
if (!collected.isEmpty()) { |
81 |
for (Iterator iter = collected.iterator(); iter.hasNext() && translation == null;) { |
| 39 |
for (Iterator iter = collected.iterator(); iter.hasNext();) { |
82 |
IInstallableUnit localizationIU = (IInstallableUnit) iter.next(); |
| 40 |
IInstallableUnit localizationIU = (IInstallableUnit) iter.next(); |
83 |
for (Iterator jter = locales.iterator(); jter.hasNext();) { |
| 41 |
String translation = localizationIU.getProperty(actualKey); |
84 |
String localeKey = makeLocalizedKey(actualKey, (String) jter.next()); |
|
|
85 |
translation = localizationIU.getProperty(localeKey); |
| 42 |
if (translation != null) |
86 |
if (translation != null) |
| 43 |
return translation; |
87 |
return translation; |
| 44 |
} |
88 |
} |
| 45 |
} |
89 |
} |
| 46 |
} |
90 |
} |
| 47 |
|
91 |
|
| 48 |
String defaultKey = DEFAULT_LOCALE.toString() + '.' + actualKey; |
92 |
String defaultKey = makeLocalizedKey(actualKey, DEFAULT_LOCALE.toString()); |
| 49 |
String defaultValue = iu.getProperty(defaultKey); |
93 |
String defaultValue = iu.getProperty(defaultKey); |
| 50 |
if (defaultValue != null) |
94 |
if (defaultValue != null) |
| 51 |
return defaultValue; |
95 |
return defaultValue; |
|
Lines 53-77
Link Here
|
| 53 |
return value; |
97 |
return value; |
| 54 |
} |
98 |
} |
| 55 |
|
99 |
|
| 56 |
private static final String TRANSLATED_PROPERTIES_NAME = "_translated_properties"; //$NON-NLS-1$ |
|
|
| 57 |
|
| 58 |
/** |
100 |
/** |
| 59 |
*/ |
101 |
*/ |
| 60 |
private static String[] buildLocalizationVariants(IInstallableUnit iu, Locale locale) { |
102 |
private static List buildLocaleVariants(Locale locale) { |
| 61 |
String id = iu.getId().toString(); |
|
|
| 62 |
String nl = locale.toString(); |
103 |
String nl = locale.toString(); |
| 63 |
ArrayList result = new ArrayList(4); |
104 |
ArrayList result = new ArrayList(4); |
| 64 |
int lastSeparator; |
105 |
int lastSeparator; |
| 65 |
while (true) { |
106 |
while (true) { |
| 66 |
result.add(id + '_' + nl + TRANSLATED_PROPERTIES_NAME); |
107 |
result.add(nl); |
| 67 |
lastSeparator = nl.lastIndexOf('_'); |
108 |
lastSeparator = nl.lastIndexOf('_'); |
| 68 |
if (lastSeparator == -1) |
109 |
if (lastSeparator == -1) |
| 69 |
break; |
110 |
break; |
| 70 |
nl = nl.substring(0, lastSeparator); |
111 |
nl = nl.substring(0, lastSeparator); |
| 71 |
} |
112 |
} |
| 72 |
// Add the empty suffix last (most general) |
113 |
// Add the default locale (most general) |
| 73 |
result.add(id + TRANSLATED_PROPERTIES_NAME); |
114 |
result.add(DEFAULT_LOCALE); |
| 74 |
return (String[]) result.toArray(new String[result.size()]); |
115 |
return result; |
|
|
116 |
} |
| 117 |
|
| 118 |
private static String makeLocalizedKey(String actualKey, String localeImage) { |
| 119 |
return localeImage + '.' + actualKey; |
| 75 |
} |
120 |
} |
| 76 |
|
121 |
|
| 77 |
} |
122 |
} |