# Localisation and internationalisation

> Locales, LANG and LC_* variables, time zones and TZ, and character encodings from ASCII to UTF-8.

Source: https://aminbytes.dev/bytes/administrative-tasks/localisation/


A server in Frankfurt, an admin in Toronto, a customer file from 1998 with accented names: computers have to agree
on how to show dates, numbers and letters for people all over the world. Linux splits that into two ideas.
**Internationalisation** (shortened to i18n: 18 letters between the i and the n) is making software able to adapt.
**Localisation** (l10n) is the actual adapting: German month names, a comma as the decimal sign, the Berlin time zone.

As an admin you set three things: the **locale** (language and formats), the **time zone**, and the **character
encoding** files are stored in.

## Character encodings: ASCII to UTF-8

A file is just bytes. An **encoding** is the table that maps bytes to characters. You'll meet four names:

| Name | Size | What it covers |
| --- | --- | --- |
| **ASCII** | 7 bits, 128 characters | English letters, digits, punctuation, control codes. Nothing else |
| **ISO-8859** | 8 bits, 256 characters | A family of 15 one-byte tables: ASCII plus 128 extra characters for one region. `ISO-8859-1` (Latin-1) is Western Europe, `ISO-8859-15` adds the € sign |
| **Unicode** | A list, not an encoding | One number (code point) for every character in every script: `é` is `U+00E9`, `€` is `U+20AC` |
| **UTF-8** | 1 to 4 bytes per character | The usual way to store Unicode. The first 128 characters are identical to ASCII |

UTF-8 is the default on every current Linux system. Look at how the same letter becomes different bytes:

```console
$ printf 'A' | od -An -tx1
 41
$ printf 'é' | od -An -tx1
 c3 a9
$ printf '€' | od -An -tx1
 e2 82 ac
```

`A` is one byte, the same as in ASCII. `é` takes two bytes in UTF-8, while in ISO-8859-1 it was the single byte
`e9`. When a program reads UTF-8 bytes as Latin-1, `c3 a9` shows up as two characters: `Ã©`. That's the
classic broken-accent look, called *mojibake*.

### Converting with iconv

`iconv` converts text from one encoding to another. `-f` is "from", `-t` is "to":

```console
$ printf 'caf\xe9\n' > latin1.txt        # é as one ISO-8859-1 byte
$ file latin1.txt
latin1.txt: ISO-8859 text
$ iconv -f ISO-8859-1 -t UTF-8 latin1.txt -o utf8.txt
$ file utf8.txt
utf8.txt: Unicode text, UTF-8 text
$ cat utf8.txt
café
```

Without `-o`, `iconv` writes to standard output, so `iconv -f ISO-8859-1 -t UTF-8 old.txt > new.txt` works too.
More things to know:

```bash
iconv -l                                       # list every encoding iconv knows
echo 'Crème brûlée' | iconv -f UTF-8 -t ASCII//TRANSLIT   # Creme brulee
iconv -f UTF-8 -t ASCII//IGNORE in.txt         # drop what doesn't fit
```

Without `//TRANSLIT` or `//IGNORE`, `iconv` stops with an error at the first character the target can't hold.

 (Side note: `//TRANSLIT` uses the rules of your current locale. Under the plain `C` locale you may get `Cr?me br?l?e` instead, so run it with a UTF-8 locale like `en_US.UTF-8`.)

## Locales

A **locale** is a named bundle of rules: language for messages, date and time format, number format, currency,
sort order, which characters are letters. Its name looks like this:

```text
language_TERRITORY.codeset@modifier
de_DE.UTF-8         German as used in Germany, UTF-8
en_GB.UTF-8         English as used in the UK
nl_BE.UTF-8         Dutch as used in Belgium
sr_RS.UTF-8@latin   Serbian, written in Latin letters
```

Two special locales exist everywhere: `C` (also called `POSIX`), the plain, English, ASCII behaviour of classic
Unix, and `C.UTF-8`, the same rules but with UTF-8 characters.

### The variables

The locale is picked through environment variables, one per category:

| Variable | Controls | Example difference |
| --- | --- | --- |
| `LC_CTYPE` | Character classes and encoding | Is `é` a letter? How many bytes per character? |
| `LC_NUMERIC` | Number format | `3.14` or `3,14`, `1,000` or `1.000` |
| `LC_TIME` | Date and time format | `Tue Dec  1` or `Di 1. Dez` |
| `LC_COLLATE` | Sort order | `A B a b` or `a A b B` |
| `LC_MONETARY` | Currency | `$`, `€`, where the sign goes |
| `LC_MESSAGES` | Language of program messages | `No such file or directory` or `Datei oder Verzeichnis nicht gefunden` |
| `LC_PAPER` | Paper size | Letter or A4 |
| `LC_MEASUREMENT` | Units | Imperial or metric |
| `LC_NAME`, `LC_ADDRESS`, `LC_TELEPHONE` | Formats for names, addresses, phone numbers | |
| `LC_IDENTIFICATION` | Information about the locale itself | |

