Java ResourceBundle.Control 的 getCandidateLocales() 方法



描述

Java ResourceBundle.Control 的 getCandidateLocales(String baseName, Locale locale) 方法返回一個 Locale 列表,作為 baseName 和 locale 的候選區域設定。每次工廠方法嘗試為目標區域設定查詢資源包時,ResourceBundle.getBundle 工廠方法都會呼叫此方法。

宣告

以下是java.util.Control.getCandidateLocales() 方法的宣告

public List<Locale< getCandidateLocales(String baseName, Locale locale)

引數

  • baseName − 資源包的基本名稱,一個完全限定的類名

  • locale − 所需資源包的區域設定

返回值

此方法返回給定區域設定的候選 Locale 列表

異常

NullPointerException − 如果 baseName 或 locale 為空

從美國區域設定的 ResourceBundle Control 獲取 Locale 列表示例

以下示例演示瞭如何使用 Java ResourceBundle.Control 的 getCandidateLocales() 方法來獲取美國區域設定的區域設定列表。我們建立了一個具有 FORMAT_DEFAULT 的資源包控制。然後,使用 getCandidateLocales() 方法列印對應於 hello_en_US.properties 檔案的美國區域設定的候選區域設定。

package com.tutorialspoint;

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

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      // print the CandidateLocales
      System.out.println(rbc.getCandidateLocales("hello", Locale.US));
   }
}

輸出

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

hello = Hello World!

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

[en_US, en, ]

從法國區域設定的 ResourceBundle Control 獲取 Locale 列表示例

以下示例演示瞭如何使用 Java ResourceBundle.Control 的 getCandidateLocales() 方法來獲取法國區域設定的區域設定列表。我們建立了一個具有 FORMAT_DEFAULT 的資源包控制。然後,使用 getCandidateLocales() 方法列印對應於 hello_fr_FR.properties 檔案的法國區域設定的候選區域設定。

package com.tutorialspoint;

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

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      // print the CandidateLocales
      System.out.println(rbc.getCandidateLocales("hello", Locale.FRANCE));
   }
}

輸出

假設在你的 CLASSPATH 中有一個名為hello_fr_FR.properties 的資原始檔,其內容如下。

hello = Bonjour le monde!

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

[fr_FR, fr, ]

從德國區域設定的 ResourceBundle Control 獲取 Locale 列表示例

以下示例演示瞭如何使用 Java ResourceBundle.Control 的 getCandidateLocales() 方法來獲取德國區域設定的區域設定列表。我們建立了一個具有 FORMAT_DEFAULT 的資源包控制。然後,使用 getCandidateLocales() 方法列印對應於 hello_de_DE.properties 檔案的德國區域設定的候選區域設定。

package com.tutorialspoint;

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

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      // print the CandidateLocales
      System.out.println(rbc.getCandidateLocales("hello", Locale.GERMANY));
   }
}

輸出

假設在你的 CLASSPATH 中有一個名為hello_de_DE.properties 的資原始檔,其內容如下。

hello = Hallo Welt!

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

[de_DE, de, ]
java_util_resourcebundle_control.htm
廣告
© . All rights reserved.