Using locale settings to detect wheter to use imperial units

前端 未结 6 567
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 09:00

I\'m working on an app that wants to display lengths either in centimeters (cm) or in inches(\"). Is there a way to select the right unit from the locale? In any event I\'m also

6条回答
  •  醉话见心
    2021-01-31 09:35

    1. Programmatically

    Making a small improvement in the solution from @vidstige

    I would use getCountry().toUpperCase() to be safe and change the checks to a switch for a cleaner code. Something like this:

    public static UnitLocale getFrom(Locale locale) {
        String countryCode = locale.getCountry().toUpperCase();
        switch (countryCode) {
            case "US":
            case "LR":
            case "MM":
                return Imperial;
            default:
                return Metric;
        }
    }
    
    1. From Resources

    Another solution could be creating resources folders for each country like: [values_US] [values_LR] [values_MM] with a boolean resource changed to true. Then read that boolean resource from the code.

提交回复
热议问题