Java Swing - Localize Temperature
Asked Answered
N

1

11

Is there a way to localize Temperature in Java? Like temperature format will be based on the locale?

Because for example, for Norwegian language temperature format should be 14 °C. There should be a space before degrees symbol. But other languages should be 14°C.

Nauplius answered 9/1, 2017 at 10:7 Comment(4)
Is it just the space or may there be Locales that shall display the corresponding °F ?Leveroni
Using a combination of ResourceBundle and MessageFormat should work . Also have a look here, maybe this could help you : openjavaweatherapi.sourceforge.net/javadoc/com/oopitis/weather/…Scour
A simple switch to list every Fahrenheit user to convert the value you want. For the rest, use Celsius, there is not a lot country still using it. See this post to see the listZsazsa
Seems like there will be a new framework in an upcoming Java release. See JSR 363Phylum
A
7

The following example demonstrates temperature localization, including customizable rounding and formatting of decimal values by locale specific properties.

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

This can be verified by the following unit test:

@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);
    }
}

Note that by contract, all provided temperature values should have the same base scale (here it's kelvin).

Ancestry answered 9/1, 2017 at 12:25 Comment(3)
I would use a TempLocal that will use the Locale and use it to convert the value instead. Since the list of F° users are limited, I switch could do it. This will be easier than writing one file per country. See this post to see how short the switch could beZsazsa
@Zsazsa The OP refers to a multilingual application, that probably already uses property files for each supported language. The displayed temperature string can differ in more aspects than just the local scale's conversion value.Ancestry
I see that more like the numeric format use by Java, not like a multilingual application, but I don't know.Zsazsa

© 2022 - 2024 — McMap. All rights reserved.