Java Code Examples for java.util.Locale.getCountry()

The following are Jave code examples for showing how to use getCountry() of the java.util.Locale class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: Response.java   View Source Code Vote up 6 votes
/**
 * Called explicitely by user to set the Content-Language and
 * the default encoding
 */
public void setLocale(Locale locale) {

    if (locale == null) {
        return;  // throw an exception?
    }

    // Save the locale for use by getLocale()
    this.locale = locale;

    // Set the contentLanguage for header output
    contentLanguage = locale.getLanguage();
    if ((contentLanguage != null) && (contentLanguage.length() > 0)) {
        String country = locale.getCountry();
        StringBuffer value = new StringBuffer(contentLanguage);
        if ((country != null) && (country.length() > 0)) {
            value.append('-');
            value.append(country);
        }
        contentLanguage = value.toString();
    }

}
 
Example 2
Project: FreeCol   File: FreeColDirectories.java   View Source Code Vote up 6 votes
/**
 * Gets a list containing the names of all possible message files
 * for a locale.
 *
 * @param prefix The file name prefix.
 * @param suffix The file name suffix.
 * @param locale The {@code Locale} to generate file names for.
 * @return A list of candidate file names.
 */
public static List<String> getLocaleFileNames(String prefix,
                                              String suffix,
                                              Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();

    List<String> result = new ArrayList<>(4);

    if (!language.isEmpty()) language = "_" + language;
    if (!country.isEmpty()) country = "_" + country;
    if (!variant.isEmpty()) variant = "_" + variant;

    result.add(prefix + suffix);
    String filename = prefix + language + suffix;
    if (!result.contains(filename)) result.add(filename);
    filename = prefix + language + country + suffix;
    if (!result.contains(filename)) result.add(filename);
    filename = prefix + language + country + variant + suffix;
    if (!result.contains(filename)) result.add(filename);
    return result;
}
 
Example 3
Project: chromium-net-for-android   File: LocaleUtils.java   View Source Code Vote up 6 votes
/**
 * @return the string for the given locale, translating
 * Android deprecated language codes into the modern ones
 * used by Chromium.
 */
public static String getLocale(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();

    // Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the
    // updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino.
    // So apply a mapping.
    // See http://developer.android.com/reference/java/util/Locale.html
    if ("iw".equals(language)) {
        language = "he";
    } else if ("in".equals(language)) {
        language = "id";
    } else if ("tl".equals(language)) {
        language = "fil";
    }
    return country.isEmpty() ? language : language + "-" + country;
}
 
Example 4
Project: lams   File: NumberUtil.java   View Source Code Vote up 6 votes
/**
    * Convert a string back into a Float. Assumes string was formatted using formatLocalisedNumber originally. Should
    * ensure that it is using the same locale/number format as when it was formatted. If no locale is suppied, it will
    * use the server's locale
    *
    * Need to strip out any spaces as spaces are valid group separators in some European locales (e.g. Polish) but they
    * seem to come back from Firefox as a plain space rather than the special separating space.
    */
   public static Float getLocalisedFloat(String inputStr, Locale locale) {
String numberStr = inputStr;
if (numberStr != null) {
    numberStr = numberStr.replace(" ", "");
}
if ((numberStr != null) && (numberStr.length() > 0)) {
    Locale useLocale = locale != null ? locale : NumberUtil.getServerLocale();
    NumberFormat format = NumberFormat.getInstance(useLocale);
    ParsePosition pp = new ParsePosition(0);
    Number num = format.parse(numberStr, pp);
    if ((num != null) && (pp.getIndex() == numberStr.length())) {
	return num.floatValue();
    }
}
throw new NumberFormatException("Unable to convert number " + numberStr + "to float using locale "
	+ locale.getCountry() + " " + locale.getLanguage());
   }
 
Example 5
Project: PlusGram   File: LocaleController.java   View Source Code Vote up 6 votes
public static String getLocaleStringIso639() {
    Locale locale = getInstance().getSystemDefaultLocale();
    if (locale == null) {
        return "fa";
    }
    String languageCode = locale.getLanguage();
    String countryCode = locale.getCountry();
    String variantCode = locale.getVariant();
    if (languageCode.length() == 0 && countryCode.length() == 0) {
        return "fa";
    }
    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();
}
 
Example 6
Project: OpenJSharp   File: ExecutableInputMethodManager.java   View Source Code Vote up 5 votes
private String createLocalePath(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    String localePath = null;
    if (!variant.equals("")) {
        localePath = "_" + language + "/_" + country + "/_" + variant;
    } else if (!country.equals("")) {
        localePath = "_" + language + "/_" + country;
    } else {
        localePath = "_" + language;
    }

    return localePath;
}
 
Example 7
Project: black-mirror   File: SpeechService.java   View Source Code Vote up 5 votes
private String getDefaultLanguageCode() {
    final Locale locale = Locale.getDefault();
    final StringBuilder language = new StringBuilder(locale.getLanguage());
    final String country = locale.getCountry();
    if (!TextUtils.isEmpty(country)) {
        language.append("-");
        language.append(country);
    }
    return language.toString();
}
 
