Java Locale toLanguageTag() 方法



描述

Java Locale toLanguageTag() 方法返回一個格式良好的 IETF BCP 47 語言標籤,表示此區域設定。

宣告

以下是java.util.Locale.toLanguageTag() 方法的宣告

public String toLanguageTag()

引數

返回值

此方法返回表示區域設定的 BCP47 語言標籤。

異常

獲取美國區域設定的語言標籤示例

以下示例演示了 Java Locale toLanguageTag() 方法的用法。我們正在建立一個美國的區域設定,然後檢索並列印其語言標籤。

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = Locale.US;

      // print this locale
      System.out.println("Locale:" + locale);

      // print the language tag of this locale
      System.out.println("Language Tag:" + locale.toLanguageTag());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale:en_US
Language Tag:en-US

獲取加拿大區域設定的語言標籤示例

以下示例演示了 Java Locale toLanguageTag() 方法的用法。我們正在建立一個加拿大的區域設定,然後檢索並列印其語言標籤。

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = Locale.CANADA;

      // print this locale
      System.out.println("Locale:" + locale);

      // print the language tag of this locale
      System.out.println("Language Tag:" + locale.toLanguageTag());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale:en_CA
Language Tag:en-CA

獲取法國區域設定的語言標籤示例

以下示例演示了 Java Locale toLanguageTag() 方法的用法。我們正在建立一個法國的區域設定,然後檢索並列印其語言標籤。

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale = Locale.FRANCE;

      // print this locale
      System.out.println("Locale:" + locale);

      // print the language tag of this locale
      System.out.println("Language Tag:" + locale.toLanguageTag());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale:fr_FR
Language Tag:fr-FR
java_util_locale.htm
廣告
© . All rights reserved.