use of java.util.PropertyResourceBundle in project omegat by omegat-org.
the class Main method applyConfigFile.
/**
* Load System properties from a specified .properties file. In order to
* allow this to reliably change the display language, it must called before
* any use of {@link Log#log}, thus it logs to {@link System#out}.
*
* @param path
* to config file
*/
private static void applyConfigFile(String path) {
if (path == null) {
return;
}
File configFile = new File(path);
if (!configFile.exists()) {
return;
}
System.out.println("Reading config from " + path);
try (FileInputStream in = new FileInputStream(configFile)) {
PropertyResourceBundle config = new PropertyResourceBundle(in);
// Put config properties into System properties and into OmegaT params.
for (String key : config.keySet()) {
String value = config.getString(key);
System.setProperty(key, value);
PARAMS.put(key, value);
System.out.println("Read from config: " + key + "=" + value);
}
// System.setProperty() will not work.
if (config.containsKey("user.language")) {
String userLanguage = config.getString("user.language");
Locale userLocale = config.containsKey("user.country") ? new Locale(userLanguage, config.getString("user.country")) : new Locale(userLanguage);
Locale.setDefault(userLocale);
}
} catch (FileNotFoundException exception) {
System.err.println("Config file not found: " + path);
} catch (IOException exception) {
System.err.println("Error while reading config file: " + path);
}
}
use of java.util.PropertyResourceBundle in project omegat by omegat-org.
the class OStrings method loadBundle.
/**
* Loads resources from the specified file. If the file cannot be loaded,
* resources are reverted to the default locale. Useful when testing
* localisations outside the jar file.
*/
public static void loadBundle(String filename) {
boolean loaded = false;
try {
// Load the resource bundle
FileInputStream in = new FileInputStream(filename);
bundle = new PropertyResourceBundle(in);
loaded = true;
in.close();
} catch (FileNotFoundException exception) {
System.err.println("Resource bundle file not found: " + filename);
} catch (IOException exception) {
System.err.println("Error while reading resource bundle file: " + filename);
}
// loaded, and if not, revert to the default
if (!loaded) {
System.err.println("Reverting to resource bundle for the default locale");
bundle = ResourceBundle.getBundle("org/omegat/Bundle");
}
}
use of java.util.PropertyResourceBundle in project ripme by RipMeApp.
the class UTF8Control method newBundle.
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
// Only this line is changed to make it to read properties files as UTF-8.
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
} finally {
stream.close();
}
}
return bundle;
}
use of java.util.PropertyResourceBundle in project jersey by jersey.
the class OsgiRegistry method getResourceBundle.
/**
* Tries to load resource bundle via OSGi means. No caching involved here,
* as localization properties are being cached in Localizer class already.
*
* @param bundleName name of the resource bundle to load
* @return resource bundle instance if found, null otherwise
*/
public ResourceBundle getResourceBundle(final String bundleName) {
final int lastDotIndex = bundleName.lastIndexOf('.');
final String path = bundleName.substring(0, lastDotIndex).replace('.', '/');
final String propertiesName = bundleName.substring(lastDotIndex + 1, bundleName.length()) + ".properties";
for (final Bundle bundle : bundleContext.getBundles()) {
final Enumeration<URL> entries = findEntries(bundle, path, propertiesName, false);
if (entries != null && entries.hasMoreElements()) {
final URL entryUrl = entries.nextElement();
try {
return new PropertyResourceBundle(entryUrl.openStream());
} catch (final IOException ex) {
if (LOGGER.isLoggable(Level.FINE)) {
// does not make sense to localize this
LOGGER.fine("Exception caught when tried to load resource bundle in OSGi");
}
return null;
}
}
}
return null;
}
use of java.util.PropertyResourceBundle in project che by eclipse.
the class ContributionTemplateStore method readIncludedTemplates.
private void readIncludedTemplates(Collection templates, /*, IConfigurationElement element*/
String file) throws IOException {
// String file= element.getAttribute(FILE);
if (file != null) {
// Bundle plugin = Platform.getBundle(element.getContributor().getName());
// URL url= FileLocator.find(plugin, Path.fromOSString(file), null);
URL url = ContributionTemplateStore.class.getResource(file);
if (url != null) {
ResourceBundle bundle = null;
InputStream bundleStream = null;
InputStream stream = null;
try {
// String translations= element.getAttribute(TRANSLATIONS);
// if (translations != null) {
//file.substring(0, file.lastIndexOf('.'));
String props = "/org/eclipse/templates/default-templates.properties";
URL bundleURL = ContributionTemplateStore.class.getResource(props);
if (bundleURL != null) {
bundleStream = bundleURL.openStream();
bundle = new PropertyResourceBundle(bundleStream);
}
// }
stream = new BufferedInputStream(url.openStream());
TemplateReaderWriter reader = new TemplateReaderWriter();
TemplatePersistenceData[] datas = reader.read(stream, bundle);
for (int i = 0; i < datas.length; i++) {
TemplatePersistenceData data = datas[i];
if (data.isCustom()) {
if (data.getId() == null)
JavaPlugin.logErrorMessage("Ignoring template ''" + data.getTemplate().getName() + "'' since it has no id.");
else
JavaPlugin.logErrorMessage("Ignoring template ''" + data.getTemplate().getName() + "'' since it is deleted.");
} else if (validateTemplate(data.getTemplate())) {
templates.add(data);
}
}
} finally {
try {
if (bundleStream != null)
bundleStream.close();
} catch (IOException x) {
} finally {
try {
if (stream != null)
stream.close();
} catch (IOException x) {
}
}
}
}
}
}
Aggregations