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
- A text editor. Any will do, though VIM will excell.
- 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.
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.
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.
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.
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.
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!
If you execute locale
you can see the language variables that are used for various sections of the operating system:
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.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=
More about locale variables in the gettext manual: Locale Environment Variables - GNU `gettext' utilities
You can export these variables e.g. export LC_PAPER="ru_RU.UTF-8"
- this will make a temporary change.
If you want to make it permanent you can change the regional formats using gnome-language-selector
. Or you can add the export command in ~/.profile and/or ~/.bashrc
For the papersize, I found that you can edit /etc/papersize
- change "letter" to "a4".
You might also need to set export LC_PAPER="en_GB.UTF_8"
(or maybe export LC_PAPER="ru_RU.UTF_8"
if ru_RU defaults to A4)
Source: answers.launchpad.net: Question #6846 : Questions : “evince” package : Ubuntu
P.S. Personally I use en_GB (English / United Kingdom) for everything since it uses the SI metric system instead of American English and US units.
Best Answer
We've got documentation on the wiki pages that describe how to add a new language to Ubuntu. In a nutshell:
Choose the name of the locale. The format is
language_REGION@modifier
, where language is an ISO 639-2 two-letter code (or the three-letter one if the former is not available), REGION is an ISO 3166 code representing the region where this language is spoken, and modifier has no set syntax and can be used to specify advanced uses for the locale (e.g. a different script). Most locales will not need a modifier.Create a file with that name. Once the name has been chosen, you'll have to create a file with that name, which will contain the definition of your locale. Note that if your language is spoken in different regions, you might have to create different files, one for each region.
Define the locale. At this point you'll have to populate the file with the definition of your language. Locale definition files have got a specific syntax. Consult the additional resources below to learn about this syntax and how to define the new locale. Looking through the available locale files in the glibc sources can be helpful to get an idea of the format. Also remember to reuse: use the copy statement to include sections from locales with identical content.
Test the locale definition. Once your locale file or files are ready, you should test them locally to ensure they are correct. The basic steps are:
copying the file to
/usr/local/share/i18n/locales/
,running the following command to generate a binary file to be used by applications, and doing the actual test.
Example (testing the Asturian locale with the date command):
Additional resources on creating a glibc locale: