Java ResourceBundle getLocale() 方法



描述

java ResourceBundle getLocale() 方法返回此資源捆綁的區域設定。此方法可在呼叫 getBundle() 後使用,以確定返回的資源捆綁是否真正對應於請求的區域設定,或者是否是回退。

宣告

以下是java.util.ResourceBundle.getLocale() 方法的宣告

public Locale getLocale()

引數

返回值

此方法返回此資源捆綁的區域設定。

異常

從美國區域設定的資源捆綁中獲取區域設定

以下示例演示了 Java ResourceBundle getLocale() 方法的使用方法來獲取區域設定。我們為相應的 hello_en_US.properties 檔案建立了一個美國區域設定的資源捆綁。然後我們列印分配給鍵 hello 的文字。然後使用 getLocale() 方法檢索區域設定並列印區域設定。

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;

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

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

      // print the text assigned to key "hello"
      System.out.println(bundle.getString("hello"));

      // get the locale
      System.out.println(bundle.getLocale());
   }
}

輸出

假設我們在 CLASSPATH 中有一個可用的資原始檔hello_en_US.properties,內容如下。此檔案將用作我們示例程式的輸入:

hello = Hello World!

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

Hello World!
en_US

從法國區域設定的資源捆綁中獲取區域設定

以下示例演示了 Java ResourceBundle getLocale() 方法的使用方法來獲取區域設定。我們為相應的 hello_fr_FR.properties 檔案建立了一個法國區域設定的資源捆綁。然後我們列印分配給鍵 hello 的文字。然後使用 getLocale() 方法檢索區域設定並列印區域設定。

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;

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

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.FRANCE);

      // print the text assigned to key "hello"
      System.out.println(bundle.getString("hello"));

      // get the locale
      System.out.println(bundle.getLocale());
   }
}

輸出

假設我們在 CLASSPATH 中有一個可用的資原始檔hello_fr_FR.properties,內容如下。此檔案將用作我們示例程式的輸入:

hello = Bonjour le monde!

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

Bonjour le monde!
fr_FR

從德國區域設定的資源捆綁中獲取區域設定

以下示例演示了 Java ResourceBundle getLocale() 方法的使用方法來獲取區域設定。我們為相應的 hello_de_DE.properties 檔案建立了一個德國區域設定的資源捆綁。然後我們列印分配給鍵 hello 的文字。然後使用 getLocale() 方法檢索區域設定並列印區域設定。

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;

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

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.GERMANY);

      // print the text assigned to key "hello"
      System.out.println(bundle.getString("hello"));

      // get the locale
      System.out.println(bundle.getLocale());
   }
}

輸出

假設我們在 CLASSPATH 中有一個可用的資原始檔hello_de_DE.properties,內容如下。此檔案將用作我們示例程式的輸入:

hello = Hallo Welt!

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

Hallo Welt!
de_DE
java_util_resourcebundle.htm
廣告
© . All rights reserved.