Example usage for java.util ResourceBundle getString

List of usage examples for java.util ResourceBundle getString

Introduction

In this page you can find the example usage for java.util ResourceBundle getString.

Prototype

public final String getString(String key) 

Source Link

Document

Gets a string for the given key from this resource bundle or one of its parents.

Usage

From source file:com.inkubator.hrm.web.converter.CompanyConverter.java

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {

    ResourceBundle resourceBundle = ResourceBundle.getBundle("Messages",
            new Locale(FacesUtil.getSessionAttribute(HRMConstant.BAHASA_ACTIVE).toString()));

    String message = StringUtils.EMPTY;
    String data = (String) value;
    if (Objects.equals(data, HRMConstant.ORGANIZATION_LEVEL_HOLDING)) {
        message = resourceBundle.getString("organization.holding");
    } else if (Objects.equals(data, HRMConstant.ORGANIZATION_LEVEL_COMPANY)) {
        message = resourceBundle.getString("organization.company");
    }/*from w  w w.  ja v a 2  s  .co m*/

    return message;
}

From source file:com.gettextresourcebundle.GettextResourceBundleControlTest.java

/**
 * test that PO files loaded from the file system will reload after 
 * cache timeout occurs and modified dates are different
 * @throws IOException//w w w . jav  a 2s .  c  o  m
 */
