Howard Lewis Ship wrote:
Messages are loaded just-in-time, using the Registry's locale.

For me, the issue is that the Locale is fixed which works fine (I
guess) for a standalone program, but becomes problematic for web apps
(Tapestry apps) where different concurrent users need messages in
different locales.

I suppose we could implement a kind of thread-local to store the
user's locale. This would be used when accessing localized messages
from code (i.e., via a Messages object).

The problem is the %key mechanism in contributions, which is another
way to allow access to localized messages. These become strings, or
parts of strings, that then can "flow" (via contributions, schemas,
collaborating services, etc.) into all kinds of unpredictable places. So what locale is used for them ... and how do they get properly
localized? I don't have a solution.


To some degree, what's needed iis a way to keep messages virtual (keys
+ parameters) right up until there's a known locale, then fold them
down to localized strings. I can see this as a service on top of
HiveMind.  I'd like one solution that covers everything, magically,
but I don't seem to have it!


Ok, so I was wrong because I thought it was just "the logging side"... mainly because the locale should be controlled by the end "user" (developper). For instance, in Tapestry, the page has a getLocale() property which should be used by the developper to localize messages intented for the application's end-user (the net surfer). My way to do this is to use a "I18nMessage" class which store message information (key and parameters). Here it is (copyleft if you want it a as base) :


/*
 * Created on 1 sept. 2004
 */
package com.asystan.common.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author Mikael Cluseau
 */
public class I18nMessage {

        private final String bundleName;

        private final String key;

        private final String[] parameters;

        /*
         * 
------------------------------------------------------------------------
         * Constructors
         * 
------------------------------------------------------------------------
         */

        public I18nMessage(String key) {
                this(key, null, null);
        }

        public I18nMessage(String key, String[] parameters) {
                this(key, parameters, null);
        }

        public I18nMessage(String key, String bundleName) {
                this(key, null, bundleName);
        }

        public I18nMessage(String key, String[] parameters, String bundleName) {
                this.key = key;
                this.parameters = parameters;
                this.bundleName = bundleName;
        }

        /*
         * 
------------------------------------------------------------------------
         * Convertion methods
         * 
------------------------------------------------------------------------
         */

        public String toString(ResourceBundle bundle) {
                if (key == null) {
                        return null;
                }

                String retval = bundle == null ? null : bundle.getString(key);
                if (retval == null) {
                        retval = key;
                }

                if (parameters == null) {
                        return retval;
                }

                for (int i = 0; i < parameters.length; i++) {
                        retval = retval.replaceAll("\\{" + i + "\\}", 
parameters[i]);
                }

                return retval;
        }

        public String toString(Locale locale) {
                return toString(ResourceBundle.getBundle(bundleName, locale));
        }

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        public String toString() {
                return toString(ResourceBundle.getBundle(bundleName));
        }

        /*
         * 
------------------------------------------------------------------------
         * Accessors
         * 
------------------------------------------------------------------------
         */

        /**
         * @return Returns the key.
         */
        public String getKey() {
                return key;
        }

        /**
         * @return Returns the parameters.
         */
        public String[] getParameters() {
                return parameters;
        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to