Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Daniel Murphy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
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;
			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) {
		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) {
		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) {
		int amp = value.indexOf('&');
		if (amp == -1 || amp == value.length() - 1) {
			return null;
		}
		return amp;
	}
	// the resource bundle to use
	private final static ResourceBundle resourceBundle;
	static {
		try {
			bundle = ResourceBundle.getBundle("locale/java-simple-mvc");
catch (MissingResourceException mre) {
			try {
				bundle = ResourceBundle.getBundle("java-simple-mvc");
catch (MissingResourceException mre2) {
				..println("Could not find locale file.");
				bundle = new ResourceBundle() {
					protected Object handleGetObject(String key) {
						return key;
					}
					@SuppressWarnings("unchecked")
					public Enumeration getKeys() {
					}
					@SuppressWarnings("unchecked")
					private final Enumeration EMPTY_ENUMERATION = new Enumeration() {
						public boolean hasMoreElements() {
							return false;
						}
						public Object nextElement() {
						}
					};
				};
			}
		}
		 = bundle;
	}
New to GrepCode? Check out our FAQ X