@Test
public void testFileReloads() throws IOException {
    File localeDirectory = new File("./src/test/resources/locale");
    File localeNewDirectory = new File("./src/test/resources/localeNew");
    File targetDirectory = new File("./target/testLocales");
    targetDirectory.mkdirs();

    //unit test with 5 second cache time
    final long cacheTTL = 5 * 1000l;
    GettextResourceBundleControl.setCacheTTL(cacheTTL);

    FileUtils.copyDirectory(localeDirectory, targetDirectory, false);

    ResourceBundle en_US = ResourceBundle.getBundle(targetDirectory.getAbsolutePath(), Locale.US,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_US for the en_US locale", "en_US", en_US.getString("locale"));

    ResourceBundle en_CA = ResourceBundle.getBundle(targetDirectory.getAbsolutePath(), Locale.CANADA,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_CA for the en_CA locale", "en_CA", en_CA.getString("locale"));

    FileUtils.copyDirectory(localeNewDirectory, targetDirectory, false);

    en_US = ResourceBundle.getBundle(targetDirectory.getAbsolutePath(), Locale.US,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_US for the en_US locale before cache expiry", "en_US",
            en_US.getString("locale"));

    en_CA = ResourceBundle.getBundle(targetDirectory.getAbsolutePath(), Locale.CANADA,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_CA for the en_CA locale before cache expiry", "en_CA",
            en_CA.getString("locale"));

    //wait until cacheTTL passes so that cache expires
    long end = System.currentTimeMillis() + (cacheTTL);
    while (end > System.currentTimeMillis()) {
        try {
            Thread.sleep(end - System.currentTimeMillis());
        } catch (InterruptedException e) {

        }
    }

    en_US = ResourceBundle.getBundle(targetDirectory.getAbsolutePath(), Locale.US,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_US #2 for the en_US locale before cache expiry", "en_US #2",
            en_US.getString("locale"));

    en_CA = ResourceBundle.getBundle(targetDirectory.getAbsolutePath(), Locale.CANADA,
            GettextResourceBundleControl.getControl("test"));
    assertEquals("The locale key should be en_CA #2 for the en_CA locale before cache expiry", "en_CA #2",
            en_CA.getString("locale"));
}

From source file:mx.com.pixup.portal.db.DBConecta.java

/**
 * Lee los parmetros de conexion del archivo JDBC.properties y los guarda en en dbProp
 *///  w  w  w.  j a va  2  s . c  o m
public DBConecta() {
    try {
        ResourceBundle rsb = ResourceBundle.getBundle("setup");
        Enumeration enum1 = rsb.getKeys();
        Properties dbProp = new Properties();
        while (enum1.hasMoreElements()) {
            String sTmp = (String) enum1.nextElement();
            dbProp.setProperty(sTmp, rsb.getString(sTmp));
            //System.out.println("+" + sTmp );
        }
        this.dbProp = dbProp;
        System.out.println(dbProp);

    } catch (Exception e) {
        System.out.println("+" + e.getMessage());
    }

}

From source file:SplitPaneDemo2.java

public SplitPaneDemo() {
    //Read image names from a properties file.
    ResourceBundle imageResource;
    try {/*from  w w  w .  j a  v  a  2s  .  c om*/
        imageResource = ResourceBundle.getBundle("imagenames");
        String imageNamesString = imageResource.getString("images");
        imageNames = parseList(imageNamesString);
    } catch (MissingResourceException e) {
        handleMissingResource(e);
    }

    //Create the list of images and put it in a scroll pane.
    list = new JList(imageNames);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    list.addListSelectionListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    //Set up the picture label and put it in a scroll pane.
    ImageIcon firstImage = createImageIcon("images/" + (String) imageNames.firstElement());
    if (firstImage != null) {
        picture = new JLabel(firstImage);
    } else {
        picture = new JLabel((String) imageNames.firstElement());
    }
    JScrollPane pictureScrollPane = new JScrollPane(picture);

    //Create a split pane with the two scroll panes in it.
    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, pictureScrollPane);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(150);

    //Provide minimum sizes for the two components in the split pane.
    Dimension minimumSize = new Dimension(100, 50);
    listScrollPane.setMinimumSize(minimumSize);
    pictureScrollPane.setMinimumSize(minimumSize);

    //Provide a preferred size for the split pane.
    splitPane.setPreferredSize(new Dimension(400, 200));
}

From source file:com.jaspersoft.studio.utils.expr.AInterpreter.java

protected String prepareExpression(String expression, int recursion) throws Exception {
    while (expression.indexOf("$P{") >= 0) {
        String pname = Misc.extract(expression, "$P{", "}");
        JRParameter pr = null;//from  w  ww. j  av a2  s .c o m
        pr = dataset.getParametersMap().get(pname);
        // for (JRParameter p : dataset.getParametersList()) {
        // if (p.getName().equals(pname)) {
        // pr = p;
        // break;
        // }
        // }
        if (pr == null)
            throw new JRException("Paramater $P{" + pname + "} does not exists in the dataset");
        String pnameLiteral = getLiteral(pname);
        expression = Misc.strReplace(pnameLiteral, "$P{" + pname + "}", expression);

        if (!literals.contains(pnameLiteral))
            recursiveInterpreter(recursion, pr);
    }

    // Try to evaluate the variable
    while (expression.indexOf("$V{") >= 0) {
        String vname = Misc.extract(expression, "$V{", "}");
        JRVariable vr = null;
        vr = dataset.getVariablesMap().get(vname);
        if (vr == null)
            throw new JRException("Variable $V{" + vname + "} does not exists in the dataset");
        String pnameLiteral = getVariableLiteral(vname);
        expression = Misc.strReplace(pnameLiteral, "$V{" + vname + "}", expression);

        if (!literals.contains(pnameLiteral))
            recursiveInterpreter(recursion, vr);
    }

    // Remove the field since it can't be evaluated and change it with null
    while (expression.indexOf("$F{") >= 0) {
        String fname = Misc.extract(expression, "$F{", "}");
        expression = Misc.strReplace("(null)", "$F{" + fname + "}", expression);
    }

    while (expression.indexOf("$R{") >= 0) {
        String pname = Misc.extract(expression, "$R{", "}");
        String baseName = getBundleName();
        if (!baseName.isEmpty()) {
            ResourceBundle rb = getResourceBundle();
            if (rb != null)
                baseName = Misc.nvl(rb.getString(pname));
        }
        expression = Misc.strReplace("\"" + baseName + "\"", "$R{" + pname + "}", expression);
    }
    return expression;
}

From source file:es.pode.empaquetador.presentacion.agregar.personales.AgregarPersonalesControllerImpl.java

/**
 * @see es.pode.empaquetador.presentacion.agregar.personales.AgregarPersonalesController#submit(org.apache.struts.action.ActionMapping, es.pode.empaquetador.presentacion.agregar.personales.SubmitForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *//* w  ww  .  j a  v  a  2  s  .  c  o m*/
public final void submit(ActionMapping mapping,
        es.pode.empaquetador.presentacion.agregar.personales.SubmitForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    java.util.Locale locale = (Locale) request.getSession().getAttribute(ConstantesAgrega.DEFAULT_LOCALE);
    ResourceBundle i18n = ResourceBundle.getBundle("application-resources", locale);

    String accion = form.getAction();
    String identificador = form.getIdODE();
    if (accion.equals(i18n.getString("portal_empaquetado_gestorSubman_agregarLocal.aceptar"))) {
        if ((identificador == null) || (identificador.equals(""))) {
            throw new ValidatorException("{exportar.subirArchivo}");
        }

    }
}

From source file:org.codehaus.mojo.chronos.chart.ResponseChartGenerator.java

private TimeSeriesCollection createResponseDataset(String name, ResponsetimeSamples samples,
        ResourceBundle bundle, ReportConfig config) {
    TimeSeries series = new TimeSeries(name, Millisecond.class);

    samples.appendResponsetimes(series, config.getResponsetimedivider());
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);/*from   w  w  w . j  a  va 2  s  . c om*/

    String averageLabel = bundle.getString("chronos.label.average");
    TimeSeries averageseries = MovingAverage.createMovingAverage(series, averageLabel,
            config.getAverageduration(), 0);
    dataset.addSeries(averageseries);
    return dataset;
}

From source file:JAXRSaveClassificationScheme.java

