Using locale settings to detect wheter to use imperial units

前端 未结 6 582
被撕碎了的回忆
被撕碎了的回忆 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:30

    A more or less complete way to do this is this way.

    Kotlin:

    private fun Locale.toUnitSystem() =
        when (country.toUpperCase()) {
            // https://en.wikipedia.org/wiki/United_States_customary_units
            // https://en.wikipedia.org/wiki/Imperial_units
            "US" -> UnitSystem.IMPERIAL_US
            // UK, Myanmar, Liberia, 
            "GB", "MM", "LR" -> UnitSystem.IMPERIAL
            else -> UnitSystem.METRIC
        }
    

    Note that there is a difference between UK and US imperial systems, see the wiki articles for more details.

提交回复
热议问题