本文整理汇总了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);
}
}
}
示例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;
}
示例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;
}
}
示例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");
}
}
示例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");
}
}
示例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");
}
}
示例7: setLanguage
import java.util.Locale; //导入方法依赖的package包/类
@Override
public void setLanguage(Locale language) {
synchronized (this){
settingsData.languageTag = language.toLanguageTag();
save();
}
}
示例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;
}
示例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();
}
示例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;
}
}
示例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;
}
示例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");
}
}
示例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);
}
示例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);
}
}