Ví dụ sau thể hiện bản địa hóa nhiệt độ, bao gồm làm tròn tùy chỉnh và định dạng giá trị thập phân theo các thuộc tính cụ thể của miền địa phương.
public class LocalTemperature {
private final Locale locale;
private final String temperatureFormat;
private final float conversionFactor;
private final float conversionOffset;
public LocalTemperature(ResourceBundle bundle) {
locale = bundle.getLocale();
temperatureFormat = bundle.getString("temperature.decimal.format");
conversionFactor = Float.parseFloat(bundle.getString("temperature.conversion.factor"));
conversionOffset = Float.parseFloat(bundle.getString("temperature.conversion.offset"));
}
public String format(double kelvin) {
double localTemperature = conversionOffset + conversionFactor * kelvin;
DecimalFormat format = new DecimalFormat(temperatureFormat, DecimalFormatSymbols.getInstance(locale));
return format.format(localTemperature);
}
}
MyResources_DE.properties:
temperature.conversion.factor=1.0
temperature.conversion.offset=-273.15
temperature.decimal.format=###,###.##°C
MyResources_NR.properties:
temperature.conversion.factor=1.0
temperature.conversion.offset=-273.15
temperature.decimal.format=###,###.## °C
MyResources_en_US.properties:
temperature.conversion.factor=1.8
temperature.conversion.offset=-459.67
temperature.decimal.format=###,###.## °F
này có thể được xác nhận qua sự kiểm tra đơn vị sau đây:
@RunWith(Parameterized.class)
public class LocalTemperatureTest {
private final double testValue;
private final String expectedResult;
private final LocalTemperature testSubject;
public LocalTemperatureTest(Locale locale, double testValue, String expected) {
ResourceBundle bundle = ResourceBundle.getBundle("MyResources", locale);
this.testSubject = new LocalTemperature(bundle);
this.testValue = testValue;
this.expectedResult = expected;
}
@Test
public void test() {
TestCase.assertEquals("Conversion error", expectedResult, testSubject.format(testValue));
}
@Parameters(name="{index}: locale={0} kelvin={1} expected={2}")
public static Iterable<Object[]> getTestParameters() {
Locale norwegian = new Locale("nr");
Object[][] parameters = {
{Locale.GERMAN, 0, "-273,15°C"},
{Locale.GERMAN, 273.15, "0°C"},
{Locale.GERMAN, 287.15, "14°C"},
{Locale.GERMAN, 287.35, "14,2°C"},
{Locale.GERMAN, 287.38, "14,23°C"},
{Locale.GERMAN, 287.384, "14,23°C"},
{Locale.GERMAN, 287.385, "14,24°C"},
{norwegian, 287.15, "14 °C"},
{Locale.US, 300.0, "80.33 °F"}
};
return Arrays.asList(parameters);
}
}
Lưu ý rằng theo hợp đồng, tất cả các giá trị nhiệt độ được cung cấp phải có cùng thang đo cơ sở (ở đây là kelvin).
Có phải chỉ là không gian hoặc có thể có Vùng địa lý sẽ hiển thị ° F tương ứng? – Fildor
Sử dụng kết hợp 'ResourceBundle' và' MessageFormat' sẽ hoạt động. Cũng có một cái nhìn ở đây, có lẽ điều này có thể giúp bạn: http://openjavaweatherapi.sourceforge.net/javadoc/com/oopitis/weather/WeatherProperty.html – Berger
Một chuyển đổi đơn giản để liệt kê mọi người dùng Fahrenheit để chuyển đổi giá trị bạn muốn. Đối với phần còn lại, sử dụng Celsius, không có nhiều quốc gia vẫn sử dụng nó. Xem [post] này (https://www.quora.com/Which-countries-use-Fahrenheit-as-a-measurement-of-temperature) để xem danh sách – AxelH