Internalization and message bundles - TheOpenCloudEngine/metaworks GitHub Wiki

Installing a message handler

index.html

	mw3.getMessage = function(key, defaultValue){
		var localeHandler = mw3.getAutowiredObject("org.uengine.codi.mw3.model.Locale");

		if(localeHandler!=null && localeHandler.resourceBundle != null){
			var message = localeHandler.resourceBundle[key];
			if(message)
				return message;
		}

		if(defaultValue)
			return defaultValue;

		return key;
	}


Implementing message handler

package org.uengine.codi.mw3.model;

import java.util.Hashtable;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.metaworks.annotation.ServiceMethod;
	
public class Locale{
	
	static Hashtable<String, Properties> webMessageBundles = new Hashtable<String, Properties>();

	String language;
		public String getLanguage() {
			return language;
		}	
		public void setLanguage(String language) {
			this.language = language;
		}
		
	Properties resourceBundle;		
		public Properties getResourceBundle() {
			return resourceBundle;
		}	
		public void setResourceBundle(Properties resourceBundle) {
			this.resourceBundle = resourceBundle;
		}

	public Locale(){
		
	}
	// TODO : static method 로 getString 가능하게 처리		
	public Locale(Session session){
		this.setLanguage(session.getEmployee().getLocale());
	}
	
	@ServiceMethod(payload={"language"})
	public void load(){		
		// default language setting
		String language = "en";		
		if(this.getLanguage() != null)
			language = this.getLanguage();
		
		if("en-US".equals(language))	language = "en"; 
		
		if (!webMessageBundles.containsKey(language)) {
			java.util.Locale locale = new java.util.Locale(language);
			PropertyResourceBundle propertyResourceBundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle("org.uengine.messages", locale, getClass().getClassLoader());
			
			Properties props = new Properties();
			for(String key : propertyResourceBundle.keySet()){
				props.put(key, propertyResourceBundle.getString(key));
			}
			
			webMessageBundles.put(language, props);
		}
		
		resourceBundle = webMessageBundles.get(language);		
	}
	
	public String getString(String... keys) throws Exception {
	
		String message = keys[0];
		
		if(message.startsWith("$")){
			message = resourceBundle.getProperty(message.substring(1));			
			message = (message == null) ? keys[0] : message;
		}
		
		 Pattern p = Pattern.compile("%s");
		 CharSequence cs = new StringBuffer(message);
		 Matcher m = p.matcher(cs);
		 int count = 0;
		 while(m.find()) {
			 count++;
		 }	
	
		 if(count > 0 && count != keys.length - 1)
				throw new Exception("$NotEqualToFormatCountAndArgumentCount");
		
		if(keys.length == 1)
			return message;	
		
		StringBuffer sb = new StringBuffer();
		count = 0;
		m.reset();
        while (m.find()) {
        	String key = keys[1 + count++];
			key = this.getString(key);
        	
            m.appendReplacement(sb, key);
        }
        m.appendTail(sb);
        
        return sb.toString();
	}
}

Example EJS

<%=mw3.localize("$file.attach")%>

messages.properties

file.attach=File Attach
...

messages_ko.properties

file.attach=\uD30C\uC77C\uCCA8\uBD80
...