11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appen<strong>di</strong>ce I. Localizzazione<br />

La localizzazione è una funzionalità <strong>di</strong> <strong>Bash</strong> non documentata.<br />

Uno script <strong>di</strong> shell localizzato visualizza il testo dell’output nella lingua che è stata definita, nel sistema,<br />

come locale. Un utente Linux <strong>di</strong> Berlino, Germania, preferirebbe gli output degli script in tedesco,<br />

mentre suo cugino <strong>di</strong> Berlin, Maryland, li vorrebbe in inglese.<br />

Per creare uno script localizzato, che visualizzi nella lingua dell’utente tutti i messaggi (messaggi<br />

d’errore, prompt, ecc,), si usi lo schema descritto nel seguente script.<br />

#!/bin/bash<br />

# localized.sh<br />

# Script <strong>di</strong> Stéphane Chazelas,<br />

#+ mo<strong>di</strong>ficato da Bruno Haible, corretto da Alfredo Pironti.<br />

.gettext.sh<br />

E_CDERROR=65<br />

error()<br />

{<br />

printf "$@" >&2<br />

exit $E_CDERROR<br />

}<br />

cd $var || error "‘eval_gettext \"Can\’t cd to \\\$var.\"‘"<br />

# È necessaria la tripla barra inversa (escape) davanti a $var<br />

#+ "perché eval_gettext si aspetta una stringa<br />

#+ dove i valori della variabile non sono ancora stati sostituiti."<br />

# -- secondo Bruno Haible<br />

read -p "‘gettext \"Enter the value: \"‘" var<br />

# ...<br />

# ----------------------------------------------------------------------------<br />

# Commento <strong>di</strong> Alfredo Pironti:<br />

# Lo script è stato mo<strong>di</strong>ficato in modo da non utilizzare la sintassi<br />

#+ $"..." in favore <strong>di</strong> "‘gettext \"...\"‘".<br />

# Questo è corretto, ma con il nuovo programma localized.sh, i<br />

#+ coman<strong>di</strong> "bash -D nomefile" e "bash --dump-po-string nomefile"<br />

#+ non produrrebbero alcun risultato<br />

#+ (perché quei coman<strong>di</strong> cercherebbero solo le stringhe $"...")!<br />

# Il SOLO modo per estrarre le stringhe dal nuovo file è quello <strong>di</strong> usare<br />

#+ il programma ’xgettext’. Il programma xgettext, però, presenta dei problemi.<br />

# Fra gli altri, va notato quest’altro problema <strong>di</strong> ’xgettext’.<br />

787

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!