On top of those, two special variables:

- `LANG` is the **default** for every category that isn't set on its own.
- `LC_ALL` **overrides** every category, whatever else is set.

So the order of precedence is: **`LC_ALL`, then `LC_xxx`, then `LANG`**. If none is set, you get `C`.

 (Side note: GNU programs also read `LANGUAGE`, a fallback list for message translations only: `LANGUAGE=nl:de:en` means Dutch, else German, else English.)

### Looking at it: the locale command

`/usr/bin/locale` shows what's in effect:

```console
$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
```

Values in quotes are derived (here from `LANG`). Here `LC_TIME` was set on its own, so it differs. Other useful forms:

```console
$ locale -a              # locales installed on this machine
C
C.utf8
en_US.utf8
POSIX
$ locale charmap         # the encoding in use
UTF-8
```

### Seeing the effect

Set a variable just for one command to try it out:

```console
$ printf 'b\nA\na\nB\n' | LC_ALL=C sort
A
B
a
b
$ printf 'b\nA\na\nB\n' | LC_ALL=en_US.UTF-8 sort
a
A
b
B
$ LC_ALL=de_DE.UTF-8 date
Di 1. Dez 14:30:00 CET 2026
$ LC_ALL=de_DE.UTF-8 printf "%'d\n" 1234567
1.234.567
```

The `C` locale sorts by byte value, so all capitals come first. That's why scripts often start with
`export LC_ALL=C`: the output of `sort`, `ls` and `grep` ranges like `[a-z]` stays the same on every machine.

If you ask for a locale that isn't installed, you get a warning and the `C` rules:

```console
$ export LC_ALL=fr_FR.UTF-8
bash: warning: setlocale: LC_ALL: cannot change locale (fr_FR.UTF-8)
$ date
Tue Dec  1 14:30:00 CET 2026
$ unset LC_ALL
```

### Setting the locale for the system and for a user

Minimal containers ship only `C`, `C.UTF-8` and `POSIX`. Generating or installing more is distribution specific:

```bash
# Debian / Ubuntu
sudo apt install locales                     # usually already there on a full install
sudo dpkg-reconfigure locales                # menu: pick locales and the default
# or by hand: uncomment "de_DE.UTF-8 UTF-8" in /etc/locale.gen, then
sudo locale-gen
sudo update-locale LANG=de_DE.UTF-8          # writes /etc/default/locale

# RHEL / Fedora
sudo dnf install glibc-langpack-de           # one package per language
```

On any systemd machine, `localectl` reads and sets the system default:

```console
$ localectl status
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us
$ sudo localectl set-locale LANG=de_DE.UTF-8
```

The system default is stored in `/etc/locale.conf` (RHEL, Fedora, Arch) or `/etc/default/locale` (Debian,
Ubuntu). It's read at login. A single user can override it in `~/.bashrc` or `~/.profile`:

```bash {title="~/.profile"}
export LANG=nl_NL.UTF-8
export LC_MESSAGES=en_US.UTF-8     # Dutch formats, English error messages
```

> [!TIP]
> Keep `LC_ALL` out of profiles. It's meant for a quick override of one command or script. Set `LANG` plus the
> few `LC_*` you want to change instead.

## Time zones

The kernel's clock counts in UTC. Every program converts that to local time when it shows it, using the
**time zone database**: one small binary file per region in `/usr/share/zoneinfo/` (package `tzdata`). A zone like
`Europe/Berlin` knows its offset from UTC *and* every daylight saving switch in its history.

```console
$ ls /usr/share/zoneinfo/Europe | head -4
Amsterdam
Andorra
Astrakhan
Athens
```

### The system time zone: /etc/localtime and /etc/timezone

`/etc/localtime` decides the system's zone. Nowadays it's a symbolic link into the database:

```console
$ ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Dec  1 10:00 /etc/localtime -> /usr/share/zoneinfo/Europe/Berlin
```

On older systems it may be a copy of the zone file instead of a link. Debian and Ubuntu also keep the zone *name* in
a plain text file, `/etc/timezone`, that some tools still read:

```console
$ cat /etc/timezone
Europe/Berlin
```

RHEL and Fedora don't use `/etc/timezone`.

### Changing it: timedatectl

`timedatectl` is the systemd tool, and the easiest way on any current distribution:

```console
$ timedatectl
               Local time: Tue 2026-12-01 14:30:00 CET
           Universal time: Tue 2026-12-01 13:30:00 UTC
                 RTC time: Tue 2026-12-01 13:30:00
                Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
$ timedatectl list-timezones | grep -i toronto
America/Toronto
$ sudo timedatectl set-timezone America/Toronto
```

