fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
5a1224757
master
1# localization: skip(private) 2 3# Try to set the locale from the system configuration if we did not inherit any. One case where this 4# can happen is a linux with systemd where the user logs in via getty (e.g., on the system console). 5# See https://github.com/fish-shell/fish-shell/issues/3092. This isn't actually our job, so there's 6# a bunch of edge-cases we are unlikely to handle properly. If we get a value for _any_ language 7# variable, we assume we've inherited something sensible so we skip this to allow the user to set it 8# at runtime without mucking with config files. 9# 10# NOTE: This breaks the expectation that an empty LANG will be the same as LANG=POSIX, but an empty 11# LANG seems more likely to be caused by a missing or misconfigured locale configuration. 12 13function __fish_set_locale 14set -l LOCALE_VARS( __fish_locale_vars ) 15 16# We check LC_ALL to figure out if we have a locale but we don't set it later. That is because 17# locale.conf doesn't allow it so we should not set it. 18for locale_varin $LOCALE_VARS LC_ALL 19if set -q$locale_var 20return 0 21end 22end 23 24# Try to extract the locale from the kernel boot commandline. The splitting here is a bit weird, 25# but we operate under the assumption that the locale can't include whitespace. Other whitespace 26# shouldn't concern us, but a quoted "locale.LANG=SOMETHING" as a value to something else might. 27# Here the last definition of a variable takes precedence. 28if test -r /proc/cmdline 29for varin ( string match -ra'locale.[^=]+=\S+' < /proc/cmdline) 30set -l kv( string replace'locale.' '' --$var | string split'=' ) 31# Only set locale variables, not other stuff contained in these files - this also 32# automatically ignores comments. 33if contains --$kv [ 1 ] $LOCALE_VARS 34and set -q kv[ 2 ] 35set -gx$kv [ 1 ] ( string trim -c'\'"' --$kv [ 2 ]) 36end 37end 38end 39 40# Now read the config files we know are used by various OS distros. 41# 42# /etc/sysconfig/i18n is for old Red Hat derivatives (and possibly of no use anymore). 43# 44# /etc/env.d/02locale is from OpenRC. 45# 46# The rest are systemd inventions but also used elsewhere (e.g. Void Linux). systemd's 47# documentation is a bit unclear on this. We merge all the config files (and the commandline), 48# which seems to be what systemd itself does. (I.e. the value for a variable will be taken from 49# the highest-precedence source) We read the systemd files first since they are a newer 50# invention and therefore the rest are likely to be accumulated cruft. 51# 52# NOTE: Slackware puts the locale in /etc/profile.d/lang.sh, which we can't use because it's a 53# full POSIX-shell script. 54set -l user_cfg_dir( set -q XDG_CONFIG_HOME;and echo $XDG_CONFIG_HOME ;or echo ~/.config) 55for fin $user_cfg_dir /locale.conf /etc/locale.conf /etc/env.d/02locale /etc/sysconfig/i18n /etc/default/locale 56if test -r $f 57while read -l kv 58set kv( string split'=' --$kv ) 59if contains --$kv [ 1 ] $LOCALE_VARS 60and set -q kv[ 2 ] 61# Do not set already set variables again - this makes the merging happen. 62if not set -q$kv [ 1 ] 63set -gx$kv [ 1 ] ( string trim -c'\'"' --$kv [ 2 ]) 64end 65end 66end < $f 67end 68end 69end