po-check-translate 7.41 KB
Newer Older
Alban Vidal's avatar
Alban Vidal committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#!/bin/bash

# VERSION 1.0 - 2018-01-03
#
# po-check-translate - Check file after translation, especially designed to french
#
# Copyright (C) 2017, 2018 Alban Vidal <alban.vidal@zordhak.fr>
#
# GNU GENERAL PUBLIC LICENSE Version 3
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.

################################################################################
##########                       Exit number list:                    ########## 
################################################################################
# 0 => OK
# 1 => error or normal error is found
# 2 => file_to_check is required
# 3 => Unknown option
# 4 => file_to_check don't exist or not readable
# 5 => Encoding error. The file is not in UTF-8

################################################################################
##########                    Define color to output:                 ########## 
################################################################################
_WHITE_="tput sgr0"
_RED_="tput setaf 1"
_GREEN_="tput setaf 2"
_ORANGE_="tput setaf 3"

################################################################################

# Get script name
_SCRIPT_NAME_=${0##*/}

# Usage (called by -h or --help)
function usage()
{

    echo "Usage: $_SCRIPT_NAME_ [options] file_to_check"
    echo "Options:"
    echo "    -h, --help                print this help"
    echo "    -l, --licence             print licence"
    exit 0
}

function licence()
{
    echo " ——————————————————————————————————————————————————————————————————————————————————————————"
    echo "|    po-check-translate Copyright (C) 2017, 2018 Alban Vidal <alban.vidal@zordhak.fr>       |"
    echo "|    This program comes with ABSOLUTELY NO WARRANTY                                         |"
    echo "|    This is free software, and you are welcome to redistribute it under certain conditions |"
    echo " ——————————————————————————————————————————————————————————————————————————————————————————"
    echo ""
    echo "    GNU GENERAL PUBLIC LICENSE Version 3"
    echo ""
    echo "    This program is free software: you can redistribute it and/or modify"
    echo "    it under the terms of the GNU General Public License as published by"
    echo "    the Free Software Foundation, either version 3 of the License, or"
    echo "    (at your option) any later version."
    echo ""
    echo "    This program is distributed in the hope that it will be useful,"
    echo "    but WITHOUT ANY WARRANTY; without even the implied warranty of"
    echo "    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
    echo "    GNU General Public License for more details."
    echo ""
    echo "    You should have received a copy of the GNU General Public License"
    echo "    along with this program.  If not, see <https://www.gnu.org/licenses/>."
    echo ""
    exit 0
}

# Print error messages
function error()
{
    exit_nb=$1
    shift
    echo "$($_RED_)ERROR$($_WHITE_): $@" >&2
#    echo "$($_RED_)ERROR$($_WHITE_): $($_ORANGE_)$@$($_WHITE_)" >&2
    exit $exit_nb 
}

# Check args
opt="$1"
while test ! -z "$opt" -a -z "${opt##-*}"
do

    case "$opt" in
        "-h"|"--help")
            usage
            ;;
        "-l"|"--licence")
            licence
            ;;
        *)
            error 3 Unknown option: $($_ORANGE_)$1$($_WHITE_)
            ;;
    esac

    shift
    opt="$1"
done

################################################################################

# Test if remaining arg is 'one': file_to_check
if [ ! $# -eq 1 ] ; then
    error 2 file_to_check is required, to usage see: $_SCRIPT_NAME_ --help
fi

# File to check
file_to_check=$1

# Check if $file_to_check exist and if is readable
# -f => FILE exists and is a regular file
# -r => FILE exists and read permission is granted
if [ ! -f $file_to_check -a ! -r $file_to_check ] ; then
    error 4 file $($_ORANGE_)$file_to_check$($_WHITE_) don\'t exist or is not readable
fi

################################################################################

# Define start of output
OUTPUT="Checking $($_ORANGE_)${file_to_check}$($_WHITE_):"

################################################################################

# ~~ Vérification du format de l'encodage ~~
# file -b, --brief => Do not prepend filenames to output lines (brief mode).
# file -i, --mime  => Causes the file command to output mime type strings rather than the more traditional human readable ones.  Thus it may say ‘text/plain; charset=us-ascii’ rather than “ASCII text”.
if  ( ! file -bi $file_to_check|grep --quiet 'charset=utf-8' ) || ( ! grep --quiet 'Content-Type: text/plain; charset=UTF-8' $file_to_check ) ;then
#    echo "Erreur d'encodage : Le format n'est pas en UTF-8"
    error 5 Encoding error. The file is not in UTF-8
fi

# ~~ Mises en forme et vérifications ~~
#
# Cat du fichier avec insertion du numéro de ligne
cat -n $file_to_check | \
#
# On retire les espaces avant les numéros de lignes
sed -E 's/^( )+//' | \
#
# - On retire les lignes en commentées et les lignes vides
grep -vE $'^[0-9]+\t(#(~|,|.|:)|$)' |\
#
# - On retire tous les en-têtes de traduction
grep -vE $'^[0-9]+\t"(Project-Id-Version|Report-Msgid-Bugs-To|POT-Creation-Date|PO-Revision-Date|Last-Translator|Language-Team|Language|MIME-Version|Content-Type|Content-Transfer-Encoding|X-Generator|Plural-Forms):' | \
#
# - On utilise grep en Perl, on met tout sur une ligne et on ne prend que les lignes traduites
grep -Poz '[0-9]+\tmsgstr.*(\n([0-9]+\t".*"\n)*)' | \
#
# Une fois la chaine bien formatée, on fait les tests :
# - On recherche les espaces à remplacer par des insécables, ou les espaces en doubles
# ([a-Z():;«»\?](\ )?(;|:|\?|»)) > Espace (ou absence d'espace insécable) après une lettre ou une ponctuation, avant une ponctuation
# (« )                           > Espace après un guillemet gauche
# (: "$)                         > Espace en fin de ligne après un deux-points
# ( [0-9])                       > Espace avant un chiffre
# (  )                           > Espace doubles
# (" )                           > Espace en début de ligne
#
if RETURN=$(GREP_COLOR='1;41' grep -aE '([a-Z():;«»\?](\ )?(;|:|\?|»))|(« )|(: "$)|( [0-9])|(  )|(" )' --color=always) ; then
    # Grep match
    echo "$OUTPUT $($_RED_)Check the following lines$($_WHITE_):"
    # Et on affiche les lignes à vérifier (en couleur tant qu'à faire)
    echo "$RETURN" | sed "s/\([0-9]\+\)\t/ERR line $($_RED_)\1$($_WHITE_) : /"
    exit 1
else
    # No grep => OK
    echo "$OUTPUT $($_GREEN_)Done, no error$($_WHITE_)."
    exit 0
fi