Without systemd, or to understand what that command does, change the link yourself:

```bash
sudo ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime
echo "America/Toronto" | sudo tee /etc/timezone      # Debian and Ubuntu only
```

On Debian and Ubuntu, `sudo dpkg-reconfigure tzdata` does both with a menu.

### Finding your zone: tzselect

Not sure of the exact name? `tzselect` asks a few questions (continent, country, sometimes a region) and prints the
`TZ` value:

```console
$ tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
...
#? 2
Please select a country whose clocks agree with yours.
...
Therefore TZ='America/Toronto' will be used.
...
You can make this change permanent for yourself by appending the line
	TZ='America/Toronto'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
America/Toronto
```

`tzselect` changes **nothing** on the system. It only tells you the name. That's a common exam trick.

### Per user and per command: the TZ variable

The `TZ` environment variable overrides `/etc/localtime` for one process, or for a user who exports it in
`~/.profile`. It's handy to see the time elsewhere:

```console
$ date +"%F %T %Z"
2026-12-01 14:30:00 CET
$ TZ=Asia/Tokyo date +"%F %T %Z"
2026-12-01 22:30:00 JST
$ TZ=America/New_York date +"%F %T %Z"
2026-12-01 08:30:00 EST
$ date -u +"%F %T %Z"
2026-12-01 13:30:00 UTC
```

A user on a shared server who lives elsewhere can add `export TZ="America/Toronto"` to their `~/.profile`. The
system keeps its own zone, their shell shows their local time.

### More date tricks

`date` shows the time in whatever format you ask for, using the current locale and zone:

```console
$ date
Tue Dec  1 02:30:00 PM CET 2026
$ date +"%A %d %B %Y, %H:%M"
Tuesday 01 December 2026, 14:30
$ date -d "next friday" +%F
2026-12-04
$ date -d @0 -u
Thu Jan  1 12:00:00 AM UTC 1970
$ date +%s
1796131800
```

(The first and last lines use the `en_US.UTF-8` 12-hour format. Under `C` you'd see `14:30:00` and `00:00:00`.)
Setting the clock with `date` and `hwclock` is part of the next topic, system time.

## Exam checklist

- Locale names: `language_TERRITORY.codeset`, for example `de_DE.UTF-8`. `C` and `POSIX` are the plain default.
- Precedence: `LC_ALL` beats `LC_*` beats `LANG`. `LC_ALL=C` gives scripts predictable sorting and output.
- `locale` shows the settings, `locale -a` the installed locales. `LC_TIME`, `LC_NUMERIC`, `LC_COLLATE`, `LC_MESSAGES` are the ones to know by heart.
- System locale: `/etc/locale.conf` (RHEL) or `/etc/default/locale` (Debian), or `localectl set-locale`.
- `/etc/localtime` is a link to (or copy of) a file in `/usr/share/zoneinfo/`. `/etc/timezone` holds the zone name on Debian and Ubuntu.
- `timedatectl set-timezone Zone/City` changes the system zone. `tzselect` only helps you find the name.
- `TZ=Zone/City` overrides the zone for one command or one user. `date -u` shows UTC.
- ASCII is 7-bit, ISO-8859-x is 8-bit per region, Unicode is the character list, UTF-8 stores it in 1 to 4 bytes and is ASCII compatible.
- `iconv -f FROM -t TO file`, `iconv -l` lists encodings, `//TRANSLIT` approximates characters the target lacks.


## Byte check

1. `LANG=en_US.UTF-8`, `LC_TIME=de_DE.UTF-8` and `LC_ALL=fr_FR.UTF-8` are all set. In which language does `date` print the day name?
   - English, because `LANG` is the main setting
   - German, because `LC_TIME` is about time
   - **French, because `LC_ALL` overrides everything** (correct)
   The order is `LC_ALL`, then the specific `LC_*` variable, then `LANG`. `LC_ALL` beats them all.

2. What is `/etc/localtime` on most modern systems?
   - A text file with a zone name like `Europe/Berlin`
   - **A symbolic link to a file in `/usr/share/zoneinfo/`** (correct)
   - A script that sets the `TZ` variable at login
   It points to the binary zone file in the time zone database. The text file with the zone name is Debian's `/etc/timezone`.

3. An old file is stored in ISO-8859-1 (Latin-1). Which command turns it into UTF-8?
   - **`iconv -f ISO-8859-1 -t UTF-8 old.txt -o new.txt`** (correct)
   - `iconv -f UTF-8 -t ISO-8859-1 old.txt -o new.txt`
   - `LANG=C.UTF-8 cat old.txt > new.txt`
   `-f` is the encoding the file has now, `-t` the one you want. Setting `LANG` changes how text is shown, not the bytes in a file.


