My original proposal above, from 2002-05-27, namely to decide on preferred units of measurement and standard paper size by searching for country codes in the LC_* environment variables (exactly as suggested in the above piece of code) is IMHO still the most advisable solution today! Please just try it, before thinking up alternatives.
The alternatives simply don't wash: The /etc/papersize mechanism is a complete non-starter, as it is a global setting that you cannot easily override for individual users or processes. The nl_langinfo(PAPER_HEIGHT) / nl_langinfo(PAPER_WIDTH) was a draft proposal (ISO technical report 14652) that only glibc implemented but that never made it into the POSIX standard or any other ISO standard. It won't work on other platforms and just leads to ugly platform-specific #ifdef massacres.
The above code will do the right thing by default on all POSIX platforms (not just Linux) and provides easy and obvious ways for users to override the defaults. It is the most pragmatic and workable solution you are going to find, given the rather messy standardization in this area.
What it does is very simple: The defaults are millimetres and A4 (because that's what ~95% of the world population use), and only if your environment suggests that you are in Canada or the U.S. will the default paper size become U.S.-Letter, and only if you are in the U.S. will the default unit of measurements become inches. This will be determined by the first of the variables LC_ALL, LC_{PAPER,MEASUREMENT}, LANG that is defined and non-empty, exactly the same priority order that setlocale() applies, but implemented in a strictly portable way without requiring the setlocale() implementation to even know of LC_{PAPER,MEASUREMENT}.
The inclusion of LC_{PAPER,MEASUREMENT} makes sure that users can override their countries' default for paper and measurement without affecting anything else. On the day when the U.S. finally goes metric, simply set LC_MEASUREMENT=POSIX and its done.
My original proposal above, from 2002-05-27, namely to decide on preferred units of measurement and standard paper size by searching for country codes in the LC_* environment variables (exactly as suggested in the above piece of code) is IMHO still the most advisable solution today! Please just try it, before thinking up alternatives.
The alternatives simply don't wash: The /etc/papersize mechanism is a complete non-starter, as it is a global setting that you cannot easily override for individual users or processes. The nl_langinfo( PAPER_HEIGHT) / nl_langinfo( PAPER_WIDTH) was a draft proposal (ISO technical report 14652) that only glibc implemented but that never made it into the POSIX standard or any other ISO standard. It won't work on other platforms and just leads to ugly platform-specific #ifdef massacres.
The above code will do the right thing by default on all POSIX platforms (not just Linux) and provides easy and obvious ways for users to override the defaults. It is the most pragmatic and workable solution you are going to find, given the rather messy standardization in this area.
What it does is very simple: The defaults are millimetres and A4 (because that's what ~95% of the world population use), and only if your environment suggests that you are in Canada or the U.S. will the default paper size become U.S.-Letter, and only if you are in the U.S. will the default unit of measurements become inches. This will be determined by the first of the variables LC_ALL, LC_{PAPER, MEASUREMENT} , LANG that is defined and non-empty, exactly the same priority order that setlocale() applies, but implemented in a strictly portable way without requiring the setlocale() implementation to even know of LC_{PAPER, MEASUREMENT} .
The inclusion of LC_{PAPER, MEASUREMENT} makes sure that users can override their countries' default for paper and measurement without affecting anything else. On the day when the U.S. finally goes metric, simply set LC_MEASUREMENT= POSIX and its done.