Example 8
Project: jdk8u-jdk   File: ExecutableInputMethodManager.java   View Source Code Vote up 5 votes
private String createLocalePath(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    String localePath = null;
    if (!variant.equals("")) {
        localePath = "_" + language + "/_" + country + "/_" + variant;
    } else if (!country.equals("")) {
        localePath = "_" + language + "/_" + country;
    } else {
        localePath = "_" + language;
    }

    return localePath;
}
 
Example 9
Project: Equella   File: MultiEditBox.java   View Source Code Vote up 5 votes
@Override
protected void prepareModel(RenderContext info)
{
	super.prepareModel(info);
	final MultiEditBoxState state = getState(info);
	Map<String, HtmlValueState> map = new LinkedHashMap<String, HtmlValueState>();

	TreeSet<Locale> languages = new TreeSet<Locale>(localeComparator);
	languages.addAll(getContributeLocales());
	languages.addAll(getBundleLocales(info));

	for( Locale locale : languages )
	{
		String id = locale.toString();
		HtmlValueState valState = langMap.getValueState(info, id);

		// Build format into: Language [(country)] where country
		String displayString = Constants.BLANK;

		String displayLanguage = locale.getDisplayLanguage();
		String displayCountry = locale.getCountry();
		displayString = !Check.isEmpty(displayCountry) ? MessageFormat.format(
			"{0} ({1})", displayLanguage, displayCountry) : displayLanguage; //$NON-NLS-1$

		valState.setLabel(new TextLabel(displayString));
		map.put(id, valState);
	}
	state.setSize(size);
	state.setLocaleMap(map);
}
 
Example 10
Project: openjdk-jdk10   File: MyResourcesProvider.java   View Source Code Vote up 5 votes
@Override
protected String toBundleName(String baseName, Locale locale) {
    StringBuilder sb = new StringBuilder(baseName);
    String lang = locale.getLanguage();
    if (!lang.isEmpty()) {
        sb.append('_').append(lang);
        String country = locale.getCountry();
        if (!country.isEmpty()) {
            sb.append('_').append(country);
        }
    }
    return sb.toString();
}
 
Example 11
Project: jdk8u-jdk   File: TrueTypeFont.java   View Source Code Vote up 5 votes
static String getCodePage() {

        if (defaultCodePage != null) {
            return defaultCodePage;
        }

        if (FontUtilities.isWindows) {
            defaultCodePage =
                (String)java.security.AccessController.doPrivileged(
                   new sun.security.action.GetPropertyAction("file.encoding"));
        } else {
            if (languages.length != codePages.length) {
                throw new InternalError("wrong code pages array length");
            }
            Locale locale = sun.awt.SunToolkit.getStartupLocale();

            String language = locale.getLanguage();
            if (language != null) {
                if (language.equals("zh")) {
                    String country = locale.getCountry();
                    if (country != null) {
                        language = language + "_" + country;
                    }
                }
                for (int i=0; i<languages.length;i++) {
                    for (int l=0;l<languages[i].length; l++) {
                        if (language.equals(languages[i][l])) {
                            defaultCodePage = codePages[i];
                            return defaultCodePage;
                        }
                    }
                }
            }
        }
        if (defaultCodePage == null) {
            defaultCodePage = "";
        }
        return defaultCodePage;
    }
 
Example 12
Project: chromium-for-android-56-debug-video   File: PwsClientImpl.java   View Source Code Vote up 5 votes
/**
 * Get the language code for the default locales and prepend it to the Accept-Language string
 * if it isn't already present. The logic should match PrependToAcceptLanguagesIfNecessary in
 * chrome/browser/android/preferences/pref_service_bridge.cc
 * @param locales A comma separated string that represents a list of default locales.
 * @param acceptLanguages The default language list for the language of the user's locales.
 * @return An updated language list.
 */
@VisibleForTesting
static String prependToAcceptLanguagesIfNecessary(String locales, String acceptLanguages) {
    String localeStrings = locales + "," + acceptLanguages;
    String[] localeList = localeStrings.split(",");

    ArrayList<Locale> uniqueList = new ArrayList<>();
    for (String localeString : localeList) {
        Locale locale = LocaleUtils.forLanguageTag(localeString);
        if (uniqueList.contains(locale) || locale.getLanguage().isEmpty()) {
            continue;
        }
        uniqueList.add(locale);
    }

    // If language is not in the accept languages list, also add language code.
    // A language code should only be inserted after the last languageTag that
    // contains that language.
    // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
    // with Chrome but may fail on arbitrary lists of language tags due to
    // differences in case and whitespace.
    HashSet<String> seenLanguages = new HashSet<>();
    ArrayList<String> outputList = new ArrayList<>();
    for (int i = uniqueList.size() - 1; i >= 0; i--) {
        Locale localeAdd = uniqueList.get(i);
        String languageAdd = localeAdd.getLanguage();
        String countryAdd = localeAdd.getCountry();

        if (!seenLanguages.contains(languageAdd)) {
            seenLanguages.add(languageAdd);
            outputList.add(languageAdd);
        }
        if (!countryAdd.isEmpty()) {
            outputList.add(LocaleUtils.toLanguageTag(localeAdd));
        }
    }
    Collections.reverse(outputList);
    return TextUtils.join(",", outputList);
}
 
