[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bug#383321: su-to-root => localisation ;-)



Hello Bil,

I am sitting in the Internet Cafee in Strasbourg and stay there up to 19:00
Please test the new version of the script...

'xgettext' will produce 8 strings to translate:

msgid "usage: $0 [-X] [-p <user>] -c <command>\\n  -c command: command to execute\\n  -p <user>: user to switch to (default: root)\\n  -X: command is a X11 program\\n"
msgid "About to execute $COMMAND.\\nThis command needs $PRIV privileges to be executed.\\n
msgid "Using $suname...\\nEnter $PRIV passwd:\\n"
msgid "Y"
msgid "y"
msgid "N"
msgid "n"
msgid "Incorrect password or command failed. Try again? (y/n)'"


Am 2007-04-19 22:48:02, schrieb Bill Allombert:
> On Sun, Aug 20, 2006 at 08:28:26AM -0500, Christian Perrier wrote:
> > >   GT5=$(gettext 'Incorrect password or command failed. Try again?')
> > >   echo -n "${GT5} (${ANS})"
> > >   read ans
> > >   if   [ ${FLAG} -eq 0 ] ; then
> > >     if test "${ans}" != "y" -a "${ans}" != "Y"; then exit 1 ; fi
> > >   elif [ ${FLAG} -eq 1 ] ; then
> > >     if test "${ans}" != "j" -a "${ans}" != "J"; then exit 1 ; fi
> > >   elif [ ${FLAG} -eq 2 ] ; then
> > >     if test "${ans}" != "o" -a "${ans}" != "N"; then exit 1 ; fi
> > >   fi
> > > }
> > 
> > I'm not sure how to do it in shell programming, but this anyway
> > doesn't scale very well to other languages. The locale information
> > should be used here (the Yes/No shortcuts are part of locale
> > files....I'm however not aware of the exact way to use them).
> 
> I have rewritten su-to-root to be more l10n-friendly by removing text
> duplication and using printf.
> 
> I need two advices from the l10n experts:
> 1) the best way to check if gettext is available.

sonce the gettext strings in a shell script are:

   gettext 'Hello Bil!'
or
   eval_gettext "Hellp ${FRIEND}!"

I use now at the beginning of the shell scripts:

----8>----------------------------------
if [ -n "$(which gettext)" ] ; then
  . gettext.sh
  TEXTDOMAIN=exampleprog
  TEXTDOMAINDIR=/usr/share/locale
else
  gettext()
  {
    echo "$1"
  }
  eval_gettext()
  {
    echo "$1"
  }
fi
----8>----------------------------------

Which works without any errors...


> 2) the best way to deal with the yes/no prompting above.

echo "Please answer with Y or N:"

> 
> Is there good examples of l10n scripts in Debian?
  ^^
  Are

Unfortunatly not, but maybe we can ask the "gettext" maintainer to
add my sniplet

> I have attached the current, mostly untested, version.

I will test it...

----8<------------------------------------------------------------------
#!/bin/bash

if test -r ~/.su-to-rootrc; then
. ~/.su-to-rootrc
fi

PRIV=root
COMMAND=
NEEDS=text

if [ -n "$(which gettext)" ] ; then
  . gettext.sh
  TEXTDOMAIN=su-to-root
  TEXTDOMAINDIR=/usr/share/locale
else
  gettext()
  {
    echo "$1"
  }
  eval_gettext()
  {
    echo "$1"
  }
fi

eshell() {
   getent passwd $1 | cut -f7 -d:
}

usage () {
  txt=$(gettext "usage: $0 [-X] [-p <user>] -c <command>\n  -c command: command to execute\n  -p <user>: user to switch to (default: root)\n  -X: command is a X11 program\n")
  echo -e "$txt" >&2
  exit 1
}

for i in "$@"; do
   case "$prev" in
     -p)
       PRIV="$i";;
     -c)
       COMMAND="$i";;
     -X) 
       NEEDS="X11";;
   esac
   prev="$i"
done

if [ -z "$COMMAND" ] ; then
   usage;
fi

euid=$(id -u)
privid=$(id -u $PRIV)
if test "$euid" = "$privid"; then
   $COMMAND
else
  case $NEEDS in
  text)
    if test "$euid" != 0; then
      txt=$(eval_gettext 'About to execute $COMMAND.\nThis command needs $PRIV privileges to be executed.\n')
      echo -e "$txt"
    fi
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11:/usr/local/sbin:/usr/local/bin
    SHELL=`eshell $PRIV`
    case $SU_TO_ROOT_SU in
      sux)  suname=sux; cmd='sux  -p "$PRIV"    "$COMMAND"';;
      sudo) suname=sudo;cmd='sudo -u "$PRIV"    "$COMMAND"';;
      *)    suname=su;  cmd='su   -p "$PRIV" -c "$COMMAND"';;
    esac
    txt=$(eval_gettext 'Using $suname...\nEnter $PRIV passwd:\n')
    echo -e "${txt}"
    while ! eval $cmd; do
      GT_Y=$(gettext 'Y')
      GT_y=$(gettext 'y')
      GT_N=$(gettext 'N')
      GT_n=$(gettext 'n')
      txt=$(gettext 'Incorrect password or command failed. Try again? (y/n)')
      echo -e "${txt}"
      read ans
      if test "$ans" != "${GT_y}" -a "$ans" != "${GT_Y}"; then
        exit 1
      fi
    done;;
  X11)
    if test -z "$SU_TO_ROOT_X"; then
      if which gksu >/dev/null 2>&1 ; then
        if test "X$KDE_FULL_SESSION" = "Xtrue" \
          && which kdesu >/dev/null 2>&1 ; then
          SU_TO_ROOT_X=kdesu
        else
          SU_TO_ROOT_X=gksu
        fi;
      elif which kdesu >/dev/null 2>&1 ; then 
        SU_TO_ROOT_X=kdesu
      elif which sux >/dev/null 2>&1 ; then 
        SU_TO_ROOT_X=sux
      else
        SU_TO_ROOT_X=su-to-root
      fi
    fi
    case $SU_TO_ROOT_X in
      gksu) gksu -u "$PRIV" "$COMMAND";;
      kdesu) kdesu -u "$PRIV" "$COMMAND";;
      sux) env SU_TO_ROOT_SU=sux \
        x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND";;
  # As a last resort, open a new x-terminal-emulator and prompt for the password
  # Do not use -X here!
      *) x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND";;
    esac;;
  esac
fi
----8<------------------------------------------------------------------


Thanks, Greetings and nice Day
    Michelle Konzack
    Systemadministrator
    Tamay Dogan Network
    Debian GNU/Linux Consultant


-- 
Linux-User #280138 with the Linux Counter, http://counter.li.org/
##################### Debian GNU/Linux Consultant #####################
Michelle Konzack   Apt. 917                  ICQ #328449886
                   50, rue de Soultz         MSN LinuxMichi
0033/6/61925193    67100 Strasbourg/France   IRC #Debian (irc.icq.com)

Attachment: signature.pgp
Description: Digital signature


Reply to: