當前位置: 首頁>>代碼示例>>Java>>正文


Java Locale.toLanguageTag方法代碼示例

本文整理匯總了Java中java.util.Locale.toLanguageTag方法的典型用法代碼示例。如果您正苦於以下問題:Java Locale.toLanguageTag方法的具體用法?Java Locale.toLanguageTag怎麽用?Java Locale.toLanguageTag使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.Locale的用法示例。


在下文中一共展示了Locale.toLanguageTag方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testCurrentLocales

import java.util.Locale; //導入方法依賴的package包/類
/**
 * Ensure that all current locale ids parse.  Use DateFormat as a proxy
 * for all current locale ids.
 */
public void testCurrentLocales() {
    Locale[] locales = java.text.DateFormat.getAvailableLocales();
    Builder builder = new Builder();

    for (Locale target : locales) {
        String tag = target.toLanguageTag();

        // the tag recreates the original locale,
        // except no_NO_NY
        Locale tagResult = Locale.forLanguageTag(tag);
        if (!target.getVariant().equals("NY")) {
            assertEquals("tagResult", target, tagResult);
        }

        // the builder also recreates the original locale,
        // except ja_JP_JP, th_TH_TH and no_NO_NY
        Locale builderResult = builder.setLocale(target).build();
        if (target.getVariant().length() != 2) {
            assertEquals("builderResult", target, builderResult);
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:27,代碼來源:LocaleEnhanceTest.java

示例2: getParentLocale

import java.util.Locale; //導入方法依賴的package包/類
private static Locale getParentLocale(Locale locale) {
    Locale parent = parentLocalesMap.get(locale);

    if (parent == null) {
        String tag = locale.toLanguageTag();
        for (Map.Entry<Locale, String[]> entry : baseMetaInfo.parentLocales().entrySet()) {
            if (Arrays.binarySearch(entry.getValue(), tag) >= 0) {
                parent = entry.getKey();
                break;
            }
        }
        if (parent == null) {
            parent = locale; // non existent marker
        }
        parentLocalesMap.putIfAbsent(locale, parent);
    }

    if (locale.equals(parent)) {
        // means no irregular parent.
        parent = null;
    }

    return parent;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:CLDRLocaleProviderAdapter.java

示例3: checkLookup

import java.util.Locale; //導入方法依賴的package包/類
private static void checkLookup(String ranges, String tags,
        String expectedLocale) {

    List<Locale.LanguageRange> priorityList = Locale.LanguageRange
            .parse(ranges);
    List<Locale> localeList = generateLocales(tags);
    Locale loc = Locale.lookup(priorityList, localeList);
    String actualLocale
            = loc.toLanguageTag();

    if (!actualLocale.equals(expectedLocale)) {
        System.err.println("Locale.lookup failed with ranges: " + ranges
                + " Expected: " + expectedLocale
                + " Actual: " + actualLocale);
        err = true;
    }

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:Bug8035133.java

示例4: main

import java.util.Locale; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    int errors = 0;

    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources", locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:Main.java

示例5: main

import java.util.Locale; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    int errors = 0;
    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            System.out.println("ERROR: " + value + " expected: " + tag);
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:Main.java

示例6: main

import java.util.Locale; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    int errors = 0;

    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:Main.java

示例7: setLanguage

import java.util.Locale; //導入方法依賴的package包/類
@Override
public void setLanguage(Locale language) {
	synchronized (this){
		settingsData.languageTag = language.toLanguageTag();
		save();
	}
}
 
開發者ID:Gurgy,項目名稱:Cypher,代碼行數:8,代碼來源:TOMLSettings.java

示例8: getCandidateLocales

import java.util.Locale; //導入方法依賴的package包/類
@Override
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
    String key = baseName + '-' + locale.toLanguageTag();
    List<Locale> candidates = CANDIDATES_MAP.get(key);
    if (candidates == null) {
        LocaleProviderAdapter.Type type = baseName.contains(DOTCLDR) ? CLDR : JRE;
        LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
        candidates = adapter instanceof ResourceBundleBasedAdapter ?
            ((ResourceBundleBasedAdapter)adapter).getCandidateLocales(baseName, locale) :
            defaultControl.getCandidateLocales(baseName, locale);

        // Weed out Locales which are known to have no resource bundles
        int lastDot = baseName.lastIndexOf('.');
        String category = (lastDot >= 0) ? baseName.substring(lastDot + 1) : baseName;
        Set<String> langtags = ((JRELocaleProviderAdapter)adapter).getLanguageTagSet(category);
        if (!langtags.isEmpty()) {
            for (Iterator<Locale> itr = candidates.iterator(); itr.hasNext();) {
                if (!adapter.isSupportedProviderLocale(itr.next(), langtags)) {
                    itr.remove();
                }
            }
        }
        // Force fallback to Locale.ENGLISH for CLDR time zone names support
        if (locale.getLanguage() != "en"
                && type == CLDR && category.equals("TimeZoneNames")) {
            candidates.add(candidates.size() - 1, Locale.ENGLISH);
        }
        CANDIDATES_MAP.putIfAbsent(key, candidates);
    }
    return candidates;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:32,代碼來源:LocaleData.java

示例9: localeToTags

import java.util.Locale; //導入方法依賴的package包/類
private static Stream<String> localeToTags(Locale loc) {
    Objects.requireNonNull(loc);

    String tag = loc.toLanguageTag();
    List<String> tags = null;

    switch (loc.getLanguage()) {
        // ISO3166 compatibility
        case "iw":
            tags = List.of(tag, tag.replaceFirst("^he", "iw"));
            break;
        case "in":
            tags = List.of(tag, tag.replaceFirst("^id", "in"));
            break;
        case "ji":
            tags = List.of(tag, tag.replaceFirst("^yi", "ji"));
            break;

        // Special COMPAT provider locales
        case "ja":
            if (loc.getCountry() == "JP") {
                tags = List.of(tag, jaJPJPTag);
            }
            break;
        case "no":
        case "nn":
            if (loc.getCountry() == "NO") {
                tags = List.of(tag, noNONYTag);
            }
            break;
        case "th":
            if (loc.getCountry() == "TH") {
                tags = List.of(tag, thTHTHTag);
            }
            break;
    }

    return tags == null ? List.of(tag).stream() : tags.stream();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:40,代碼來源:IncludeLocalesPlugin.java

示例10: testLookup

import java.util.Locale; //導入方法依賴的package包/類
private static void testLookup() {
    boolean error = false;
    String ranges = "hi-IN, itc-Ital";
    String tags = "hi-IN, itc-Ital";
    List<LanguageRange> priorityList = LanguageRange.parse(ranges);
    List<Locale> localeList = generateLocales(tags);
    Locale actualLocale
            = Locale.lookup(priorityList, localeList);
    String actualLocaleString = "";

    if (actualLocale != null) {
        actualLocaleString = actualLocale.toLanguageTag();
    } else {
        error = true;
    }

    String expectedLocale = "hi-IN";

    if (!expectedLocale.equals(actualLocaleString)) {
        error = true;
    }

    if (error) {
        System.err.println("testLookup() failed on language ranges:"
                + " [" + ranges + "] and language tags: [" + tags + "]");
        err = true;
    }

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:Bug8159420.java

示例11: getBundle

import java.util.Locale; //導入方法依賴的package包/類
@Override
public ResourceBundle getBundle(String baseName, Locale locale) {
    ResourceBundle rb = super.getBundle(baseName, locale);
    String tag = locale.toLanguageTag();
    if (tag.equals("und")) {
        tag = "ROOT"; // to a human friendly name
    }
    System.out.printf("    MyResourcesProvider.getBundle(%s, %s)%n         -> %s%n",
                      baseName, tag, rb);
    return rb;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:MyResourcesProvider.java

示例12: main

import java.util.Locale; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    int errors = 0;
    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println(rb.getBaseBundleName() + ": locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            System.out.println("ERROR: " + value + " expected: " + tag);
            errors++;
        }

        // inaccessible bundles
        try {
            ResourceBundle.getBundle("jdk.test.internal.resources.Foo", locale);
            System.out.println("ERROR: jdk.test.internal.resources.Foo should not be accessible");
            errors++;
        } catch (MissingResourceException e) {
            e.printStackTrace();

            Throwable cause = e.getCause();
            System.out.println("Expected: " +
                (cause != null ? cause.getMessage() : e.getMessage()));
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:35,代碼來源:Main.java

示例13: create

import java.util.Locale; //導入方法依賴的package包/類
@Override
public MessageFully create(MessageMapper mapper, Collection<Parameter> parameters) {
	Locale locale = this.getTranslator().getDefaultLocale();
	String language = locale.toLanguageTag();
	String translation = this.getTranslator().translate(mapper.getKey(), locale, parameters);
	return new MessageFully(mapper.getKey(), mapper.getSeverity(), language, translation, parameters);
}
 
開發者ID:rooting-company,項目名稱:roxana,代碼行數:8,代碼來源:MessageFullyCreator.java

示例14: testBug7002320

import java.util.Locale; //導入方法依賴的package包/類
public void testBug7002320() {
    // forLanguageTag() and Builder.setLanguageTag(String)
    // should add a location extension for following two cases.
    //
    // 1. language/country are "ja"/"JP" and the resolved variant (x-lvariant-*)
    //    is exactly "JP" and no BCP 47 extensions are available, then add
    //    a Unicode locale extension "ca-japanese".
    // 2. language/country are "th"/"TH" and the resolved variant is exactly
    //    "TH" and no BCP 47 extensions are available, then add a Unicode locale
    //    extension "nu-thai".
    //
    String[][] testdata = {
        {"ja-JP-x-lvariant-JP", "ja-JP-u-ca-japanese-x-lvariant-JP"},   // special case 1
        {"ja-JP-x-lvariant-JP-XXX"},
        {"ja-JP-u-ca-japanese-x-lvariant-JP"},
        {"ja-JP-u-ca-gregory-x-lvariant-JP"},
        {"ja-JP-u-cu-jpy-x-lvariant-JP"},
        {"ja-x-lvariant-JP"},
        {"th-TH-x-lvariant-TH", "th-TH-u-nu-thai-x-lvariant-TH"},   // special case 2
        {"th-TH-u-nu-thai-x-lvariant-TH"},
        {"en-US-x-lvariant-JP"},
    };

    Builder bldr = new Builder();

    for (String[] data : testdata) {
        String in = data[0];
        String expected = (data.length == 1) ? data[0] : data[1];

        // forLanguageTag
        Locale loc = Locale.forLanguageTag(in);
        String out = loc.toLanguageTag();
        assertEquals("Language tag roundtrip by forLanguageTag with input: " + in, expected, out);

        // setLanguageTag
        bldr.clear();
        bldr.setLanguageTag(in);
        loc = bldr.build();
        out = loc.toLanguageTag();
        assertEquals("Language tag roundtrip by Builder.setLanguageTag with input: " + in, expected, out);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:43,代碼來源:LocaleEnhanceTest.java


注:本文中的java.util.Locale.toLanguageTag方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。