Skip to content

Commit 8fece5d

Browse files
committed
chore: added script to compare translations
1 parent 6ef6dfe commit 8fece5d

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

scripts/compare-translations.sh

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/bin/bash
2+
#
3+
# This script compares a language translation file with the default English translation
4+
# and reports missing and obsolete translation keys.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public License,
7+
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
# obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# @package phpMyFAQ
11+
# @author Thorsten Rinne <thorsten@phpmyfaq.de>
12+
# @copyright 2025 phpMyFAQ Team
13+
# @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
# @link https://www.phpmyfaq.de
15+
# @since 2025-12-25
16+
17+
# Colors for output
18+
RED='\033[0;31m'
19+
GREEN='\033[0;32m'
20+
YELLOW='\033[1;33m'
21+
BLUE='\033[0;34m'
22+
NC='\033[0m' # No Color
23+
24+
# Function to display usage
25+
usage() {
26+
echo "Usage: $0 <language_code>"
27+
echo ""
28+
echo "Examples:"
29+
echo " $0 pt_br # Compare Portuguese (Brazil) with English"
30+
echo " $0 de # Compare German with English"
31+
echo " $0 fr # Compare French with English"
32+
echo ""
33+
echo "The script will compare the translation file language_<code>.php"
34+
echo "with the default English translation (language_en.php)"
35+
exit 1
36+
}
37+
38+
# Check if language code is provided
39+
if [ -z "$1" ]; then
40+
echo -e "${RED}Error: Language code is required${NC}"
41+
echo ""
42+
usage
43+
fi
44+
45+
LANG_CODE="$1"
46+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
47+
TRANSLATIONS_DIR="${SCRIPT_DIR}/../phpmyfaq/translations"
48+
ENGLISH_FILE="${TRANSLATIONS_DIR}/language_en.php"
49+
LANGUAGE_FILE="${TRANSLATIONS_DIR}/language_${LANG_CODE}.php"
50+
51+
# Check if English file exists
52+
if [ ! -f "$ENGLISH_FILE" ]; then
53+
echo -e "${RED}Error: English translation file not found at: ${ENGLISH_FILE}${NC}"
54+
exit 1
55+
fi
56+
57+
# Check if target language file exists
58+
if [ ! -f "$LANGUAGE_FILE" ]; then
59+
echo -e "${RED}Error: Translation file for '${LANG_CODE}' not found at: ${LANGUAGE_FILE}${NC}"
60+
exit 1
61+
fi
62+
63+
# Create temporary files for storing keys
64+
TEMP_DIR=$(mktemp -d)
65+
EN_KEYS="${TEMP_DIR}/en_keys.txt"
66+
LANG_KEYS="${TEMP_DIR}/lang_keys.txt"
67+
MISSING_KEYS="${TEMP_DIR}/missing_keys.txt"
68+
OBSOLETE_KEYS="${TEMP_DIR}/obsolete_keys.txt"
69+
70+
# Extract keys from English file (handle both single and double quotes)
71+
grep -E "\\\$PMF_LANG\[" "$ENGLISH_FILE" | sed -E "s/.*\\\$PMF_LANG\[['\"]([^'\"]+)['\"]\].*/\1/" | sort -u > "$EN_KEYS"
72+
73+
# Extract keys from target language file (handle both single and double quotes)
74+
grep -E "\\\$PMF_LANG\[" "$LANGUAGE_FILE" | sed -E "s/.*\\\$PMF_LANG\[['\"]([^'\"]+)['\"]\].*/\1/" | sort -u > "$LANG_KEYS"
75+
76+
# Find missing keys (in English but not in target language)
77+
comm -23 "$EN_KEYS" "$LANG_KEYS" > "$MISSING_KEYS"
78+
79+
# Find obsolete keys (in target language but not in English)
80+
comm -13 "$EN_KEYS" "$LANG_KEYS" > "$OBSOLETE_KEYS"
81+
82+
# Count keys
83+
EN_COUNT=$(wc -l < "$EN_KEYS" | tr -d ' ')
84+
LANG_COUNT=$(wc -l < "$LANG_KEYS" | tr -d ' ')
85+
MISSING_COUNT=$(wc -l < "$MISSING_KEYS" | tr -d ' ')
86+
OBSOLETE_COUNT=$(wc -l < "$OBSOLETE_KEYS" | tr -d ' ')
87+
88+
# Display header
89+
echo ""
90+
echo -e "${BLUE}=================================${NC}"
91+
echo -e "${BLUE}Translation Comparison Report${NC}"
92+
echo -e "${BLUE}=================================${NC}"
93+
echo ""
94+
echo -e "Language: ${YELLOW}${LANG_CODE}${NC}"
95+
echo -e "English file: ${ENGLISH_FILE}"
96+
echo -e "Language file: ${LANGUAGE_FILE}"
97+
echo ""
98+
99+
# Display statistics
100+
echo -e "${BLUE}Statistics:${NC}"
101+
echo -e " English keys: ${EN_COUNT}"
102+
echo -e " Language keys: ${LANG_COUNT}"
103+
echo -e " ${GREEN}Missing keys: ${MISSING_COUNT}${NC}"
104+
echo -e " ${YELLOW}Obsolete keys: ${OBSOLETE_COUNT}${NC}"
105+
echo ""
106+
107+
# Calculate completion percentage
108+
if [ "$EN_COUNT" -gt 0 ]; then
109+
COMPLETION=$((($EN_COUNT - $MISSING_COUNT) * 100 / $EN_COUNT))
110+
echo -e "Translation completion: ${GREEN}${COMPLETION}%${NC}"
111+
echo ""
112+
fi
113+
114+
# Display missing keys
115+
if [ "$MISSING_COUNT" -gt 0 ]; then
116+
echo -e "${GREEN}Missing Keys (in English but not in ${LANG_CODE}):${NC}"
117+
echo -e "${GREEN}=================================================${NC}"
118+
cat "$MISSING_KEYS" | while read key; do
119+
echo -e " ${RED}${NC} $key"
120+
done
121+
echo ""
122+
else
123+
echo -e "${GREEN}✓ No missing keys! All English keys are translated.${NC}"
124+
echo ""
125+
fi
126+
127+
# Display obsolete keys
128+
if [ "$OBSOLETE_COUNT" -gt 0 ]; then
129+
echo -e "${YELLOW}Obsolete Keys (in ${LANG_CODE} but not in English):${NC}"
130+
echo -e "${YELLOW}==================================================${NC}"
131+
cat "$OBSOLETE_KEYS" | while read key; do
132+
echo -e " ${YELLOW}${NC} $key"
133+
done
134+
echo ""
135+
echo -e "${YELLOW}Note: These keys may be deprecated and can be removed.${NC}"
136+
echo ""
137+
else
138+
echo -e "${GREEN}✓ No obsolete keys! The translation is clean.${NC}"
139+
echo ""
140+
fi
141+
142+
# Display summary
143+
echo -e "${BLUE}=================================${NC}"
144+
if [ "$MISSING_COUNT" -eq 0 ] && [ "$OBSOLETE_COUNT" -eq 0 ]; then
145+
echo -e "${GREEN}✓ Translation is perfectly synchronized with English!${NC}"
146+
elif [ "$MISSING_COUNT" -eq 0 ]; then
147+
echo -e "${YELLOW}⚠ Translation is complete but has ${OBSOLETE_COUNT} obsolete keys.${NC}"
148+
elif [ "$OBSOLETE_COUNT" -eq 0 ]; then
149+
echo -e "${RED}✗ Translation is missing ${MISSING_COUNT} keys.${NC}"
150+
else
151+
echo -e "${RED}✗ Translation needs attention: ${MISSING_COUNT} missing, ${OBSOLETE_COUNT} obsolete.${NC}"
152+
fi
153+
echo -e "${BLUE}=================================${NC}"
154+
echo ""
155+
156+
# Cleanup temporary files
157+
rm -rf "$TEMP_DIR"
158+
159+
# Exit with appropriate code
160+
if [ "$MISSING_COUNT" -gt 0 ] || [ "$OBSOLETE_COUNT" -gt 0 ]; then
161+
exit 1
162+
else
163+
exit 0
164+
fi

0 commit comments

Comments
 (0)