首頁  >  文章  >  Java  >  本地爪哇

本地爪哇

WBOY
WBOY原創
2024-08-30 16:15:49524瀏覽

Java locale 類別是 java.util 套件中的最後一個類,有助於根據使用者的地理區域呈現資訊。需要顯示的資訊根據特定的地理、政治或文化區域而改變,並且需要執行定位任務的操作已知是區域設定敏感的。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

此類的物件使用以下五個變體作為其欄位:

  • 語言
  • 腳本
  • 變體
  • 擴充

這並不代表物件的容器;相反,它是一種辨識物件的機制。這裡 Unicode 區域設定包含用於覆蓋區域設定的預設行為的屬性和關鍵字。

文法:

public final class Locale
extends Object
implements Cloneable, Serializable

上面的語法表示 Locale 類別是一個最終類,這意味著沒有其他類別可以擴展該類別。

Locale 類別實作以下兩個介面的行為:

  • Cloneable:實作此介面允許該類別的物件使用object.clone()方法為其物件的欄位副本產生一個欄位。
  • Serialized:實作此介面可以讓需要透過網路傳輸的物件保留其狀態,以便可以使用 readObject 輕鬆恢復。

Java Locale 類別如何運作?

Java 語言環境類別使用 BCP 47 實作標識符,並支援用於語言環境資料交換的 LDML BCP 47 相容擴充。

Java 語言環境類別包含以下欄位:

  • 語言: 這是一個不區分大小寫的字段,包含[a-zA-Z]{2,8}、ISO 639 alpha-2 或alpha-3 語言代碼形式的值,或最多8 個字母的註冊語言子標籤。例:“es”用於西班牙語,“ja”用於日語。
  • 腳本: 它也是一個不區分大小寫的字段,包含格式為 –[a-zA-Z]{4} 的腳本,ISO 15924 alpha-4 腳本程式碼。例:拉丁文、西里爾。
  • 國家  (地區): 這是一個不區分大小寫的字段,用於表示形式為–[a-zA-Z]{ 的locale 物件的國家或地區2} | [0-9]{3}。例:「IN」(印度)、「FR」(法國)。
  • 變體: 這是一個區分大小寫的字段,用於表示具有自己語義的Locale 變體,使用以下形式的值– ( ('_'|'-') SUBTAG) * 其中子標籤= [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}。例:「polyton」(多音希臘文)、「POSIX」。
  • 擴充:它是一個映射,以鍵值對形式表示除了語言辨識之外的擴充。

類別有 3 個建構子:

  • 區域設定(字串語言)
  • 區域設定(字串語言,字串國家)
  • 區域設定(字串語言、字串國家、字串變體)

無法指定腳本或擴充。使用上述任何一個建構函數,都可以建立一個 locale 物件並指定其字段,並且此資訊用於根據使用者的國家或地區變更要顯示的資訊。

Java 語言環境方法

下面給的是 java 語言環境方法:

1. getDefault()

此方法根據呼叫 JVM 實例傳回語言環境的預設物件。

範例:如果在 France JVM 上執行此函數,則會傳回法語語言的 Locale 物件。

語法 1:

public static Locale getDefault ()

語法 2:

public static Locale getDefault (Locale.Category category)

2. setDefault(區域設定 locale1)

此方法用於將給定區域設定設為 JVM 實例的預設區域設定。

3.雜湊碼 ()

此方法是物件類別中 hashCode 方法的重寫方法,其中傳回該物件的 hashCode 值。

文法:

public int hashCode ()

4. getISOCountries ()

此方法傳回 ISO 639 中提到的 2 個字母的國家/地區代碼清單。

文法:

public static String[] getISOCountries ()

5. getISOLanguages ()

此方法傳回 ISO 639 中存在的 2 個字母的語言代碼。

文法:

public static String[] getISOLanguages ()

6.取得語言 ()

此方法用於顯示呼叫區域物件的語言程式碼。

文法:

public String getLanguage ()

7. getDisplayScript()

此方法用於取得給定語言環境的腳本的字串表示形式。此方法從 JDK 1.7 開始可用。

文法:

public String getDisplayScript ()

8. getDisplayCountry ()

This method is used to display the name of the country to which the locale object belongs to.

Syntax 1:

public final String getDisplayCountry ()

Syntax 2:

public String getDisplayCountry (Locale locale1)

9. getCountry ()

This method is used to get the ISO 3166 2-letter code for the country that the given locale belongs to.

Syntax:

public String getCountry ()

10. equals (Object locale2)

This is an inherited method from the Object class in this class that helps to check whether 2 locale objects are equal or not. It requires an object to a second locale object to be passed as an argument.

