bash localization won't work with multilines strings (with strong syntax or through `eval`)

后端 未结 4 1970
日久生厌
日久生厌 2021-01-02 02:15

There is a nice feature in bash, about localization (language translation):

TEXTDOMAIN=coreutils
LANG=fr_CH.utf8
echo $\"system boot\"
démarrage système
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-02 02:22

    If using eval is bad with arbitrary variables, there is a way to do this only when called/needed, in running eval only on message part:

    function lPrintf() {
        local sFormat="$(
            eval 'echo $"'"${1}"'"'.
        )"
        shift
        printf "${sFormat%.}" $@
    }
    
    lPrintf "system boot"
    démarrage système
    
    lPrintf  $'Written by %s, %s, %s,\nand %s.\n' techno moi lui-même bibi
    Écrit par techno, moi, lui-même,
    et bibi.
    

    ( The dot at end of translated string ensure that whole string, including leading line-break, where passed to variable sFormat. They will be dropped with ${sFormat%.} )

提交回复
热议问题