Example 13
Project: parabuild-ci   File: RefCapablePropertyResourceBundle.java   View Source Code Vote up 5 votes
private RefCapablePropertyResourceBundle(String baseName,
        PropertyResourceBundle wrappedBundle, ClassLoader loader) {
    this.baseName = baseName;
    this.wrappedBundle = wrappedBundle;
    Locale locale = wrappedBundle.getLocale();
    this.loader = loader;
    language = locale.getLanguage();
    country = locale.getCountry();
    variant = locale.getVariant();
    if (language.length() < 1) language = null;
    if (country.length() < 1) country = null;
    if (variant.length() < 1) variant = null;
}
 
Example 14
Project: openjdk-jdk10   File: ExecutableInputMethodManager.java   View Source Code Vote up 5 votes
private String createLocalePath(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    String localePath = null;
    if (!variant.equals("")) {
        localePath = "_" + language + "/_" + country + "/_" + variant;
    } else if (!country.equals("")) {
        localePath = "_" + language + "/_" + country;
    } else {
        localePath = "_" + language;
    }

    return localePath;
}
 
Example 15
Project: openjdk-jdk10   File: Bug8154797.java   View Source Code Vote up 5 votes
static void compareResources(Locale loc) {
    String mapKeyHourFormat = HMT, mapKeyGmtFormat = GMT;
    ResourceBundle compatBundle, cldrBundle;
    compatBundle = LocaleProviderAdapter.forJRE().getLocaleResources(loc)
            .getJavaTimeFormatData();
    cldrBundle = LocaleProviderAdapter.forType(Type.CLDR)
            .getLocaleResources(loc).getJavaTimeFormatData();
    if (loc.getCountry() == "FR" || loc.getCountry() == "FI") {
        mapKeyHourFormat = loc.getCountry() + HMT;
        mapKeyGmtFormat = loc.getCountry() + GMT;
    }

    if (!(expectedResourcesMap.get(mapKeyGmtFormat)
            .equals(compatBundle.getString(GMT_RESOURCE_KEY))
            && expectedResourcesMap.get(mapKeyHourFormat)
            .equals(compatBundle.getString(HMT_RESOURCE_KEY))
            && expectedResourcesMap.get(mapKeyGmtFormat)
            .equals(cldrBundle.getString(GMT_RESOURCE_KEY))
            && expectedResourcesMap.get(mapKeyHourFormat)
            .equals(cldrBundle.getString(HMT_RESOURCE_KEY)))) {

        throw new RuntimeException("Retrieved resource does not match with "
                + "  expected string for Locale " + compatBundle.getLocale());

    }

}
 
Example 16
Project: myfaces-trinidad   File: MessageBundleTest.java   View Source Code Vote up 5 votes
private String _getBundleName(Locale locale)
{
  int index = _bundleName.lastIndexOf(".");
  String bundleName = _bundleName.substring(index + 1);

  if (locale.getLanguage().length() == 0)
    return bundleName;

  bundleName +=   "_" +  locale.getLanguage();

  if (locale.getCountry().length() != 0)
    bundleName = bundleName  + "_" + locale.getCountry();
  return bundleName;
}
 
Example 17
Project: EveryCoolPic   File: Utils.java   View Source Code Vote up 4 votes
public static String getLocaleString() {
    Locale locale = Locale.getDefault();
    return locale.getLanguage() + "-" + locale.getCountry();
}
 
Example 18
Project: LightSIP   File: AcceptLanguage.java   View Source Code Vote up 3 votes
/**
 * Sets the language parameter of this AcceptLanguageHeader.
 *
 *
 *
 * @param language - the new Locale value of the language of
 *
 * AcceptLanguageHeader
 *
 *
 */
public void setAcceptLanguage(Locale language) {
    // JvB: need to take sub-tag into account
    if ( "".equals(language.getCountry())) {
        this.languageRange = language.getLanguage();
    } else {
        this.languageRange = language.getLanguage() + '-' + language.getCountry();
    }
}
 
Example 19
Project: XERUNG   File: CountryMaster.java   View Source Code Vote up 2 votes
/**
 * Returns the device's locale iso code
 * 
 * @return String
 */
public String getDefaultCountryIso() {
	Locale locale = Locale.getDefault();
	return locale.getCountry();
}
 
Example 20
Project: util   File: StringUtils.java   View Source Code Vote up 2 votes
/**
 * Determine the RFC 3066 compliant language tag, as used for the HTTP
 * "Accept-Language" header.
 * 
 * @param locale
 *            the Locale to transform to a language tag
 * @return the RFC 3066 compliant language tag as {@code String}
 */
public static String toLanguageTag(Locale locale) {
	return locale.getLanguage() + (hasText(locale.getCountry()) ? "-" + locale.getCountry() : "");
}