Search in sources :

Example 31 with PropertyResourceBundle

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);
    }
}
Also used : Locale(java.util.Locale) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 32 with PropertyResourceBundle

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");
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 33 with PropertyResourceBundle

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ResourceBundle(java.util.ResourceBundle) PropertyResourceBundle(java.util.PropertyResourceBundle) URL(java.net.URL) URLConnection(java.net.URLConnection) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 34 with PropertyResourceBundle

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;
}
Also used : ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) PropertyResourceBundle(java.util.PropertyResourceBundle) IOException(java.io.IOException) URL(java.net.URL) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 35 with PropertyResourceBundle

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) {
                    }
                }
            }
        }
    }
}
Also used : TemplatePersistenceData(org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) TemplateReaderWriter(org.eclipse.che.jface.text.templates.persistence.TemplateReaderWriter) IOException(java.io.IOException) URL(java.net.URL) PropertyResourceBundle(java.util.PropertyResourceBundle)

Aggregations

PropertyResourceBundle (java.util.PropertyResourceBundle)66 ResourceBundle (java.util.ResourceBundle)32 InputStream (java.io.InputStream)31 IOException (java.io.IOException)27 URL (java.net.URL)24 InputStreamReader (java.io.InputStreamReader)20 URLConnection (java.net.URLConnection)14 MissingResourceException (java.util.MissingResourceException)13 FileInputStream (java.io.FileInputStream)12 File (java.io.File)9 Locale (java.util.Locale)7 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)4 Bundle (org.osgi.framework.Bundle)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Properties (java.util.Properties)3 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)3 TemplateReaderWriter (org.eclipse.jface.text.templates.persistence.TemplateReaderWriter)3