📌  相关文章
📜  Java中的 Locale getDisplayLanguage(Locale) 方法及示例

📅  最后修改于: 2022-05-13 01:55:46.551000             🧑  作者: Mango

Java中的 Locale getDisplayLanguage(Locale) 方法及示例

Java中Locale 类getDisplayLanguage(Locale inLoc)方法用于获取指定语言环境的语言名称。如果有可能,返回的名称将根据 inLocale 进行本地化。

句法:

public String getDisplayLanguage(Locale inLoc)

参数:该方法接受一个Locale类型的参数inLoc ,表示名称的本地化显示。

返回值:此方法返回适合给定语言环境的显示语言的名称。

异常:如果参数 inLoc 为 null,则该方法抛出NullPointerException

下面的程序说明了 getDisplayLanguage() 方法的工作:
方案一:

// Java code to illustrate getDisplayLanguage() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("en", "US");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Displaying the language of this locale
        System.out.println("Language: "
                           + first_locale
                                 .getDisplayLanguage(
                                     new Locale("fr", "FR")));
    }
}
输出:
First Locale: en_US
Language: anglais

方案二:

// Java code to illustrate getDisplayLanguage() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("en", "US");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Displaying the language of this locale
        System.out.println("Language: "
                           + first_locale
                                 .getDisplayLanguage(
                                     new Locale("", "FR")));
    }
}
输出:
First Locale: en_US
Language: English