Tool for easy translation

Discussion in 'Plugins/Modules/Addons' started by SinglWolf, Jan 18, 2017.

  1. SinglWolf

    SinglWolf New Member

    I offer you a small bash script to facilitate routine job when translating into another language a large number of identical rows.
    The script's source code is well documented and needs no further description.
    Restrictions:
    1. Before running the script set the variables script to the required values.
    2. In the path to the script must not be spaces.
    3. Before running the script, make sure that you have enough access rights for files translate.
    4. Created template using script requires manual editing. Please note that in the template there will be many rows with the same variables but with different content. These lines should be removed.

    Good luck in translation!
    Code:
    #!/usr/bin/env bash
    #===============================================================================
    #
    #          FILE: replacer.sh
    #
    #         USAGE: ./replacer.sh {prepare|replace|restore}
    #        AUTHOR: Alexander Wolf (SinglWolf), [email protected]
    #  ORGANIZATION: Forum ServerDoma.ru
    #       CREATED: 19.01.2017 18:33:54 YEKT
    #      REVISION:  0.4
    # Copyright (c) 2017, Alexander Wolf
    #===============================================================================
    # This script is free and distributed "as is"
    # Using the script at your own risk
    clear
    tmprootdir="$(dirname $0)"
    echo ${tmprootdir} | grep '^/' >/dev/null 2>&1
    if [ X"$?" == X"0" ]; then
        export ROOTDIR="${tmprootdir}"
    else
        export ROOTDIR="$(pwd)"
    fi
    
    # DEBUG="0" - real replace strings in the translation files
    # DEBUG="1" - only display IDs and substitution strings
    DEBUG="1"
    #
    translate_lang="ru"
    #
    path_to_translate="/usr/local/ispconfig/"
    #
    name_template="template_for_placeholders.txt"
    # Functions
    prepare_file()
    {
        if [ -e "${ROOTDIR}/bacup_ok"  ] ; then
            :
        else
            echo "Backup all files ${translate_lang}*.lng to directory ${ROOTDIR}..."
            find ${path_to_translate} -name "${translate_lang}*.lng" -exec cp --parents {} ${ROOTDIR}/ \;
            echo "Please do not delete this file" > ${ROOTDIR}/bacup_ok
        fi
        if [ -e "${ROOTDIR}/quotation_ok"  ] ; then
            :
        else
            echo "Replacement double quotation to single quotation marks for IDs to all files ${translate_lang}*.lng..."
            find ${path_to_translate} -type f -name "${translate_lang}*.lng" -print0 | xargs -0 sed -i -e "s/\$wb\[\"/\$wb\[\'/g"
            find ${path_to_translate} -type f -name "${translate_lang}*.lng" -print0 | xargs -0 sed -i -e "s/\"\] =/\'\] =/g"
            echo "Replacement completed."
            echo "Please do not delete this file" > ${ROOTDIR}/quotation_ok
        fi
        #
        if [ -e "${ROOTDIR}/list_ok"  ] ; then
            :
        else
            echo "Creating a list of translation files with absolute paths."
            find ${path_to_translate} -type f -name "${translate_lang}*.lng" > list.txt
            echo "Creating a list completed."
            echo "Please do not delete this file" > ${ROOTDIR}/list_ok
        fi
        #
        if [ -e "${ROOTDIR}/${name_template}"  ] ; then
            echo "The template file for the replacement is already there..."
            echo "Please delete this file (${ROOTDIR}/${name_template})."
            echo "Or assign a different name using the value of variable \$name_template"
            exit 3
        else
            echo "Creating template for placeholders..."
            # The parsing of the template, for example:
            # 'Website quota (Harddisk)'~"Website quota (Harddisk)"#
            # |          IDs           | | String for replace     |
            # "~" - the separator between ID and String for replace
            # "#" - the marker of the end of the row
            cat /dev/null > ${ROOTDIR}/template_temp.txt
            find ${path_to_translate} -type f -name "${translate_lang}*.lng" -print0 | xargs -0 cat >> ${ROOTDIR}/template_temp.txt
            sed -i -e "/<?php/d" ${ROOTDIR}/template_temp.txt
            sed -i -e "/?>/d" ${ROOTDIR}/template_temp.txt
            cat ${ROOTDIR}/template_temp.txt | sort -u > ${ROOTDIR}/${name_template}
            sed -i -e "s/\$wb\[\x27/\x27/g" ${ROOTDIR}/${name_template}
            sed -i -e "s/\] = /~/g" ${ROOTDIR}/${name_template}
            cat ${ROOTDIR}/${name_template} | tr '\n' '#' > ${ROOTDIR}/temp.txt
            mv ${ROOTDIR}/temp.txt ${ROOTDIR}/${name_template}
            sed -i -e "s/;#/#\n/g" ${ROOTDIR}/${name_template}
            sed -i -e "s/~\x27/~\"/g" ${ROOTDIR}/${name_template}
            sed -i -e "s/\x27#/\"#/g" ${ROOTDIR}/${name_template}
            cat ${ROOTDIR}/${name_template} | sort -u > ${ROOTDIR}/temp.txt
            mv ${ROOTDIR}/temp.txt ${ROOTDIR}/${name_template}
            rm -rf ${ROOTDIR}/template_temp.txt
            echo "Creating template for placeholders completed."
            exit 0
        fi
    }
    ###################################################################################
    replace_str()
    {
        if [ -e "${ROOTDIR}/${name_template}"  ] ; then
            clear
            if [ X"${DEBUG}" == X"1" ]; then
                :
            else
                echo "Replacing strings in all files ${translate_lang}*.lng"
                echo "Please wait..."
            fi
            #
            IFS1="$IFS"
            #
            for i in $(cat ${ROOTDIR}/list.txt); do
                #
                IFS="#"
                #
                for j in $(cat ${ROOTDIR}/${name_template}); do
                    #
                    IFS="~"
                    b="$(echo "${j}")"
                    word_1=$(echo "${b}" | grep -o '^[^~]*')
                    word_1=$(echo "${word_1}" | sed "s~^[ \t]*~~")
                    a(){ shift;echo "$@";}
                    word_2="$(a $b)"
                    if [ X"${DEBUG}" == X"1" ]; then
                        echo "DEBUG MODE"
                        echo "ID: ${word_1} => Value: ${word_2}"
                    else
                        sed -i -e "s%\(\$wb\[${word_1}\] = \).*%\1${word_2};%g" "${i}"
                    fi
                    IFS="#"
                done
                IFS="$IFS1"
                if [ X"${DEBUG}" == X"1" ]; then
                    break
                else
                    :
                fi
            done
            IFS="$IFS1"
            if [ X"${DEBUG}" == X"1" ]; then
                echo "display IDs and substitution strings completed."
            else
                echo "Replacing strings in all files ${translate_lang}*.lng completed."
                echo "Good luck in translation!"
            fi
            exit 0
        else
            echo "First you need to make a template for a replacement."
            echo "To do this, run the command:"
            echo "${ROOTDIR}/replacer.sh prepare"
            echo "Or create the template manually"
            pars_help
            echo "And save under the name ${name_template}"
            echo "in the directory ${ROOTDIR}/"
            exit 1
        fi
    }
    ###################################################################################
    restore_file()
    {
        clear
        if [ -e "${ROOTDIR}/bacup_ok"  ] ; then
            echo "Restore the original state of all translation files ${translate_lang}*.lng"
            cp -r ${ROOTDIR}/usr/local/ispconfig/interface ${path_to_translate}
            echo "Completed."
            exit 0
        else
            echo "First you need to backup the files language."
            echo "To do this, run the command:"
            echo "${ROOTDIR}/replacer.sh prepare"
            exit 1
        fi
    }
    ###################################################################################
    pars_help()
    {
        echo "The parsing of the template, for example:"
        echo "'Website quota (Harddisk)'~\"Website quota (Harddisk)\"#"
        echo "|          IDs           | | String for replace     |"
        echo " \"~\" - the separator between ID and String for replace"
        echo " \"#\" - the marker of the end of the row"
    }
    ###################################################################################
    case "$1" in
        replace)
            replace_str
            ;;
        prepare)
            prepare_file
            ;;
        restore)
            restore_file
            ;;
        *)    echo "Usage: ${ROOTDIR}/replacer.sh {prepare|replace|restore}"
            pars_help
            exit 2
            ;;
    esac
    exit 0
    
     

    Attached Files:

    Last edited: Jan 20, 2017
    till likes this.
  2. SinglWolf

    SinglWolf New Member

    Who downloaded the previous version of the script, don't use it. There was an error. Please download the current version. Fixed and modified. Sorry.:rolleyes:
    A detailed description and a fresh version on my website. Use the Google translator. If something is unclear, just ask, always answer.;)
     
    Last edited: Jan 19, 2017

Share This Page