diff --git a/README.md b/README.md index ebbfeae..526d407 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # thea2Shattering_i18n_tools -Scripting tools to interact fir Thea 2 The Shattering files in order to translate them easily. \ No newline at end of file +Scripting tools to interact with Thea 2 The Shattering's files in order to translate them easily. \ No newline at end of file diff --git a/extract_module.sh b/extract_module.sh index 503c8a0..6a8d0ca 100755 --- a/extract_module.sh +++ b/extract_module.sh @@ -4,16 +4,22 @@ # then pass the .pot in poedit to merge duplication if [[ "$#" -ne 1 ]]; then - echo "Please passe exactly 1 parameter: the txt module" + echo "Please pass exactly 1 parameter: the txt module" exit 1 fi -ORIGINAL_TXT=$1 -POT="${ORIGINAL_TXT%%.txt}.pot" +ORIGINAL_TXT="$1" +POT_DIR="$(pwd)/Modules/pot/" +POT=$(basename "${ORIGINAL_TXT%%.txt}.pot") +POT="${POT_DIR}/${POT}" # Reinit exiting POT -echo > "${POT}" +mkdir -p "${POT_DIR}" +truncate -s 0 "${POT}" + +# First thing first, let's anonymize the pot and delete all email references +sed -i 's/[A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,6\}//' "${POT}" comment_event="" comment_node="" diff --git a/rollback_database.sh b/rollback_database.sh index 33823d7..fccaf62 100755 --- a/rollback_database.sh +++ b/rollback_database.sh @@ -4,24 +4,33 @@ # grep Key DATABASE_UI_LOCALIZATION.xml | awk -F'"' '{print "#. "$2"\n" "msgid " "\""$4"\"" "\n" "msgstr " "\"\""}' > DATABASE_UI_LOCALIZATION.pot # then pass the .pot in poedit to merge duplication and add the Zanata header -if [[ "$#" -ne 2 ]]; then - echo "Please passe exactly 2 parameters: the .xml and the corresponding .po" +if [[ "$#" -ne 3 ]]; then + echo "Please pass exactly 3 parameters: the .xml, the corresponding .po and the language" exit 1 fi -ORIGINAL_XML=$1 -FR_XML="${ORIGINAL_XML%%.xml}.fr.xml" -ORIGINAL_PO=$2 -TEMP_PO="/tmp/passage.temp" -TEMP_MSGSTR="/tmp/msgstr.temp" -TEMP_MSGID="/tmp/msgid.temp" +ORIGINAL_XML="$1" +LANGUAGE="$3" +FINAL_DIR="$(pwd)/Translation/${LANGUAGE}/game_files/Databases/" +TRANS_XML=$(basename "${ORIGINAL_XML}") +TRANS_XML="${FINAL_DIR}/${TRANS_XML}" +ORIGINAL_PO="$2" +WORKDIR="/tmp/thea2/${LANGUAGE}" +TEMP_PO="/${WORKDIR}/passage.temp" +TEMP_MSGSTR="/${WORKDIR}/msgstr.temp" +TEMP_MSGID="/${WORKDIR}/msgid.temp" -#Let's work on a FR xml: -cp ${ORIGINAL_XML} ${FR_XML} +#Let's work on the translated xml: +mkdir -p "${FINAL_DIR}" +mkdir -p "${WORKDIR}" +cp "${ORIGINAL_XML}" "${TRANS_XML}" + +# First thing first, let's anonymize the pot and delete all email references +sed -i 's/[A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,6\}//' "${ORIGINAL_PO}" function insert_to_xml { - sed -i "s~\(Key=\"${key}\" Val=\)\".*\"\(.*\)~\1\"${translation}\"\2~g" ${FR_XML} + sed -i "s~\(Key=\"${key}\" Val=\)\".*\"\(.*\)~\1\"${translation}\"\2~g" ${TRANS_XML} } @@ -29,7 +38,7 @@ for key in $(grep Key ${ORIGINAL_XML} | awk -F'"' '{print $2}'); do msgstr="" msgid="" translation="" - # We search for the exact key in the po file untile we get a blank line, to get the full msgstr and the msgid + # We search for the exact key in the po file until we get a blank line, to get the full msgstr and the msgid awk -v key="${key}" '{pat="#. "key"$"} !NF{s=0}s;$0 ~ pat{ print $0; s=1 }' ${ORIGINAL_PO} > ${TEMP_PO} # Extract msgstr and merge all lines into one diff --git a/rollback_module.sh b/rollback_module.sh index 8be4dbc..79aef40 100755 --- a/rollback_module.sh +++ b/rollback_module.sh @@ -5,22 +5,142 @@ # then pass the .pot in poedit to merge duplication and add the Zanata header if [[ "$#" -ne 2 ]]; then - echo "Please passe exactly 2 parameters: the .xml and the corresponding .po" + echo "Please pass exactly 2 parameters: the .po and the language" exit 1 fi -LANGUAGE=${2} +LANGUAGE="${2}" WORKDIR="/tmp/thea2/${LANGUAGE}" -PO=${1} -TXT="${PO%%.po}.txt" -FINALDIR="$(pwd)/${LANGUAGE}" +PO="${1}" +FINALDIR="$(pwd)/Translation/${LANGUAGE}/game_files/Modules/" +TXT=$(basename "${PO%%.po}.txt") +TXT="${FINALDIR}/${TXT}" -mkdir -p ${WORKDIR} -mkdir -p ${FINALDIR} -readarray -t file < ${PO} +# Initialization +mkdir -p "${WORKDIR}" +mkdir -p "${FINALDIR}" +truncate -s 0 "${TXT}" +readarray -t FILE < ${PO} +declare -A eventArray=() +declare -A translationArray=() -for line in "${file[@]}"; do - echo $line -done \ No newline at end of file +# First thing first, let's anonymize the po and delete all email references +sed -i 's/[A-Za-z0-9._%+-]\+@[A-Za-z0-9.-]\+\.[A-Za-z]\{2,6\}//' "${PO}" + +function extract_translation { + key="${line}" + # We need to escape special char for regex: [ + and ( + key="${key//[/\\\\[}" + key="${key//+/\\\\\+}" + key="${key//(/\\\\\(}" + # We search for the exact key in the po file until we get a blank line, to get the full msgstr and the msgid + tempPO=$(awk -v key="${key}" '{pat="#. "key""} !NF{s=0}s;$0 ~ pat{ print $0; s=1 }' ${PO}) + # Extract msgstr and merge all lines into one + tempMsgstr=$(awk '!NF{s=0}s;/msgstr/{ print $0; s=1 }' <<< "${tempPO}" | sed -e 's/^"//' -e 's/"$//' -e 's/^msgstr "//') + msgstr=$(awk 'NR{printf "%s",$0;next;}1' <<< ${tempMsgstr}) + + # Extract msgid and merge all lines into one + tempMsgid="$(awk '/msgid/{ s=1 }s;/msgstr/{ s=0 }' <<< \"${tempPO}\" |grep -v "msgstr" | sed -e 's/^"//' -e 's/"$//' -e 's/^msgid "//')" + msgid="$(awk 'NR{printf "%s",$0;next;}1' <<< ${tempMsgid})" + + # Escape special char causing problem in with sed and xml + msgstr=${msgstr//'\n'/'\\n'} + msgstr=${msgstr//'\"'/"""} + msgstr=${msgstr//'&'/'\&'} + msgid=${msgid//'\n'/'\\n'} + msgid=${msgid//'\"'/"""} + msgid=${msgid//'&'/'\&'} + + if [[ "${msgstr}" == "" ]]; then + # if the msgstr is empty, then it's not translated yet, we use original string + translation=${msgid} + else + translation=${msgstr} + fi + + echo ${translation} +} + +function rebuild_txt { + ## We need to recurse on all event, then getting all node and their content for each event ## + + # In order to recurse properly, we need to sort the translationArray + # To sort we use a new sorted array which contains all keys sorted following "Version style" order + # eg: 0.2.story is before 0.2.1 and 0.2.1 is before 0.11.1 + sorted=() + while IFS= read -rd '' key; do + sorted+=( "$key" ) + done < <(printf '%s\0' "${!translationArray[@]}" | sort -zV) + + currentEvent=0 + currentNode="" + for index in "${!eventArray[@]}"; do + # If currentEvent is different than our index, then we just changed event and need to close the precedent + # We also closed the next event in line + if [[ ${currentEvent} != ${index} ]]; then + echo "[/NODE]" >> ${TXT} + echo >> ${TXT} + echo "[/EVENT]" >> ${TXT} + echo >> ${TXT} + fi + echo "${eventArray[${index}]}" >> ${TXT} + for key in "${sorted[@]}"; do + # We work only on key related to our current event + if [[ "${key}" == "${index}"* ]]; then + eventID="${index}" + nodeID="$(awk -F'.' '{print $2}' <<< ${key})" + type="$(awk -F'.' '{print $3}' <<< ${key})" + # If nodeID is different than currentNodeID, then we just changed the node and need to close the node + # We do this only when we are in the same event and not for the first iteration + if [[ "${currentNodeID}" != "${nodeID}" && ${currentEvent} == ${index} && "${currentNodeID}" != "" ]]; then + echo "[/NODE]" >> ${TXT} + echo >> ${TXT} + fi + # Here type is a story or an out + if [[ "${type}" == "story" ]]; then + echo "+[NODE]${nodeID}" >> ${TXT} + echo "[STORY]" >> ${TXT} + echo "${translationArray[${key}]}" >> ${TXT} + echo "[/STORY]" >> ${TXT} + currentNodeID=${nodeID} + currentEvent=${index} + else + echo "[OUT]${translationArray[${key}]}" >> ${TXT} + fi + fi + done + done + # At the last of last event, we go outside the loop so we need to close remaining node and event + echo "[/NODE]" >> ${TXT} + echo >> ${TXT} + echo "[/EVENT]" >> ${TXT} + echo >> ${TXT} +} + +for index in "${!FILE[@]}"; do + line="${FILE[${index}]}" + line=${line##\#. } + # If the line contains an event, we need to init variables for following compute + if [[ ${line} == *"[EVENT]"* ]]; then + event="$(awk -F'@@' '{print $1}' <<< ${line})" + eventID="$(awk -F'(' '{print $2}' <<< ${event})" + eventID="${eventID%%\)}" + node="$(awk -F'@@' '{print $2}' <<< ${line})" + nodeID="$(awk -F']' '{print $2}' <<< ${node})" + eventArray[${eventID}]="${event}" + fi + if [[ ${line} == *"[STORY]"* ]]; then + translation=$(extract_translation "${line}") + translationArray["${eventID}.${nodeID}.story"]="${translation}" + elif [[ ${line} == *"[OUT]"* ]]; then + out="$(awk -F'@@' '{print $3}' <<< ${line})" + outID=$(awk -F']' '{print $2}' <<< ${out}) + translation=$(extract_translation "${line}") + translationArray["${eventID}.${nodeID}.${outID}"]="${translation}" + fi +done + +# Now we have 2 arrays: one with all event name, and one with all translation. We can rebuild the original file +rebuild_txt \ No newline at end of file