Skip to main content

Detect a UTF-8-capable locale for running child processes in

[Home | Download | GitLab | PyPI | crates.io | ReadTheDocs]

Overview

Sometimes it is useful for a program to be able to run a child process and more or less depend on its output being valid UTF-8. This can usually be accomplished by setting one or more environment variables, but there is the question of what to set them to - what UTF-8-capable locale is present on this particular system? This is where the utf8_locale module comes in.

Examples

For the Rust implementation:

use std::process;

use utf8_locale;

let utf8env = utf8_locale::Utf8Detect()::new().detect()?;
let cmd = process::Command::new(...).env_clear().envs(utf8_env.env);

For the Python implementation:

import subprocess

import utf8_locale

utf8env = utf8_locale.Utf8Detect().detect()
subprocess.check_output([...], encoding="UTF-8", env=utf8env.env)

Classes (Python and Rust)

LanguagesDetect

The detect() method of this class examines either the provided environment variables or the current process's environment and returns a list of language codes in order of preference that may then be used for determining which UTF-8-capable locale to use.

Utf8Detect

The detect() method of this class runs the external locale command to obtain a list of the supported locale names, and then picks a suitable one to use so that programs are more likely to output valid UTF-8 characters and language-neutral messages. It prefers the C base locale, but if neither C.UTF-8 nor C.utf8 is available, it will fall back to a list of other locale names that are likely to be present on the system. The list of preferred language codes is configurable.

Functions

Note that for the Python and Rust implementation it is recommended to use the Utf8Detect and, if needed, the LanguagesDetect builder classes to perform the detection.

detect_utf8_locale()

The detect_utf8_locale() function runs the external locale command to obtain a list of the supported locale names, and then picks a suitable one to use so that programs are more likely to output valid UTF-8 characters and language-neutral messages. It prefers the C base locale, but if neither C.UTF-8 nor C.utf8 is available, it will fall back to a list of other locale names that are likely to be present on the system.

get_utf8_vars()

The get_utf8_vars() function invokes detect_utf8_locale() and then returns a dictionary/hashmap containing two entries: LC_ALL set to the obtained locale name and LANGUAGE set to an empty string so that recent versions of the gettext library do not choose a different language to output messages in.

get_utf8_env()

The get_utf8_env() function invokes detect_utf8_locale() and then returns a dictionary/hashmap containing the current environment variables, LC_ALL set to the obtained locale name, and LANGUAGE set to an empty string so that recent versions of the gettext library do not choose a different language to output messages in.

get_preferred_languages()

The get_preferred_languages() function examines either the current process environment or the provided dictionary and returns a list of the languages specified in the locale variables (LC_ALL, LANG, LC_MESSAGES, etc). It may be used by programs to add the user's currently preferred locale to their own settings.

Contact

The utf8-locale library was written by Peter Pentchev. It is developed in a GitLab repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.

Metadata

Release files for utf8-locale 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for utf8-locale 1.1.1
File Size Uploaded
utf8_locale-1.1.1.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for utf8-locale 1.1.1
File Interpreter ABI Platform
utf8_locale-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.9 kB

Release files / utf8_locale-1.1.1.tar.gz

Download URL utf8_locale-1.1.1.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4dc3b06d850118aeb02bd151dfedf630bf89f4519cc41746907ce64f9251831f
BLAKE2b-256 checksum
How to use checksums
5c0bb172493e6507b5ee2d6f5e1503eb0bc5db86e9271838dd34c765bb2dbe7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / utf8_locale-1.1.1-py3-none-any.whl

Download URL utf8_locale-1.1.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a95e43798e0a230a7a38a8c7393daefdff06044e721bc5d0bf9125e67a888bc
BLAKE2b-256 checksum
How to use checksums
732962ef9e4bdff9527f6f78cf0ba4e8096cc09b10ff9cb3798217886d2436bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page