How can I customize a system locale?

Solution 1:

This answer will demonstrate how to fully customize a locale, optionally using an existing locale as a base. The type of locale that will be used in this answer is the i18n type; such type of locale will be implied in every mention of the word "locale" in the body of the answer.

Find a guide

The first thing you need to customize a locale, is get to know the format of the locale. This web locale editor has a comprehensive description of every field in a locale, despite not being usable by just anyone in a right-away manner. This will be the guide to use.

Needed tools

The only tools you need to work with locales are

  1. A text editor. Any will do, though VIM will excell.
  2. A text-to-unicode converter. It would be most expected that you didn't have one, so I will provide you with a simple one. Read the next subsection to obtain it.

I recommend you to create a directory and save everything related with the work you will do on the locale to it.

Text-to-unicode converter (encodestr)

NOTE: if you are using non-common characters you may have problems with the script I write below; you might want to take a look at the alternative scripts linked by nass.

Save the following code to a file. This file will be identified by your system as a python script, which is alright because it is one. This guide assumes you called the script encodestr.

#!/usr/bin/env python3

import sys

def main():
    if len(sys.argv) != 2:
        exit(f'Usage: {sys.argv[0]} <string to encode>')
    print(encode(sys.argv[1]))

def encode(s):
    return ''.join(f"<U{ord(c):04X}>" for c in s)

if __name__ == '__main__':
    sys.exit(main())

After you have saved it, give the file execution permission - this will come in handy. You can do that using nautilus, or by running from a terminal the following command, on the directory in which you saved the script: chmod +x encodestr.

You can now test this script by running on the same directory: ./encodestr hello. You should get an output equal to <U0068><U0065><U006C><U006C><U006F>.

Modify an existing locale

Modifying an existing locale is the easiest case, as ideally you will pick a locale that fits your needs almost totally, in which case you need to make only small modifications. To tailor it, you can use the guide to know which of the sections are the ones you want to change, and how you can change them. All the installed locales reside in /usr/share/i18n/locales, however the guide contains pretty much every locale ever created.

However, the first thing you will notice when you open a locale is that you will read english words as much as weird <UXXXX> codes. You will need to be able to create these codes - this is where the encodestr script comes.

When you know which sections you have to change, you need to know each field contained in the section.

For example, the LC_TIME section contains the field t_fmt, and many others. Each field has its own description on the guide.

Now that you know which fields to modify, use the guide again to find out what values does the field accept.

For example, the field t_fmt accepts text and the common strfmt variables.

Just when you know what to write on the fields you want to modify, run the text through the encodestr script to get its encoded equivalent.

NOTE: Not every field needs to be encoded, use an existing locale as a reference if needed.

For example, if you want to have the time format Time: %T in the t_fmt field, run ./encodestr "Time: %T"; this will return <U0054><U0069><U006D><U0065><U003A><U0020><U0025><U0054> and so you will have the line t_fmt <U0054><U0069><U006D><U0065><U003A><U0020><U0025><U0054> in your locale (in it's respective section, of course).

After you have modified every field you wanted to, you will need to proceed to install the locale to the system, so it can be used. Proceed to the installation section for this.

Creating a locale from scratch

As opposed to modifying one, the other method you can use to create your custom locale is working on it from scratch. This is not very difficult if - again - you use the guide. Here's a useful tip though: you can copy sections from other locales without actual copy and paste methods. You just have to follow this format:

LC_SECTION
copy "locale"
END LC_SECTION

Where LC_SECTION is the section you want to modify, and locale is the locale you want to copy that section from.

For example, if you want to copy the time format in the English locale, you would have this text your custom locale file:

LC_TIME
copy "en_GB"
END LC_TIME

And that saves you a lot of work and assures that any change made to the referenced locale will be mirrored to yours.

Installing your custom locale

The steps I am going to show below may not be optimal, but work.

  1. Compile your locale. Don't panic! This takes no time. Just run the command sudo localedef -i custom -f UTF-8 custom.UTF-8 -c -v where custom is the name of your locale.

  2. Copy your locale to the /usr/share/i18n/locales/ directory. You will definitely need sudo priviledges to do this, and you can do it with the command sudo cp locale /usr/share/i18n/locales/ where locale is the filename of your custom locale.

  3. Specify your locale in /var/lib/locales/supported.d/. For example, create a file name /var/lib/locales/supported.d/asd and add the line custom UTF-8, where custom is the name of your locale.

  4. Run the magic command. I actually don't know what exactly this command does, but it is crucial. Run sudo locale-gen. It is most probable it generates references to the available locales where needed.

  5. Tell your system to use the locale. If you want the locale to be used on a system-wide manner, modify the /etc/environment file. If not, you can modify your ~/.profile file, or any other one that is sourced at startup. If you don't know what the latter means don't worry, just use the ~/.profile file. To know what you have to write in it, first run the locale command. It should issue a list with items like LANGUAGE, LC_MESSAGES, LC_CTYPE and many others. You will write, for each of the sections you want (listed by the previous command), on separate lines, this SECTION="locale" where SECTION is the section and locale the name of your custom locale.

For example, if you want to use the time format from the Netherlands, but the english language, write:

LANGUAGE="en_GB:en"
LC_TIME="nl_NL"

NOTE: If you want to use one locale for every locale setting, write LC_ALL="locale" where locale is the name of your custom locale. If you want to use a locale for everything but for the language, you can write LANG="locale" and (on another line, of course) LANGUAGE="locale":XX where XX is the 2 letter code for the language you want.

For example, if you want to use the Dutch locale and the English language, you could use:

LANG="nl_NL"
LANGUAGE="nl_NL:en"

You have finished

The next time you log in, your locale should be in use. You can test this by running the locale command again.

Please comment about inconsistencies or false statements in this guide!

Solution 2:

Not sure what kind of customization you want, but you can set some locale variables using your ~/.locale. For example, to get english messages but german time and the likes I use

export LC_ALL=""
export LANG="en_GB.utf8"
export LC_CTYPE="de_DE.utf8"
export LC_NUMERIC="de_DE.utf8"
export LC_TIME="de_DE.utf8"
export LC_COLLATE="de_DE.utf8"
export LC_MONETARY="de_DE.utf8"
export LC_MESSAGES="en_GB.utf8"
export LC_PAPER="de_DE.utf8"
export LC_NAME="en_GB.utf8"
export LC_ADDRESS="de_DE.utf8"
export LC_TELEPHONE="de_DE.utf8"
export LC_MEASUREMENT="de_DE.utf8"
export LC_IDENTIFICATION="de_DE.utf8"

Generating custom locales is also possible. See e.g. this guide for dates.