You are not logged in.

#1 2017-04-23 11:46:01

karlospv94
Member
Registered: 2016-10-12
Posts: 100

How to localize a string with colors and variables in bash

I have some colors defined out of the function and also a function where I want to use these colors and localize the string where I use them:

. gettext.sh

(...)

CC="\033[1;36m"
WC="\033[1;97m"
NC="\033[0m"

(...)

init() {
    pkg="$1"
    echo -e "Package ${WC}${pkg}${NC} ${CC}not found${NC}."
}

(...)

I have refactorized the function init to:

init() {
    eval_gettext "Package \${WC}\${pkg}\${NC} \${CC}not found\${NC}."; echo -e
}

When I try to get the msgids I can see the following:

#, sh-format
msgid "Package ${WC}${pkg}${NC} ${CC}not found${NC}."
msgstr ""

But when I execute the script, then I see this message:

/usr/bin/gettext.sh: line 88: export: `WC
pkg
NC
CC
NC': not a valid identifier
Package  not found.

In fact, shellcheck is advising that the color variables are not being used...

How could I refactorize it to work?


Edit:

This code works:

printf "$(gettext "Package %b%s %bnot found%b.\n")" "$WC" "$pkg" "$CC" "$NC"

But It has the problem that a command would not be executed in the fist param of the printf

Last edited by karlospv94 (2017-04-23 17:16:59)

Offline

#2 2017-04-23 15:34:16

DopustimVladimir
Member
From: Bishkek (Kyrgyzstan)
Registered: 2017-04-23
Posts: 9

Re: How to localize a string with colors and variables in bash


Web oriented programmer

Offline

#3 2017-04-23 15:41:44

karlospv94
Member
Registered: 2016-10-12
Posts: 100

Re: How to localize a string with colors and variables in bash

DopustimVladimir wrote:

Sorry, I don't see the difference

Offline

Board footer

Powered by FluxBB