/**
     * Establishes a connection to a registry.
     */*w  ww  .java2s.  c  om*/
     * @param queryUrl        the URL of the query registry
     * @param publishUrl        the URL of the publish registry
     */
    public void makeConnection(String queryUrl, String publishUrl) {
        /*
         * Specify proxy information in case you
         *  are going beyond your firewall.
         */
        ResourceBundle bundle = ResourceBundle.getBundle("JAXRExamples");
        String httpProxyHost = bundle.getString("http.proxyHost");
        String httpProxyPort = bundle.getString("http.proxyPort");
        String httpsProxyHost = bundle.getString("https.proxyHost");
        String httpsProxyPort = bundle.getString("https.proxyPort");

        /*
         * Define connection configuration properties.
         * To delete, you need both the query URL and the
         * publish URL.
         */
        Properties props = new Properties();
        props.setProperty("javax.xml.registry.queryManagerURL", queryUrl);
        props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishUrl);
        props.setProperty("com.sun.xml.registry.http.proxyHost", httpProxyHost);
        props.setProperty("com.sun.xml.registry.http.proxyPort", httpProxyPort);
        props.setProperty("com.sun.xml.registry.https.proxyHost", httpsProxyHost);
        props.setProperty("com.sun.xml.registry.https.proxyPort", httpsProxyPort);

        try {
            // Create the connection, passing it the 
            // configuration properties
            ConnectionFactory factory = ConnectionFactory.newInstance();
            factory.setProperties(props);
            connection = factory.createConnection();
            System.out.println("Created connection to registry");
        } catch (Exception e) {
            e.printStackTrace();

            if (connection != null) {
                try {
                    connection.close();
                } catch (JAXRException je) {
                }
            }
        }
    }

From source file:com.heliosphere.athena.base.resource.bundle.ResourceBundleManager.java

/**
 * Returns the resource bundle string of a given enumerated value for the given enumeration class. This
 * method is generally used by enumeration classes using the {@code BundleEnum} annotation.
 * <hr>//from   w  w w  .ja  va  2s. co  m
 * @param eClass Class of the enumeration.
 * @param e Enumerated value.
 * @param locale {@link Locale} to use for resource string retrieval.
 * @return Resource bundle string.
 */
@SuppressWarnings("nls")
public static final String getResourceForMethodName(@NonNull final Class<? extends Enum<?>> eClass,
        final Enum<?> e, final Locale locale) {
    String key = null;
    ResourceBundle bundle;
    String methodName = null;
    String className;

    int index = 1;
    boolean found = false;
    while (!found) {
        className = Thread.currentThread().getStackTrace()[index].getClassName();
        if (className.equals(eClass.getName())) {
            methodName = Thread.currentThread().getStackTrace()[index].getMethodName();
            found = true;
        } else {
            index++;
        }
    }

    if (found) {
        for (Method method : eClass.getMethods()) {
            BundleEnum annotation = method.getAnnotation(BundleEnum.class);
            if (annotation != null && method.getName().equals(methodName)) {
                bundle = ResourceBundle.getBundle(annotation.file(), locale);
                key = annotation.path() + "." + e.name();
                return bundle.getString(key);
            }
        }
    }

    throw new ResourceBundleException(BundleAthenaBase.ResourceBundleInvalidKey, null, key, null, locale, e);
}

From source file:com.heliosphere.demeter.base.resource.bundle.ResourceBundleManager.java

/**
 * Returns the resource bundle string of a given enumerated value for the given enumeration class. This
 * method is generally used by enumeration classes using the {@code BundleEnum} annotation.
 * <hr>/*  w  ww .  j ava 2  s . c  o  m*/
 * @param eClass Class of the enumeration.
 * @param e Enumerated value.
 * @param locale {@link Locale} to use for resource string retrieval.
 * @return Resource bundle string.
 */
@SuppressWarnings("nls")
public static final String getResourceForMethodName(@NonNull final Class<? extends Enum<?>> eClass,
        final Enum<?> e, final Locale locale) {
    String key = null;
    ResourceBundle bundle;
    String methodName = null;
    String className;

    int index = 1;
    boolean found = false;
    while (!found) {
        className = Thread.currentThread().getStackTrace()[index].getClassName();
        if (className.equals(eClass.getName())) {
            methodName = Thread.currentThread().getStackTrace()[index].getMethodName();
            found = true;
        } else {
            index++;
        }
    }

    if (found) {
        for (Method method : eClass.getMethods()) {
            BundleEnum annotation = method.getAnnotation(BundleEnum.class);
            if (annotation != null && method.getName().equals(methodName)) {
                bundle = ResourceBundle.getBundle(annotation.file(), locale);
                key = annotation.path() + "." + e.name();
                return bundle.getString(key);
            }
        }
    }

    throw new ResourceBundleException(BundleDemeterBase.ResourceBundleInvalidKey, null, key, null, locale, e);
}