-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgenerate_language.sh
More file actions
executable file
·158 lines (132 loc) · 4.53 KB
/
Copy pathgenerate_language.sh
File metadata and controls
executable file
·158 lines (132 loc) · 4.53 KB
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
154
155
156
157
158
#!/bin/bash
# Flutter Localization Helper Script
# This script helps manage language files for the Flutter app
# Colors for pretty output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}=======================================${NC}"
echo -e "${BLUE} Flutter Localization Helper Tool ${NC}"
echo -e "${BLUE}=======================================${NC}"
BASE_ARB_DIR="lib/l10n/arb"
BASE_ARB_FILE="${BASE_ARB_DIR}/intl_en.arb"
# Check if flutter is installed
if ! command -v flutter &> /dev/null; then
echo -e "${RED}Error: Flutter command not found.${NC}"
exit 1
fi
# Function to generate localization files
generate_localization() {
echo -e "${YELLOW}Generating localization files...${NC}"
flutter gen-l10n
if [ $? -eq 0 ]; then
echo -e "${GREEN}Localization files generated successfully!${NC}"
else
echo -e "${RED}Failed to generate localization files!${NC}"
exit 1
fi
}
# Function to list all supported languages
list_languages() {
echo -e "${YELLOW}Currently supported languages:${NC}"
# Find all ARB files and extract language codes
for file in ${BASE_ARB_DIR}/intl_*.arb; do
lang_code=$(basename "$file" | sed 's/intl_\(.*\)\.arb/\1/')
# Get language name from the ARB file if possible
if [ -f "$file" ]; then
lang_name=$(grep -o '"@@locale": "[^"]*"' "$file" | cut -d'"' -f4)
if [ -z "$lang_name" ]; then
lang_name=$lang_code
fi
echo -e " - ${GREEN}$lang_code${NC} ($lang_name)"
fi
done
}
# Function to create a new language file
create_language() {
if [ -z "$1" ]; then
echo -e "${RED}Error: No language code provided.${NC}"
echo -e "Usage: $0 add-language <language_code>"
exit 1
fi
lang_code=$1
new_arb_file="${BASE_ARB_DIR}/intl_${lang_code}.arb"
if [ -f "$new_arb_file" ]; then
echo -e "${RED}Error: Language file for '${lang_code}' already exists.${NC}"
exit 1
fi
if [ ! -f "$BASE_ARB_FILE" ]; then
echo -e "${RED}Error: Base language file (${BASE_ARB_FILE}) not found.${NC}"
exit 1
fi
# Copy the base language file and update the locale
cp "$BASE_ARB_FILE" "$new_arb_file"
# Update the locale in the new file
sed -i '' "s/\"@@locale\": \"en\"/\"@@locale\": \"${lang_code}\"/" "$new_arb_file"
echo -e "${GREEN}Created new language file: ${new_arb_file}${NC}"
echo -e "${YELLOW}Please translate the strings in the new file.${NC}"
}
# Function to check for missing translations
check_missing() {
echo -e "${YELLOW}Checking for missing translations...${NC}"
if [ ! -f "$BASE_ARB_FILE" ]; then
echo -e "${RED}Error: Base language file (${BASE_ARB_FILE}) not found.${NC}"
exit 1
fi
# Get all keys from base file
base_keys=$(grep -o '"[^"]*": "[^"]*"' "$BASE_ARB_FILE" | grep -v "@@" | cut -d'"' -f2)
# Check each language file
for file in ${BASE_ARB_DIR}/intl_*.arb; do
if [ "$file" != "$BASE_ARB_FILE" ]; then
lang_code=$(basename "$file" | sed 's/intl_\(.*\)\.arb/\1/')
echo -e "\nChecking ${BLUE}${lang_code}${NC}:"
missing=0
for key in $base_keys; do
if ! grep -q "\"$key\":" "$file"; then
echo -e " - Missing: ${RED}$key${NC}"
missing=$((missing + 1))
fi
done
if [ $missing -eq 0 ]; then
echo -e " ${GREEN}✓ All translations present${NC}"
else
echo -e " ${RED}$missing translation(s) missing${NC}"
fi
fi
done
}
# Function to get usage information
usage() {
echo -e "Usage: $0 <command>"
echo -e "\nCommands:"
echo -e " ${GREEN}generate${NC} - Generate localization files"
echo -e " ${GREEN}list${NC} - List supported languages"
echo -e " ${GREEN}add${NC} <code> - Add a new language (e.g., 'add fr' for French)"
echo -e " ${GREEN}check${NC} - Check for missing translations"
echo -e " ${GREEN}help${NC} - Show this help message"
}
# Main command parsing
case "$1" in
generate)
generate_localization
;;
list)
list_languages
;;
add)
create_language "$2"
;;
check)
check_missing
;;
help)
usage
;;
*)
usage
exit 1
;;
esac
exit 0