[opencms] Re: [opencms-dev] Resource Bundles

Jorge González informatico at hotelparadisepark.com
Wed Dec 21 10:41:13 CET 2005


Hi, i've used this custom bundles.

No need to reload.

Compatible with the core java bundles.

Thanks to Emmanuel Bourg



-------
/**
 *
 */
package com.util.i18n;

/**
 *
 */
	/*
	 * Copyright 2004 Emmanuel Bourg
	 *
	 * Licensed under the Apache License, Version 2.0 (the "License") you may
	 * not use this file except in compliance with the License. You may obtain
a
	 * copy of the License at
	 *
	 * http://www.apache.org/licenses/LICENSE-2.0
	 *
	 * Unless required by applicable law or agreed to in writing, software
	 * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
	 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
	 * License for the specific language governing permissions and limitations
	 * under the License.
	 */

	import java.net.URL;
	import java.util.Locale;
	import java.util.Map;

	import org.apache.commons.configuration.Configuration;
	import org.apache.commons.configuration.ConfigurationException;
	import org.apache.commons.configuration.PropertiesConfiguration;
	import
org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
	import org.apache.struts.util.MessageResources;
	import org.apache.struts.util.MessageResourcesFactory;

	/**
	 * MessageResources based on reloadble configurations.
	 *
	 * @author Emmanuel Bourg
	 */
	public class ConfigurationMessageResources extends MessageResources {

	    /**
		 *
		 */
		private static final long serialVersionUID = 2633327012667069926L;
		/** Configurations associées à leur Locale respective. */
	    protected Map configurations;

	    public ConfigurationMessageResources(MessageResourcesFactory factory,
String config, boolean returnNull) {
	        super(factory, config, returnNull);
	    }

	    public String getMessage(Locale locale, String key) {

	        // get the configuration for the specified locale
	        Configuration resource = getConfiguration(this.config + "_" +
locale.getLanguage() + ".properties");

	        if (resource == null || !resource.containsKey(key)) {
	            // look for the key in the root configuration
	            resource = getConfiguration(this.config + ".properties");
	        }

	        return resource != null ? resource.getString(key, null) : null;
	    }

	    /**
		 * Load the specified configuration from the classpath and initialize
		 * it.
		 */
	    private Configuration getConfiguration(String name) {

	        Configuration configuration = null;

	        URL url =
Thread.currentThread().getContextClassLoader().getResource(name);

	        if (url != null) {
	            PropertiesConfiguration pc = new PropertiesConfiguration();
	            PropertiesConfiguration.setDelimiter('\uffff'); // disable
																// string
																// splitting
	            pc.setURL(url);
	            pc.setReloadingStrategy(new FileChangedReloadingStrategy());

	            try {
	                pc.load();
	                configuration = pc;
	            } catch (ConfigurationException e) {
	                e.printStackTrace();
	            }
	        }

	        return configuration;
	    }
	}

------------------
package com.util.i18n;

/*
 * Copyright 2004 Emmanuel Bourg
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.struts.util.MessageResourcesFactory;
import org.apache.struts.util.MessageResources;

/**
 * Factory for ConfigurationMessageResources.
 *
 * @author Emmanuel Bourg
 */
public class ConfigurationMessageResourcesFactory extends
MessageResourcesFactory {

    /**
	 *
	 */
	private static final long serialVersionUID = 22448257912459257L;

	public MessageResources createResources(String config) {
        return new ConfigurationMessageResources(this, config,
this.returnNull);
    }
}


---------------------
package test.util.i18n;

import java.util.Locale;

import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;

import com.oneplanettravel.util.i18n.ConfigurationMessageResourcesFactory;

import junit.framework.Assert;
import junit.framework.TestCase;

public class ConfigurationMessageResourcesTest extends TestCase {

	protected MessageResourcesFactory msgFactory;

	protected MessageResources msg;

	public static void main(String[] args) {
		junit.textui.TestRunner.run(ConfigurationMessageResourcesTest.class);
	}

	protected void setUp() throws Exception {
		super.setUp();
		msgFactory = ConfigurationMessageResourcesFactory.createFactory();
	}

	/*
	 * Test method for
	 *
'com.oneplanettravel.util.lang.ConfigurationMessageResources.getMessage(Loca
le,
	 * String)'
	 */
	public void testGetMessageLocaleString() {

		while (true) {
			msg = msgFactory.createResources("test.util.i18n.configurationMessages");
			Assert.assertEquals("english", msg.getMessage(Locale.ENGLISH,
					"test.key"));
			Assert.assertEquals("español", msg.getMessage(new Locale("es"),
					"test.key"));
			System.out.println(msg.getMessage(Locale.ENGLISH, "test.key"));
			System.out.println("Assert ok, waiting...");
			System.out.println("msg=" + msg);

			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}




More information about the opencms-dev mailing list