You need to sign in or sign up before continuing.
Newer
Older
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
#!/usr/bin/env bash
# Copyright (C) 2021 Leipzig University Library
#
# 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 <http://www.gnu.org/licenses/>.
#
# @author Robert Lange <lange@ub.uni-leipzig.de>
# @license https://opensource.org/licenses/GPL-3.0 GNU GPLv3
# this script merges token files and removes unused / duplicate tokens with parent and target translation file *.ini
# assumes You are in projects base dir
CACHE_FILE=$1; INSTANCE_FILE=$2; PARENT_FILE=$3;
HAS_ERROR=0;
if [ -z "$CACHE_FILE" ] || [ -z "$INSTANCE_FILE" ]; then
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
HAS_ERROR=1
echo "Usage: $0 path/to/language/with/new/tokens path/to/language/file/of/instance [path/to/parent/file/of/instance]"
echo "example command 1: \"devops/i18n-merge.sh data/i18n/languages/de.ini de_zi4/languages/de.ini local/languages/de.ini\"";
else
if [ ! -f "$CACHE_FILE" ]
then
echo "Language File "$CACHE_FILE" does not exist on your filesystem."; HAS_ERROR=1;
fi
if [ ! -f "$INSTANCE_FILE" ]
then
echo "Language File "$INSTANCE_FILE" does not exist on your filesystem."; HAS_ERROR=1;
fi
fi
if [ -z "$CACHE_FILE" ]; then
if [ ! -f "$PARENT_FILE" ]; then
echo "Parent Language File "$PARENT_FILE" does not exist on your filesystem.";
HAS_ERROR=1;
fi
fi
if [ $HAS_ERROR = 1 ]
then
#echo "exit 1: aborting"
exit 1;
fi
echo "Read new tokens from $CACHE_FILE."
declare -A newTokens
declare -a newTokensOrder
while IFS= read -r line || [ -n "$line" ] # handle possible missing newline in last line
do
key=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,0,st-1)}')
value=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+1)}')
newTokens["$key"]="$value"
newTokensOrder+=("$key");
done < $CACHE_FILE
echo "Reading and minify existing tokens from $INSTANCE_FILE"
declare -A instanceTokens
declare -A instanceComments
declare -a instanceTokensOrder
i=0
while IFS= read -r line || [ -n "$line" ]; # handle possible missing newline in last line
do
if [ -z "$line" ]; then
#echo "empty"
instanceTokensOrder+=("X_empty_X");
elif [[ ${line:0:1} == "#" ]] || [[ ${line:0:1} == ";" ]]; then
#echo "comment"
instanceTokensOrder+=("X_comment_$i")
instanceComments["X_comment_$i"]="$line"
elif [[ ${line:0:11} == "@parent_ini" ]]; then
#echo "relative path to parent ini"
instanceTokensOrder+=("X_comment_$i")
instanceComments["X_comment_$i"]="$line"
#PARENT_FILE=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+3)} ' | sed 's/.$//')
#PARENT_FILE=$(echo "${PARENT_FILE#@(.)}")
#PARENT_FILE="${INSTANCE_FILE}/${PARENT_FILE}"
#echo "$PARENT_FILE"
else
key=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,0,st-1)}')
value=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+1)}')
#echo "$line"
# only add to instanceTokensOrder when key entry is NOT already existing -> remove duplicates / ignored by letting latest entry win
if [[ -n "${instanceTokens["$key"]}" ]]; then
echo "* remove duplicate token \"${key}\" with obsolete value ${instanceTokens["$key"]} by $value."
else
instanceTokensOrder+=("$key");
fi
instanceTokens["$key"]="$value";
fi
((i=i+1))
done < $INSTANCE_FILE
if [ -f "$PARENT_FILE" ]; then
echo "Reading and minifying existing tokens from $PARENT_FILE..."
declare -A parentTokens
declare -a parentTokensOrder
while IFS= read -r line || [ -n "$line" ]
do
if [ -z "$line" ] || [[ ${line:0:1} == "#" ]] || [[ ${line:0:1} == ";" ]] || [[ ${line:0:11} == "@parent_ini" ]]; then
continue
else
key=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,0,st-1)}')
value=$(echo "$line" | awk -F: '{ st = index($0,"=");print substr($0,st+1)}')
#echo "$line"
# only add to instanceTokensOrder when key entry is NOT already existing -> remove duplicates / ignored by letting latest entry win
if [[ ${instanceTokens["$key"]} == "$value" ]] ; then
#instanceTokens["$key"]="$value";
echo "* remove duplicate token \"${key}\" and $value with parent file."
instanceTokens["$key"]="X_duplicate_X";
fi
fi
done < $PARENT_FILE
fi
echo "Adding new token translations..."
for i in "${!newTokensOrder[@]}"
do
if [[ -n "${instanceTokens[${newTokensOrder[$i]}]}" ]]; then
echo "* replace token \"${newTokensOrder[$i]}\" with old value ${instanceTokens[${newTokensOrder[$i]}]} by new value $value"
else
instanceTokensOrder+=("${newTokensOrder[$i]}");
echo "* add new token \"${newTokensOrder[$i]}\" with value ${newTokens[${newTokensOrder[$i]}]}"
fi
instanceTokens["${newTokensOrder[$i]}"]="${newTokens[${newTokensOrder[$i]}]}";
done
echo "4) Writing merged translations to instance file..."
true > "${INSTANCE_FILE}"
for i in "${!instanceTokensOrder[@]}"
do
key="${instanceTokensOrder[$i]}"
if [[ "$key" == "X_empty_X" ]]; then
#echo "empty"
echo -en '\n' >> "${INSTANCE_FILE}"
elif [[ ${key:0:10} == "X_comment_" ]]; then
#echo "comment"
echo "${instanceComments[${key}]}" >> "${INSTANCE_FILE}"
elif [[ "${instanceTokens[${key}]}" != "X_duplicate_X" ]]; then
#echo "$key"
echo "${key}=${instanceTokens[${key}]}" >> "${INSTANCE_FILE}"
fi
done