Skip to main content

Detect a UTF-8-capable locale for running programs in

Project description

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 feature-check library was written by Peter Pentchev. It is developed in a GitLab repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

utf8_locale-1.0.3.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

utf8_locale-1.0.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file utf8_locale-1.0.3.tar.gz.

File metadata

  • Download URL: utf8_locale-1.0.3.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for utf8_locale-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ee7f765dde005c8cc94df416d53b6951b2c08d9bb92f903f6dd22df592019767
MD5 5c59b3e64ae4c1ec902107ec44958c1c
BLAKE2b-256 f4ab3fbd14b1c57b12f812f7c6bf8cd21ed8de2ef15532a94a9dcbce44f40668

See more details on using hashes here.

File details

Details for the file utf8_locale-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: utf8_locale-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for utf8_locale-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 42b6615096609b13403bc1df767ec0338b9ccb2bcfe1e5be0d38682cbc5a183b
MD5 1337165a2a8579a065086c2819cf0b41
BLAKE2b-256 9ac3f1c4a39a3cd3af3d1c29d69b3da90e57371cab123234b05f66656725740a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page