Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.locale;
 
 import java.util.List;
 
as much as possible DivConq attempts to avoid using Java's locales because they are not extensible. Translations rely entirely on our own Hub dictionaries. Formatting of dates and numbers still uses some Java locales for now.

Author(s):
Andy
 
 public class LocaleUtil {

translate the token and parameters using the provided locale

Parameters:
locale name of the local to use
token name of the dictionary item to lookup
params parameters to use when formatting the output
Returns:
translated and formatted text
 
 	static public String tr(String localeString tokenObject... params) {
 		
 		if (loc != null)
 			return loc.tr(localetokenparams);
 		
 		return null;
 	}

translate a token and parameters using the provided locale. if the first parameter is numeric and 1 then use the singular token otherwise lookup the plural token

Parameters:
locale name of the local to use
pluraltoken name of the dictionary item to lookup
singulartoken name of the dictionary item to lookup
params parameters to use when formatting the output
Returns:
translated and formatted text
 
 	static public String trp(String localeString pluraltokenString singulartokenObject... params) {
 		
 		if (loc != null)
 			return loc.trp(localepluraltokensingulartokenparams);
 		
 		return null;
 	}

Messages contain locale metadata, use that to try to get a Java style Locale

Parameters:
m message with metadata
Returns:
java type locale
 
 	static public Locale getLocale(Message m) {		
 		return LocaleUtil.getLocale(m.getFieldAsString("Locale"));
 	}

Try to get a Java style Locale for a locale id string

Parameters:
ll locale id
Returns:
java type locale
 
 	static public Locale getLocale(String ll) {		
 		if (StringUtil.isEmpty(ll)) 
 			return null;
 
 		if (ll.contains("_")) {
 			String[] lp = ll.split("_");
 			return new Locale(lp[0], lp[1].toUpperCase());
 		}
 		
 		return new Locale(ll);
	}

Try to get a DivConq style Locale for a locale id string

Parameters:
ll locale id
Returns:
DivConq type locale
	static public LocaleInfo getLocaleInfo(String ll) {		
		if (StringUtil.isEmpty(ll)) 
			return null;
		if (loc != null)
			return loc.getLocalization(ll);
		return null;
	}
	/*
	// TODO include region - TODO get aspects of this idea working
	public static ViewLocale lookup(String lang) {
		if (!ViewLocale.locales.containsKey(lang)) {
			Locale l = null;
			boolean ltr = false;
			if (lang.equals("x-pig-latin")) {
				l = new Locale("it");		// Italian, as an example only
			}
			else if (lang.equals("x-hsilgne")) {
				l = new Locale("iw");		// Hebrew, as an example only
				ltr = true;
			}
			else {
				ltr = ("ar".equals(lang) || "iw".equals(lang));				
				l = new Locale(lang);
			}
			ViewLocale ml = new ViewLocale(lang, l, ltr);
			ViewLocale.locales.put(lang, ml);
			return ml;
		}
		return ViewLocale.locales.get(lang);
	}
	*/	

Returns:
the default locale id string
	public static String getDefaultLocale() {
		if (loc != null)
			return loc.getDefault();
		return Locale.getDefault().toString();
	}

Returns:
DivConq type locale for default locale
	public static LocaleInfo getDefaultLocaleInfo() {
		if (loc != null)
			return loc.getLocalization(null);
		return null;
	}

Get list of locale ids (lang id and lang_region id) to look for based on a preferred locale id

Parameters:
locale id string of preferred locale
Returns:
list of possible locale ids to use
	static public List<StringgetSearchPath(String locale) {
		return LocaleUtil.getSearchPath(LocaleUtil.getLocaleInfo(locale));		
	}

Get list of locale ids (lang id and lang_region id) to look for based on a preferred locale id

Parameters:
locale DivConq style locale object of preferred locale
Returns:
list of possible locale ids to use
	static public List<StringgetSearchPath(LocaleInfo locale) {
		List<Stringpath = new ArrayList<String>();
		if (locale != null) {
			String tn = locale.getName();
			path.add(tn);
			if (tn.indexOf("_") > -1) 
				path.add(tn.substring(0, tn.indexOf("_")));
		}
		// add default
		//path.add(LocaleUtil.getDefaultLocale());
		if (locale.isRightToLeft())
			path.add("rtl");
		// add default
		path.add("common");
		return path;
	}
New to GrepCode? Check out our FAQ X