Syntax:

public Boolean equals (Object Locale2)

11. getDisplayVariant ()

This method is used to display the value stored in a given locale object’s variant field according to the given user.

Syntax 1:

public final String getDisplayVariant ()

Syntax 2:

public final String getDisplayVariant (Locale locale1)

12. getDisplayName ()

This method is used to display the given locale object’s name according to the user’s perspective.

Syntax 1:

public final String getDisplayName ()

Syntax 2:

public String getDisplayName (Locale locale1)

13. clone ()

This method is used to copy one object’s field-to-field value to other objects of the same class. This method is an inherited method from Cloneable Interface and helps to create a clone object of a given locale object.

14. getAvailableLocales ()

This method is used to get the array of all installed locales.

Syntax : 

public static Locale[] getAvailableLocales

15. getDisplayLanguage ()

This method returns the language associated with a given locale.

Syntax 1:

public final String getDisplayLanguage ()

Syntax 2:

public final String getDisplayLanguage (Locale locale1)

16. getISO3Country ()

This method is used to get a 3-letter abbreviation of locale country.

Syntax:

public String getISO3Country ()

17. getISO3Language ()

This method is used to get a 3-letter abbreviation of ones locale language object.

Syntax:

public String getISO3Language ()

18. toString ()

This method returns the String representation of the locale object.

Syntax:

public final String toString ()

19. forLanguageTag (StringlanguageTag)

This method is used to get the locale object for the given language tag string according to IETF BCP 47.

Syntax:

public static Locale forLanguageTag (String langTag)

Examples of Java Locale

Given below are the examples:

Example #1

Example to use Locale Class calling Locale constructor.

Code:

import java.util.Locale;
public class demo1 {
public static void main (String[] args)
{
Locale object1 = new Locale ("America", "US");
Locale myObject2 = Locale.getDefault ();
System.out.println ("Locale object1 name : " + object1);
System.out.println ("Locale myObject2 : " + myObject2);
Locale.setDefault (new Locale ("es", "ES", "WIN"));
System.out.println ("Value of Default Locale after running setDefault () "+Locale.getDefault ());
System.out.println ("String Representation of NAME of locale object myobj3 "  + myObject2.getDisplayName ());
System.out.println ("String Representation of language of locale object myobj3 "  + myObject2.getISO3Language ());
System.out.println ("ISO3 Country Name of locale object myobj3 " + myObject2.getISO3Country ());
System.out.println ("String Representation of locale object
myobj3 "  + myObject2.toString ());
}
}

Output:

本地爪哇

Example #2

Code:

import java.util.Locale;
public class demo1 {
public static void main (String[] args)
{
Locale myobj1= new Locale ("SPANISH", "ES", "WIN");
Locale myobj3= (Locale) myobj1.clone ();
System.out.println ("nName of the country : "          + myobj1.getDisplayCountry ());
System.out.println ("Name of the country of locale object myobj1 in ISO 3166 2-letter code : "               + myobj1.getCountry ());
System.out.println ("nName of the country of locale object myobj3: "    + myobj3.getDisplayCountry ());
System.out.println ("Name of the country with locale object myobj3 in ISO 3166 2-letter code : "  + myobj3.getCountry ());
System.out.println ("Display Language of locale object myobj1 "    + myobj1 .getDisplayLanguage ());
System.out.println ("ISO3 Country Name of locale object myobj3 " + myobj3.getISO3Country ());
System.out.println ("String Representation of locale object myobj3"  + myobj3.toString ());
System.out.println ("List of Countries ");
String[] listCountries = myobj3.getISOCountries ();
for (String s:listCountries){
System.out.println (s);
}
}
}

Output:

本地爪哇

Example #3

Code:

import java.util.Locale;
public class demo1 {
public static void main (String[] args)
{
Locale myobject1 = new Locale ("FRENCH", "FR",
"WIN");
Locale myobject2 = new Locale ("FRENCH", "FR", "WIN");        System.out.println ("Variant of object 1:" + myobject1.getDisplayVariant ());
System.out.println ("Whether the two locale objects are equal:" + myobject1.equals (myobject2));
Locale[] arrLocales = Locale.getAvailableLocales ();
System.out.println ("\nName of Locales are : ");
for (int i = 1; i<arrLocales.length/10; i++)
System.out.println (i + ":" + arrLocales[i]);
}
}

Output:

本地爪哇

Conclusion

Locale class is a part of the java.util package that helps to alter the information according to the specific geographical or political region. A locale-sensitive operation use this class to store information related to each variant of the locale object. Many methods are provided in this class to get the information related to the locale object.

以上是本地爪哇的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java預設方法下一篇:Java預設方法