下面列出了java.util.Locale#getVariant ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean isSupportedCalendarLocale(Locale locale) {
Locale base = locale;
if (base.hasExtensions() || base.getVariant() != "") {
base = new Locale.Builder()
.setLocale(locale)
.clearExtensions()
.build();
}
if (!supportedLocaleSet.contains(base)) {
return false;
}
String requestedCalType = locale.getUnicodeLocaleType("ca");
String nativeCalType =
getCalendarID(base.toLanguageTag()).replaceFirst("gregorian", "gregory");
if (requestedCalType == null) {
return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
} else {
return requestedCalType.equals(nativeCalType);
}
}
/**
* This method tries its best to return a valid {@link Locale} object from the TTS-specific
* Locale input (returned by {@link TextToSpeech#getLanguage}
* and {@link TextToSpeech#getDefaultLanguage}). A TTS Locale language field contains
* a three-letter ISO 639-2/T code (where a proper Locale would use a two-letter ISO 639-1
* code), and the country field contains a three-letter ISO 3166 country code (where a proper
* Locale would use a two-letter ISO 3166-1 code).
*
* This method tries to convert three-letter language and country codes into their two-letter
* equivalents. If it fails to do so, it keeps the value from the TTS locale.
*/
public static Locale normalizeTTSLocale(Locale ttsLocale) {
String language = ttsLocale.getLanguage();
if (!TextUtils.isEmpty(language)) {
String normalizedLanguage = sNormalizeLanguage.get(language);
if (normalizedLanguage != null) {
language = normalizedLanguage;
}
}
String country = ttsLocale.getCountry();
if (!TextUtils.isEmpty(country)) {
String normalizedCountry= sNormalizeCountry.get(country);
if (normalizedCountry != null) {
country = normalizedCountry;
}
}
return new Locale(language, country, ttsLocale.getVariant());
}
private static String toResourceName(
String baseName,
Locale locale
) {
String language = LanguageMatch.getAlias(locale);
String country = locale.getCountry();
String variant = locale.getVariant();
StringBuilder sb = new StringBuilder(baseName.length() + 20);
sb.append(baseName.replace('.', '/'));
if (!language.isEmpty()) {
sb.append('_').append(language);
if (!variant.isEmpty()) {
sb.append('_').append(country).append('_').append(variant);
} else if (!country.isEmpty()) {
sb.append('_').append(country);
}
}
return sb.append(".properties").toString();
}
static String bestFitFormat(Locale locale, Message[] messages) {
String lang = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
String[] localeList = new String[] { "", lang, lang + "_" + country, lang + "_" + country + "_" + variant };
String[] formats = new String[] { "", "", "", "", "", "" };
int mostFit = 0;
for (Message message : messages) {
String localeStr = message.locale();
for (int i = mostFit; i < localeList.length; i++) {
if (localeList[i].equals(localeStr)) {
mostFit++;
formats[mostFit] = message.value();
break;
}
}
}
return formats[mostFit];
}
private static String resourceBundleSuffixFor(Locale locale) {
String suffix = "";
if (Locale.ROOT != locale) {
if (!locale.getLanguage().isEmpty()) {
suffix += "_" + locale.getLanguage();
}
if (!locale.getScript().isEmpty()) {
suffix += "_" + locale.getScript();
}
if (!locale.getCountry().isEmpty()) {
suffix += "_" + locale.getCountry();
}
if (!locale.getVariant().isEmpty()) {
suffix += "_" + locale.getVariant();
}
}
suffix += ".properties";
return suffix;
}
private static boolean isSupportedCalendarLocale(Locale locale) {
Locale base = locale;
if (base.hasExtensions() || base.getVariant() != "") {
base = new Locale.Builder()
.setLocale(locale)
.clearExtensions()
.build();
}
if (!supportedLocaleSet.contains(base)) {
return false;
}
String requestedCalType = locale.getUnicodeLocaleType("ca");
String nativeCalType =
getCalendarID(base.toLanguageTag()).replaceFirst("gregorian", "gregory");
if (requestedCalType == null) {
return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
} else {
return requestedCalType.equals(nativeCalType);
}
}
/**
* Calculate the filenames for the given bundle basename and Locale,
* appending language code, country code, and variant code.
* E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO",
* "messages_de_AT", "messages_de".
* <p>Follows the rules defined by {@link java.util.Locale#toString()}.
* @param basename the basename of the bundle
* @param locale the locale
* @return the List of filenames to check
*/
protected List<String> calculateFilenamesForLocale(String basename, Locale locale) {
List<String> result = new ArrayList<String>(3);
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
StringBuilder temp = new StringBuilder(basename);
temp.append('_');
if (language.length() > 0) {
temp.append(language);
result.add(0, temp.toString());
}
temp.append('_');
if (country.length() > 0) {
temp.append(country);
result.add(0, temp.toString());
}
if (variant.length() > 0 && (language.length() > 0 || country.length() > 0)) {
temp.append('_').append(variant);
result.add(0, temp.toString());
}
return result;
}
public static void main(String[] args) throws Exception {
String language = "en";
String country = "US";
String variant = "socal";
Locale aLocale = new Locale(language, country, variant);
String localeVariant = aLocale.getVariant();
if (localeVariant.equals(variant)) {
System.out.println("passed");
} else {
System.out.println("failed");
throw new Exception("Bug4210525 test failed.");
}
}
/**
* Calculate the filenames for the given bundle basename and Locale,
* appending language code, country code, and variant code.
* E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO",
* "messages_de_AT", "messages_de".
* <p>Follows the rules defined by {@link java.util.Locale#toString()}.
* @param basename the basename of the bundle
* @param locale the locale
* @return the List of filenames to check
*/
protected List<String> calculateFilenamesForLocale(String basename, Locale locale) {
List<String> result = new ArrayList<String>(3);
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
StringBuilder temp = new StringBuilder(basename);
temp.append('_');
if (language.length() > 0) {
temp.append(language);
result.add(0, temp.toString());
}
temp.append('_');
if (country.length() > 0) {
temp.append(country);
result.add(0, temp.toString());
}
if (variant.length() > 0 && (language.length() > 0 || country.length() > 0)) {
temp.append('_').append(variant);
result.add(0, temp.toString());
}
return result;
}
private static boolean isSupportedCalendarLocale(Locale locale) {
Locale base = locale;
if (base.hasExtensions() || base.getVariant() != "") {
// strip off extensions and variant.
base = new Locale.Builder()
.setLocale(locale)
.clearExtensions()
.build();
}
if (!supportedLocaleSet.contains(base)) {
return false;
}
int calid = getCalendarID(base.toLanguageTag());
if (calid <= 0 || calid >= calIDToLDML.length) {
return false;
}
String requestedCalType = locale.getUnicodeLocaleType("ca");
String nativeCalType = calIDToLDML[calid]
.replaceFirst("_.*", ""); // remove locale part.
if (requestedCalType == null) {
return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
} else {
return requestedCalType.equals(nativeCalType);
}
}
public static void main(String[] args) throws Exception {
String language = "en";
String country = "US";
String variant = "socal";
Locale aLocale = new Locale(language, country, variant);
String localeVariant = aLocale.getVariant();
if (localeVariant.equals(variant)) {
System.out.println("passed");
} else {
System.out.println("failed");
throw new Exception("Bug4210525 test failed.");
}
}
private String getLocaleString(Locale locale)
{
if (locale == null)
{
return "en";
}
String languageCode = locale.getLanguage();
String countryCode = locale.getCountry();
String variantCode = locale.getVariant();
if (languageCode.length() == 0 && countryCode.length() == 0)
{
return "en";
}
StringBuilder result = new StringBuilder(11);
result.append(languageCode);
if (countryCode.length() > 0 || variantCode.length() > 0)
{
result.append('_');
}
result.append(countryCode);
if (variantCode.length() > 0)
{
result.append('_');
}
result.append(variantCode);
return result.toString();
}
@Test
public void testParseAllLocales() {
final Locale[] locales = Locale.getAvailableLocales();
int failures = 0;
for (final Locale l : locales) {
// Check if it's possible to recreate the Locale using just the standard constructor
final Locale locale = new Locale(l.getLanguage(), l.getCountry(), l.getVariant());
if (l.equals(locale)) { // it is possible for LocaleUtils.toLocale to handle these Locales
String str = l.toString();
// Look for the script/extension suffix
int suff = str.indexOf("_#");
if (suff == - 1) {
suff = str.indexOf("#");
}
if (suff >= 0) { // we have a suffix
try {
Locales.toLocale(str); // should cause IAE
System.out.println("Should not have parsed: " + str);
failures++;
continue; // try next Locale
} catch (final IllegalArgumentException iae) {
// expected; try without suffix
str = str.substring(0, suff);
}
}
final Locale loc = Locales.toLocale(str);
if (!l.equals(loc)) {
System.out.println("Failed to parse: " + str);
failures++;
}
}
}
if (failures > 0) {
fail("Failed "+failures+" test(s)");
}
}
public static void main(String[] args) throws Exception {
String language = "en";
String country = "US";
String variant = "socal";
Locale aLocale = new Locale(language, country, variant);
String localeVariant = aLocale.getVariant();
if (localeVariant.equals(variant)) {
System.out.println("passed");
} else {
System.out.println("failed");
throw new Exception("Bug4210525 test failed.");
}
}
private static boolean isSupportedCalendarLocale(Locale locale) {
Locale base = locale;
if (base.hasExtensions() || base.getVariant() != "") {
// strip off extensions and variant.
base = new Locale.Builder()
.setLocale(locale)
.clearExtensions()
.build();
}
if (!supportedLocaleSet.contains(base)) {
return false;
}
int calid = getCalendarID(base.toLanguageTag());
if (calid <= 0 || calid >= calIDToLDML.length) {
return false;
}
String requestedCalType = locale.getUnicodeLocaleType("ca");
String nativeCalType = calIDToLDML[calid]
.replaceFirst("_.*", ""); // remove locale part.
if (requestedCalType == null) {
return Calendar.getAvailableCalendarTypes().contains(nativeCalType);
} else {
return requestedCalType.equals(nativeCalType);
}
}
public static void main(String[] args) throws Exception {
String language = "en";
String country = "US";
String variant = "socal";
Locale aLocale = new Locale(language, country, variant);
String localeVariant = aLocale.getVariant();
if (localeVariant.equals(variant)) {
System.out.println("passed");
} else {
System.out.println("failed");
throw new Exception("Bug4210525 test failed.");
}
}
public static TGResourceBundle getBundle(TGContext context, String baseName, Locale locale){
Properties properties = new Properties();
String bundleName = baseName.replace('.','/');
String bundleExtension = ".properties";
// load default
TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
// load language
bundleName += "_";
if(locale.getLanguage() != null && locale.getLanguage().length() > 0){
bundleName += locale.getLanguage();
TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
}
// load country
bundleName += "_";
if(locale.getCountry() != null && locale.getCountry().length() > 0){
bundleName += locale.getCountry();
TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
}
// load variant
bundleName += "_";
if(locale.getVariant() != null && locale.getVariant().length() > 0){
bundleName += locale.getVariant();
TGResourceBundle.loadResources(context, (bundleName + bundleExtension ), properties);
}
return new TGResourceBundle(locale, properties);
}
public static void main(String[] args) {
if (args.length != 1) {
throw new RuntimeException("expected locale needs to be specified");
}
Locale locale = Locale.getDefault();
// don't use Locale.toString - it's bogus
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
String localeID = null;
if (variant.length() > 0) {
localeID = language + "_" + country + "_" + variant;
} else if (country.length() > 0) {
localeID = language + "_" + country;
} else {
localeID = language;
}
if (localeID.equals(args[0])) {
System.out.println("Correctly set from command line: " + localeID);
} else {
throw new RuntimeException("expected default locale: " + args[0]
+ ", actual default locale: " + localeID);
}
}
private String getLocaleString(Locale locale)
{
if (locale == null)
{
return "en";
}
String languageCode = locale.getLanguage();
String countryCode = locale.getCountry();
String variantCode = locale.getVariant();
if (languageCode.length() == 0 && countryCode.length() == 0)
{
return "en";
}
StringBuilder result = new StringBuilder(11);
result.append(languageCode);
if (countryCode.length() > 0 || variantCode.length() > 0)
{
result.append('_');
}
result.append(countryCode);
if (variantCode.length() > 0)
{
result.append('_');
}
result.append(variantCode);
return result.toString();
}
private static String localeToRFC5646LanguageTag(final Locale locale) throws IllegalArgumentException {
// language[-variant][-region]
String result = locale.getLanguage();
if (locale.getVariant().length() > 0) {
result = result + "-" + locale.getVariant(); //$NON-NLS-1$
}
if (locale.getCountry().length() > 0) {
result = result + "-" + locale.getCountry(); //$NON-NLS-1$
}
return result;
}