Package: utf8-locale
Detect a UTF-8-capable locale for running child processes in[Home|Download|GitLab|PyPI|crates.io|ReadTheDocs]OverviewSometimes 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 theutf8_localemodule comes in.ExamplesFor 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)LanguagesDetectThedetect()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.Utf8DetectThedetect()method of this class runs the externallocalecommand 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 theCbase locale, but if neitherC.UTF-8norC.utf8is 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.FunctionsNote that for the Python and Rust implementation it is recommended to
use theUtf8Detectand, if needed, theLanguagesDetectbuilder classes to
perform the detection.detect_utf8_locale()Thedetect_utf8_locale()function runs the externallocalecommand 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 theCbase locale, but if neitherC.UTF-8norC.utf8is available, it will fall back to a list of other
locale names that are likely to be present on the system.get_utf8_vars()Theget_utf8_vars()function invokesdetect_utf8_locale()and then returns
a dictionary/hashmap containing two entries:LC_ALLset to the obtained
locale name andLANGUAGEset 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()Theget_utf8_env()function invokesdetect_utf8_locale()and then returns
a dictionary/hashmap containing the current environment variables,LC_ALLset to the obtained locale name, andLANGUAGEset 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()Theget_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.ContactThefeature-checklibrary was written byPeter Pentchev.
It is developed ina GitLab repository. This documentation is
hosted atRingletwith a copy atReadTheDocs.