'How do I know the measurement units corresponding to a given locale?
I'd like to display length in a locale-dependent way, i.e.: show the length in the correct measurement unit for the current locale. I know LC_MEASUREMENT is what I need to use, however how do I use it?
GNU gettext does not give me anything to work with LC_MEASUREMENT. I looked at the source of some weather applets and they all require you to manually enter your unit (Kelvin, Fahrenheit or Celsius) in the preferences window.
It seems to me that that environment variable is never used by anyone, however I'd really like to use it to give a better user experience. Perhaps, is there a free (as in freedom) database that maps locale names to the corresponding measurement units?
FYI: my application is written in C.
Solution 1:[1]
It is an old question, but there is another way. If not a definitive solution, at least a good workaround.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <langinfo.h>
int main(void)
{
/* for LC_MEASUREMENT */
const char *nonmetric="_MM _US _LR";
char *measured;
printf("NL_MEASUREMENT: %s\n", nl_langinfo(_NL_MEASUREMENT_MEASUREMENT));
measured=getenv("LC_MEASUREMENT");
printf("\nLC_MEASUREMENT: %s\n", measured);
has_=strchr(measured, '_');
if(has_!=NULL && strstr(nonmetric, has_) != NULL)
printf("US Customary Units\n");
else
printf("Metric System\n");
/* for LC_MONETARY, LC_NUMERIC, LC_MESSAGE, LC_CTYPE and others */
struct lconv *lc;
setlocale(LC_ALL, "pt_BR.UTF-8");
lc=localeconv();
printf("\nLC_MONETARY, Currency symbol: %s\n", lc->currency_symbol);
printf("\nLC_NUMERIC, Decimal point: %s\n", lc->decimal_point);
printf("\nLC_MESSAGE, YESEXPR: %s\n", nl_langinfo(YESEXPR));
printf("\nLC_CTYPE, CODESET: %s\n", nl_langinfo(CODESET));
return 0;
}
(*) The second block of code is there just for completeness.
But the first part of the example gives the information available about the metric units. It is not much, but it is currently what is at your disposal.
It just identifies the country code. But you can test it against these 3 countries (Myanmar/Burma, the United States, and Liberia):
- *_MM (my_MM or en_MM)
- *_US (chr_US, HAW_US, en_US, es_US or lkt_US)
- *_LR (ff_LR, kpe_LR or en_LR)
and if it is different, you can assume the metric system is adopted (or at least partially adopted).
Call this program without environment variables to see your configuration:
$ ./lc_measurement.x
NL_MEASUREMENT: UTF-8
LC_MEASUREMENT: C.UTF-8
Metric System
LC_MONETARY, Currency symbol: R$
LC_NUMERIC, Decimal point: ,
LC_MESSAGE, YESEXPR: ^[+1SsyY]
LC_CTYPE, CODESET: UTF-8
$ LC_MEASUREMENT="kpe_LR" ./lc_measurement.x
NL_MEASUREMENT: UTF-8
LC_MEASUREMENT: kpe_LR
US Customary Units
LC_MONETARY, Currency symbol: R$
LC_NUMERIC, Decimal point: ,
LC_MESSAGE, YESEXPR: ^[+1SsyY]
LC_CTYPE, CODESET: UTF-8
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
