HOWTO localedef

From Gentoo Linux Wiki

Jump to: navigation, search


Index

Installation

Configuration

  • localedef

Tips

Administration

edit

Contents

[edit] Introduction

This short page explains how to use the "localedef" command to fine tune your locale settings. Beware that it does not explain everything about localedef, and be sure to read right to the end, including the "Issues" section, before trying any of this at home.

[edit] Motivation

I live in Britain and I use the Gnome desktop. When I look at a calendar I like the weeks to start on a Monday. Gnome has a neat feature that if you click on the Clock Applet in the Panel it shows the current month's calendar with all your events from Evolution. But it shows the week starting on Saturday! And there is no option in the preferences to make it start on a different day.

This how to explains how to fix this, and a bunch more about Gnome, locales and glibc on the way.

[edit] Setting the locale

[edit] Why does the Clock Applet have no setting for "First day of the Week"?

There did used to be a setting in the Clock Applet, but some releases ago it was removed, and the applet was modified to pick up the user's preference from the system-wide locale setting. Let us pass over the wisdom of this decision, it happened. Too bad. The unfortunate fact is that the default locale setting has the first day of the week as a Saturday, apparently due to a bug in the glibc supplied locale files (see more below).

The way to change the first day of the week in the Clock's calendar view is therefore to set the correct system locale. Too bad if you don't like the "correct" choice! The excellent (but short) Gentoo localization guide explains how to do this, but to save you going to that try the following instructions.

[edit] Brief guide to setting the locale

Essentially what you do is to specify then in "/etc/env.d/02locale" like this:

  LANG=en_GB.UTF-8
  LC_ALL=en_GB.UTF-8

and then run

  env-update && source /etc/profile

and then logout of your X session, restart X with Ctrl-Alt-Backspace, and log back into X again (Gnome in my case).

[edit] Why is the Clock Applet still not right?

After you do that you can check what locale you are running with the "locale" command. Assuming it says what you set it to above, then everything has worked fine. However, you will find that the Clock's calendar view now shows weeks starting on a Sunday. This is a marginal improvement on starting on a Saturday, and if you like them like that well good for you, and you can stop now. If, like me, you emphatically hate them like that, read on.



Not from the original author:

For changing only the first day of the week you may set your 02local file to something like:

 LANG=en_GB.UTF-8
 LC_TIME=en_US

