Re: I18N Tag Library

"José Navas" <[email protected]>
Newsgroups gmane.comp.jakarta.taglibs.user
Message-ID <[email protected]>
If what you are trying to do is read the resources file, you can use this
class

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package net.bac.com.cbk.common.util;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

/** @modelguid {ABE1E037-8252-4018-A711-8DD57641A3A0} */
public abstract class PropertyLoader
{
    /**
     * Looks up a resource named 'name' in the classpath. The resource must
map
     * to a file with .properties extention. The name is assumed to be
absolute
     * and can use either "/" or "." for package segment separation with an
     * optional leading "/" and optional ".properties" suffix. Thus, the
     * following names refer to the same resource:
     * <pre>
     * some.pkg.Resource
     * some.pkg.Resource.properties
     * some/pkg/Resource
     * some/pkg/Resource.properties
     * /some/pkg/Resource
     * /some/pkg/Resource.properties
     * </pre>
     *
     * @param name classpath resource name [may not be null]
     * @param loader classloader through which to load the resource [null
     * is equivalent to the application loader]
     *
     * @return resource converted to java.util.Properties [may be null if
the
     * resource was not found and THROW_ON_LOAD_FAILURE is false]
     * @throws IllegalArgumentException if the resource was not found and
     * THROW_ON_LOAD_FAILURE is true
     * @modelguid {1156E969-3D21-490F-A08D-D9F7524E3A91}
     */
    public static Properties loadProperties (String name, ClassLoader
loader)
    {
        if (name == null)
            throw new IllegalArgumentException ("null input: name");

        if (name.startsWith ("/"))
            name = name.substring (1);

        if (name.endsWith (SUFFIX))
            name = name.substring (0, name.length () - SUFFIX.length ());

        Properties result = null;

        InputStream in = null;
        try
        {
            if (loader == null) loader = ClassLoader.getSystemClassLoader();

            if (LOAD_AS_RESOURCE_BUNDLE)
            {
                name = name.replace ('/', '.');
                // Throws MissingResourceException on lookup failures:
                final ResourceBundle rb = ResourceBundle.getBundle (name,
                    Locale.getDefault (), loader);

                result = new Properties ();
                for (Enumeration keys = rb.getKeys (); keys.hasMoreElements();)
                {
                    final String key = (String) keys.nextElement ();
                    final String value = rb.getString (key);

                    result.put (key, value);
                }
            }
            else
            {
                name = name.replace ('.', '/');

                if (! name.endsWith (SUFFIX)) {
                    name = name.concat (SUFFIX);
                }

                // Returns null on lookup failures:
                in = loader.getResourceAsStream (name);
                if (in != null)
                {
                    result = new Properties ();
                    result.load (in); // Can throw IOException
                }
            }
        }
        catch (Exception e)
        {
            result = null;
        }
        finally
        {
            if (in != null) try { in.close (); } catch (Throwable ignore) {}
        }

        if (THROW_ON_LOAD_FAILURE && (result == null))
        {
            throw new IllegalArgumentException ("could not load [" + name +
"]"+
                " as " + (LOAD_AS_RESOURCE_BUNDLE
                ? "a resource bundle"
                : "a classloader resource"));
        }

        return result;
    }

    /**
     * A convenience overload of {@link #loadProperties(String,
ClassLoader)}
     * that uses the current thread's context classloader.
     * @modelguid {B449C763-826B-4D7E-AC76-5E5CE9DF2C5D}
     */
    public static Properties loadProperties (final String name)
    {
        return loadProperties (name,
            Thread.currentThread ().getContextClassLoader ());
    }

    /** @modelguid {8C0F147E-6C55-4051-A646-E5772906A475} */
    private static final boolean THROW_ON_LOAD_FAILURE = true;
    /** @modelguid {D57C850C-12BB-4B4A-98F5-84BBDC3F4069} */
    private static final boolean LOAD_AS_RESOURCE_BUNDLE = false;
    /** @modelguid {D238B664-59AF-4954-A4A9-B0729280FA50} */
    private static final String SUFFIX = ".properties";
} // End of class

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Hope this helps
Navas

On 5/3/07, Vikram Reddy <[email protected]> wrote:
>
> Sure, I am making my application I18n complinat, reading all the labels
> from
> the resources, I am using I18n tag libary from jakartha,
>
> I am replacing all of my labels with i18n tag like <i18n:message key="
> label.name">, <i18n:message key="label.age"> etc, it is working fine.
>
> But I have some labels which are hardcoded in the Backend (Java file) and
> and they are displayed on the UI I am struck how to use this tag in the
> back
> end java class.
>
> I read the documennation, nothing was mentioned about this. let me know if
> you have any questions.
>
> V
>
>
>
>
>
> On 5/3/07, Rashmi Rubdi <[email protected]> wrote:
> >
> > On 5/3/07, Vikram Reddy <[email protected]> wrote:
> > > Did any one use I18N Tag Library from Apache,  I have a question on
> > that.
> > >
> > > I have some of the labels hardcoded in the java bean whcih are
> displayed
> > on
> > > the UI, not sure how I can convert them, Can any one throw some light
> on
> >
> > The description  of the problem is a little vague, it would help to
> > know adequate relevant details.
> >
> > The documentation on the taglibs and examples in i18n-examples.war
> > might have some solutions
> >
> > > this please.. I would really appreciate that.
> > > in my jsp page, I am simply using this tag : <i18n:message key="
> > label.name"
> > > /> ..
> > >
> > > - V
> > >
> >
> > -Regards
> > Rashmi
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >
> >
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.