Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Created at Jun 8, 2010, 1:22:04 AM
  
  package com.dmurph.mvc;
  
  

Author(s):
Daniel Murphy
 
 public class I18n {

Get the text for this key. The text has no special control characters in it, and can be presented to the user.

For example, if the localization file has the line copy = &Copy [accel C], the string "Copy" is returned.

Parameters:
key the key to look up in the localization file
Returns:
the text
 
 	public static String getText(String key) {
 		String value = null;
 		
 		try {
 			value = .getString(key);
 		} catch (MissingResourceException e) {
 			..println("Unable to find the translation for the key: " + key);
 			return key;
 		};
 		
 		StringBuffer buf = new StringBuffer();
 		
 		int n = value.length();
 		boolean ignore = false;
 		for (int i = 0; i < ni++) {
 			char c = value.charAt(i);
 			switch (c) {
 				case '&' :
 					continue;
 				case '[' :
 					ignore = true;
 					break;
 				case ']' :
 					ignore = false;
 					break;
 				default :
 					if (!ignore) {
 						buf.append(c);
 					}
 			}
 		}
 		
 		return buf.toString().trim();
 	}

Look up a translation key with each {n} replaced with a value in the array

Parameters:
argKey
argReplacing
Returns:
 
 	public static String getText(String argKeyString... argReplacing) {
 		String text = getText(argKey);
 		
 		for (int i = 0; i < argReplacing.lengthi++) {
 			text = text.replace("{" + i + "}"argReplacing[i]);
 		}
 		
 		return text;
 	}

Gets and integer value of the key. If the value isn't able to parse as an integer, null is returned

Parameters:
argKey
Returns:
 
 	public static Integer getInteger(String argKey){
 		String text = getText(argKey);
 		try{
 			int i = Integer.parseInt(text);
 			return i;
 		}catchNumberFormatException e){
 			return null;
 		}
 	}

Gets the boolean value of the key. If the value is anything but "true", then false is returned

Parameters:
argKey
Returns:
	public static Boolean getBoolean(String argKey){
		String text = getText(argKey);
		try{
			boolean tf = Boolean.parseBoolean(text);
			return tf;
			return null;
		}
	}

Get the keystroke string for this key. This string can be passed directly to the Keystroke.getKeyStroke() method.

For example, if the localization file has the line copy = &Copy [accel C], the string "control C" is returned (or on the Mac, "meta C").

If the string has no [keystroke] listed, null is returned.

Parameters:
key the key to look up in the localization file
Returns:
the keystroke
	public static KeyStroke getKeyStroke(String key) {
		String value = .getString(key);
		int left = value.indexOf('[');
		int right = value.indexOf(']');
		if (left == -1 || right == -1) {
			return null;
		}
		String stroke = value.substring(left + 1, right).trim();
		// accel = command (in java-ese: "meta") on mac, control on pc
		// String accel = (App.platform.isMac() ? "meta" : "control");
		// stroke = StringUtils.substitute(stroke, "accel", accel);
		return KeyStroke.getKeyStroke(stroke);
	}

Get the mnemonic character this key.

For example, if the localization file has the line copy = &Copy [accel C], the character "C" is returned.

If the string has no &mnemonic listed, null is returned.

Parameters:
key the key to look up in the localization file
Returns:
the integer representing the mnemonic character
	public static Integer getMnemonic(String key) {
		String value = .getString(key);
		int amp = value.indexOf('&');
		if (amp == -1 || amp == value.length() - 1) {
			return null;
		}
		return new Integer(Character.toUpperCase(value.charAt(amp + 1)));
	}

Get the position of the mnemonic character in the string Used for setDisplayedMnemonicIndex

Parameters:
key
Returns:
an Integer, or null
	public static Integer getMnemonicPosition(String key) {
		String value = .getString(key);
		int amp = value.indexOf('&');
		if (amp == -1 || amp == value.length() - 1) {
			return null;
		}
		return amp;
	}
	// the resource bundle to use
	private final static ResourceBundle msg;
	static {
		try {
			bundle = ResourceBundle.getBundle("locale/java-simple-mvc");
catch (MissingResourceException mre) {
			try {
				bundle = ResourceBundle.getBundle("java-simple-mvc");
catch (MissingResourceException mre2) {
				bundle = new DefaultResourceBundle();
			}
		}
		 = bundle;
	}
New to GrepCode? Check out our FAQ X