It works without messing with glibc. Note that LC_ALL may overwrite the LC_TIME setting (haven't personally tried).

Note: it does. One thing you can do is run

locale

copy all the LC_* to your /etc/env.d/02locale, unset LC_ALL, i.e.

#LC_ALL="en_GB.UTF-8"

and follow the note not written by the original author. Beware however that this has the unpleasant effect of not having LC_ALL set, consequently several applications complaining and not functioning properly. By the way, here LC_TIME=en_US didn't help solve the "first day" issue.


[edit] glibc and locales

After some research it became clear that the poor old Gnome Clock Applet was not at fault any more, but that the default en_GB locale file supplied with glibc really does set the first day of the week to be a Sunday. Lets explore how it does that, then see how to change it.

[edit] LC_TIME settings

One of the sub-sections of your locale is for settings that affect "strftime" and other utilities that format dates and times. This subsection is called LC_TIME. If you do

  locale -k LC_TIME

on your system you will see what LC_TIME controls. For our purposes the following settings are the important ones.

  day           "Sunday; Monday; Tuesday; Wednesday; Thursday; Friday; Saturday"
  % below: ndays;1stday;1stweek
  week           7;19971130;4
  first_weekday  1


This gives the names of the seven days of the week, says (redundantly) that there are seven of them, and then that the first one listed corresponds to the name of the day of the week of 1997-11-30 (which was a Sunday). Finally and crucially, the first day of the week is defined to be the first entry in the list of day names given.

For the UK, this is wrong! In the UK weeks start on Monday, following the ISO calendar standards.

There are two ways to express what it should be: either rearrange the day names or set first_weekday to 2. Either of these will do the trick.

  day           "Monday; Tuesday; Wednesday; Thursday; Friday; Saturday; Sunday"
  % below: ndays;1stday;1stweek
  week           7;19971201;4
  first_weekday  1
  day           "Sunday; Monday; Tuesday; Wednesday; Thursday; Friday; Saturday"
  % below: ndays;1stday;1stweek
  week           7;19971130;4
  first_weekday  2

but the second one involves less editing so we'll use that. Now, how do you make this change? Read on.

note: the 'cal' command does not seem to take this into account (week start may be forced with -m or -s)

[edit] Using localedef

The tool you need is the rather nasty-to-use "localedef". Proceed as follows.

  cd ~ # go to your home dir or somewhere else you can write a temp file
  cp /usr/share/i18n/locales/en_GB en_GB_modified

then edit "en_GB_modified". The locale (5) man page explains the format of the file.

1. Update the identification section if you like

2. Update all the sections except LC_TIME to be like this:

 LC_CURRENCY
 copy "en_GB"
 END LC_CURRENCY

etc. This ensures that you don't lose anything in the other sections.

3. Add the following lines to the LC_TIME section

 week            7;19971130;5
 first_weekday   2
 first_workday   2

This assumes that you are going to leave the list of day names alone. The "5" on the end of the week line says that the first week of the year is the one containing the first occurence of the fifth day in the list (Thursday), this is the ISO rule. The other two lines set the first day of the week to the second day in the list (Monday) and incidentally the first working day to be Monday too.

4. Create a new directory for the output of locale def in your home directory.

  cd ~ && mkdir locale 

5. Run

  localedef -c -i en_GB_modified -f UTF-8 locale/en_GB.utf8@iso

6. (as root) move the resulting "en_GB.utf8@iso" directory from your own "locale" directory to "/usr/lib/locale"

  cp -r locale/en_GB.utf8\@iso /usr/lib/locale/

6. (still as root) Update /etc/env.d/02locale to point at the new "en_GB.UTF8@iso" locale that you have created.

  LANG=en_GB.utf8@iso
  LC_ALL=en_GB.utf8@iso

and then run

  env-update && source /etc/profile

and then logout of your X session, restart X with Ctrl-Alt-Backspace, and log back into X again (Gnome in my case).

If you are lucky then everything is now working, and your Gnome Clock Applet shows weeks starting on Monday! Hooray!

[edit] Issues

There are always a few issues. Currently there are a few unknowns and problems with all this.

  • The "week" and "first_weekday" keywords in LC_TIME are not documented in any standard Linux documentation; the only available documentation for them is the ISO 14652 standard.
  • It would be better if glibc could correct their en_GB defintions to put Monday as the first day of the week.
  • It would be even better if applications like the Clock Applet allowed users to choose their own preferred first day of the week. The assumption that the default for the locale will be what the user wants is a bad assumption.
  • X11 has its own locale system that is almost-but-not-entirely compatible with glibc locales. If you follow the instructions above (and like me are using Gnome) then you will get warnings like these when you start certain X11 applications.
  Gdk-WARNING **: locale not supported by Xlib
  Gdk-WARNING **: cannot set locale modifiers

These are irritating and potentially harmful.

To avoid this effect you can create your modified locale with standard name. You can do this with:

  localedef -c -i en_GB_modified -f UTF-8 locale/en_GB.utf8

or (if you don't want Unicode)

  localedef -c -i en_GB_modified -f ISO-8859-1 locale/en_GB

(Thanks for this tip to COoLoSER 14:60, 8 March 2006 (GMT))

  • Your modified /usr/lib/locale/ directory will get replaced when you upgrade anything to do with libc or gnome, so you will need to do all this again.
  • localedef is an obscure and poorly documented tool. For example "man localedef" states rather hopelessly that "The format of the created output is unspecified." It also says "If the name operand does not contain a slash, then the existence of an output file for the locale is unspecified". So if you do
   localedef -c -i en_GB_modified -f UTF-8 en_GB.utf8@iso

(notice that we did not specify a local directory to write to on the command line) instead of creating the output file locally it will mysteriously fail. If you do this as root, it will write your output in /usr/lib/locale/locale-archive. You can still use the locale you created, but you may puzzle over where the system put the output.

Thruston 18:50, 8 February 2006 (GMT)

Personal tools