<% for (Message m : ms) {%>
messages['<%=mkey%>'] = '<%=mvalue%>';
<% } %>
这样 key和value是private的,就自己上getter吧。
在项目中经常要用到将字符串解析成Locale,但是没有一个比较好用的类。
java本身提供了3个构造函数
Locale(langugae)
locale(language,country)
Locale(String language, String country, String variant)
Apache 有个 LocaleUtilstoLocale(String language)但是不支持最后2个字符为小写的,如:zh_CN支持, zh_cn就不支持。
[java] view plaincopyprint
import javautilLocale;
public class LocaleUtil
{
private final static Locale DEFAULT_LOCALE = LocaleENGLISH;
public final static String IETF_SEPARATOR = "-";
public final static String SEPARATOR = "_";
public final static String EMPTY_STRING = "";
public static Locale toLocale( String language )
{
if( !StringUtilisNullOrEmpty( language ) )
{
return langToLocale( language, SEPARATOR );
}
return DEFAULT_LOCALE;
}
public static Locale langToLocale( String lang , String separator )
{
if( StringUtilisNullOrEmpty( lang ) )
{
return DEFAULT_LOCALE;
}
String language = EMPTY_STRING;
String country = EMPTY_STRING;
String variant = EMPTY_STRING;
int i1 = langindexOf( separator );
if ( i1 < 0 )
{
language = lang;
} else
{
language = langsubstring(0, i1);
++i1;
int i2 = langindexOf( separator, i1);
if (i2 < 0)
{
country = langsubstring(i1);
} else
{
country = langsubstring(i1, i2);
variant = langsubstring(i2+1);
}
}
if(languagelength() == 2)
{
language = languagetoLowerCase();
}else
{
language = EMPTY_STRING;
}
if(countrylength() == 2)
{
country = countrytoUpperCase();
}else
{
country = EMPTY_STRING;
}
if( (variantlength() > 0) &&
((languagelength() == 2) ||(countrylength() == 2)) )
{
variant = varianttoUpperCase();
}else
{
variant = EMPTY_STRING;
}
return new Locale(language, country, variant );
}
}
import javautilLocale;
public class LocaleUtil
{
private final static Locale DEFAULT_LOCALE = LocaleENGLISH;
public final static String IETF_SEPARATOR = "-";
public final static String SEPARATOR = "_";
public final static String EMPTY_STRING = "";
public static Locale toLocale( String language )
{
if( !StringUtilisNullOrEmpty( language ) )
{
return langToLocale( language, SEPARATOR );
}
return DEFAULT_LOCALE;
}
public static Locale langToLocale( String lang , String separator )
{
if( StringUtilisNullOrEmpty( lang ) )
{
return DEFAULT_LOCALE;
}
String language = EMPTY_STRING;
String country = EMPTY_STRING;
String variant = EMPTY_STRING;
int i1 = langindexOf( separator );
if ( i1 < 0 )
{
language = lang;
} else
{
language = langsubstring(0, i1);
++i1;
int i2 = langindexOf( separator, i1);
if (i2 < 0)
{
country = langsubstring(i1);
} else
{
country = langsubstring(i1, i2);
variant = langsubstring(i2+1);
}
}
if(languagelength() == 2)
{
language = languagetoLowerCase();
}else
{
language = EMPTY_STRING;
}
if(countrylength() == 2)
{
country = countrytoUpperCase();
}else
{
country = EMPTY_STRING;
}
if( (variantlength() > 0) &&
((languagelength() == 2) ||(countrylength() == 2)) )
{
variant = varianttoUpperCase();
}else
{
variant = EMPTY_STRING;
}
return new Locale(language, country, variant );
}
}
注意:
public class StringUtil
{
public static boolean isNullOrEmpty(String target) {
return target == null || ""equals(target);
}
}
public static boolean isZh(Context context) {
Locale locale = contextgetResources()getConfiguration()locale;
String language = localegetLanguage();
if (languageendsWith("zh"))
return true;
else
return false;
}
下面是判断国家:
中文:getResources()getConfiguration()localegetCountry()equals("CN")
繁体中文: getResources()getConfiguration()localegetCountry()equals("TW")
英文(英式):getResources()getConfiguration()localegetCountry()equals("UK")
英文(美式):getResources()getConfiguration()localegetCountry()equals("US")
如果不清楚当前国家的简写,可以直接
Systemout(getResources()getConfiguration()localegetCountry());打印出来即可。
LocalegetDefault()获取系统默认的区域信息,比如:语言, 在中国默认为中文
本地默认的区域信息一般你装的是中文的系统,比如xp并且区域为中国语言为中文则为zh_CN。如果是中文就是本地化了。
以上就是关于springmvc中怎么获取到当前locale下的所有message全部的内容,包括:springmvc中怎么获取到当前locale下的所有message、java中 locale类 及相关方法、Android如何获取当前 *** 作系统的语言等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)