Commit a6e8466b authored by Alban Vidal's avatar Alban Vidal

Initial commit

parents
#!/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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment