Skip to content

Commit 3fb9b85

Browse files
Add Multi-language Support and Refactor Installation and Control Scripts (#20)
1 parent 1ba50ce commit 3fb9b85

File tree

6 files changed

+642
-208
lines changed

6 files changed

+642
-208
lines changed

1pctl

+133-111
Original file line numberDiff line numberDiff line change
@@ -10,123 +10,185 @@ ORIGINAL_ENTRANCE=entrance
1010
ORIGINAL_USERNAME=username
1111
ORIGINAL_PASSWORD=password
1212

13+
# Language selection setup
14+
LANG_FILE=".selected_language"
15+
LANG_DIR="./lang"
16+
AVAILABLE_LANGS=("en" "fa" "zh")
17+
18+
# Associative array for language names
19+
declare -A LANG_NAMES
20+
LANG_NAMES=( ["en"]="English" ["fa"]="Persian" ["zh"]="Chinese" )
21+
22+
# Check if the language is already selected and saved
23+
if [ -f "$LANG_FILE" ]; then
24+
# Load the saved language
25+
selected_lang=$(cat "$LANG_FILE")
26+
echo "${LANG_ALREADY_SELECTED_MSG} ${LANG_NAMES[$selected_lang]}"
27+
else
28+
# Prompt the user to select a language
29+
echo "$LANG_PROMPT_MSG"
30+
for i in "${!AVAILABLE_LANGS[@]}"; do
31+
lang_code="${AVAILABLE_LANGS[i]}"
32+
echo "$((i + 1)). ${LANG_NAMES[$lang_code]}"
33+
done
34+
35+
read -p "$LANG_CHOICE_MSG" lang_choice
36+
37+
if [[ $lang_choice -ge 1 && $lang_choice -le ${#AVAILABLE_LANGS[@]} ]]; then
38+
selected_lang=${AVAILABLE_LANGS[$((lang_choice - 1))]}
39+
echo "${LANG_SELECTED_CONFIRM_MSG} ${LANG_NAMES[$selected_lang]}"
40+
41+
# Save the selected language to the file
42+
echo $selected_lang > $LANG_FILE
43+
else
44+
echo "$LANG_INVALID_MSG"
45+
selected_lang="en"
46+
echo $selected_lang > $LANG_FILE
47+
fi
48+
fi
49+
50+
# Load the selected language file
51+
LANGFILE="$LANG_DIR/$selected_lang.sh"
52+
53+
if [ -f "$LANGFILE" ]; then
54+
source "$LANGFILE"
55+
else
56+
echo -e "${RED} $LANG_NOT_FOUND_MSG $LANGFILE${NC}"
57+
exit 1
58+
fi
59+
60+
# Define functions for actions
1361
function usage() {
14-
echo "1Panel 控制脚本"
62+
echo "$PANEL_CONTROL_SCRIPT"
1563
echo
1664
echo "Usage: "
1765
echo " ./1pctl [COMMAND] [ARGS...]"
1866
echo " ./1pctl --help"
1967
echo
2068
echo "Commands: "
21-
echo " status 查看 1Panel 服务运行状态"
22-
echo " start 启动 1Panel 服务"
23-
echo " stop 停止 1Panel 服务"
24-
echo " restart 重启 1Panel 服务"
25-
echo " uninstall 卸载 1Panel 服务"
26-
echo " user-info 获取 1Panel 用户信息"
27-
echo " listen-ip 切换 1Panel 监听 IP"
28-
echo " version 查看 1Panel 版本信息"
29-
echo " update 修改 1Panel 系统信息"
30-
echo " reset 重置 1Panel 系统信息"
31-
echo " restore 恢复 1Panel 服务及数据"
69+
echo " status $PANEL_SERVICE_STATUS"
70+
echo " start $PANEL_SERVICE_START"
71+
echo " stop $PANEL_SERVICE_STOP"
72+
echo " restart $PANEL_SERVICE_RESTART"
73+
echo " uninstall $PANEL_SERVICE_UNINSTALL"
74+
echo " user-info $PANEL_SERVICE_USER_INFO"
75+
echo " listen-ip $PANEL_SERVICE_LISTEN_IP"
76+
echo " version $PANEL_SERVICE_VERSION"
77+
echo " update $PANEL_SERVICE_UPDATE"
78+
echo " reset $PANEL_SERVICE_RESET"
79+
echo " restore $PANEL_SERVICE_RESTORE"
3280
}
81+
3382
function status() {
3483
systemctl status 1panel.service
3584
}
85+
3686
function start() {
3787
systemctl start 1panel.service
3888
status
3989
}
90+
4091
function stop() {
4192
systemctl stop 1panel.service
4293
status
4394
}
95+
4496
function restart() {
4597
systemctl restart 1panel.service
4698
status
4799
}
100+
48101
function uninstall() {
49-
read -p "卸载将会完全清除 1Panel 服务和数据目录,是否继续 [y/n] : " yn
102+
read -p "$PANEL_SERVICE_UNINSTALL_NOTICE : " yn
50103
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
51-
echo -e "================== 开始卸载 1Panel Linux 服务器运维管理面板 =================="
52-
echo -e ""
53-
echo -e "1) 停止 1Panel 服务进程..."
104+
echo -e "$PANEL_SERVICE_UNINSTALL_START"
54105
systemctl stop 1panel.service
55106
systemctl disable 1panel.service >/dev/null 2>&1
107+
echo -e "$PANEL_SERVICE_UNINSTALL_REMOVE"
108+
rm -rf $BASE_DIR/1panel /usr/local/bin/{1pctl,1panel} /etc/systemd/system/1panel.service
109+
systemctl daemon-reload
110+
systemctl reset-failed
111+
echo -e "$PANEL_SERVICE_UNINSTALL_SUCCESS"
56112
else
57113
exit 0
58114
fi
59-
60-
echo -e "2) 删除 1Panel 服务和数据目录..."
61-
rm -rf $BASE_DIR/1panel /usr/local/bin/{1pctl,1panel} /etc/systemd/system/1panel.service
62-
63-
echo -e "3) 重新加载服务配置文件..."
64-
systemctl daemon-reload
65-
systemctl reset-failed
66-
67-
echo -e ""
68-
echo -e "================================== 卸载完成 =================================="
69115
}
116+
70117
function user-info() {
71118
1panel user-info
72119
}
120+
73121
function listen-ip() {
74-
1panel listen-ip
75-
}
76-
function listen_ipv4() {
77-
1panel listen-ip ipv4
78-
restart
79-
}
80-
function listen_ipv6() {
81-
1panel listen-ip ipv6
82-
restart
122+
case "${target}" in
123+
ipv4)
124+
1panel listen-ip ipv4
125+
restart
126+
;;
127+
ipv6)
128+
1panel listen-ip ipv6
129+
restart
130+
;;
131+
*)
132+
1panel listen-ip
133+
;;
134+
esac
83135
}
136+
84137
function restore() {
85-
read -p "1Panel 将会恢复至上一个稳定版本,是否继续 [y/n] : " yn
138+
read -p "$PANEL_SERVICE_RESTORE_NOTICE : " yn
86139
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
87-
echo -e ""
88140
1panel restore
89141
systemctl daemon-reload
90142
restart
91-
echo -e ""
92143
1panel version
93144
else
94145
exit 0
95146
fi
96147
}
148+
97149
function version() {
98150
1panel version
99151
}
152+
100153
function reset() {
101-
1panel reset
102-
}
103-
function reset_domain() {
104-
1panel reset domain
105-
}
106-
function reset_entrance() {
107-
1panel reset entrance
108-
}
109-
function reset_https() {
110-
1panel reset https
111-
restart
112-
}
113-
function reset_ips() {
114-
1panel reset ips
115-
}
116-
function reset_mfa() {
117-
1panel reset mfa
154+
case "${target}" in
155+
domain)
156+
1panel reset domain
157+
;;
158+
entrance)
159+
1panel reset entrance
160+
;;
161+
https)
162+
1panel reset https
163+
restart
164+
;;
165+
ips)
166+
1panel reset ips
167+
;;
168+
mfa)
169+
1panel reset mfa
170+
;;
171+
*)
172+
1panel reset
173+
;;
174+
esac
118175
}
176+
119177
function update() {
120-
1panel update
121-
}
122-
function update_username() {
123-
1panel update username
124-
}
125-
function update_password() {
126-
1panel update password
127-
}
128-
function update_port() {
129-
1panel update port
178+
case "${target}" in
179+
username)
180+
1panel update username
181+
;;
182+
password)
183+
1panel update password
184+
;;
185+
port)
186+
1panel update port
187+
;;
188+
*)
189+
1panel update
190+
;;
191+
esac
130192
}
131193

132194
function main() {
@@ -153,58 +215,16 @@ function main() {
153215
user-info
154216
;;
155217
listen-ip)
156-
case "${target}" in
157-
ipv4)
158-
listen_ipv4
159-
;;
160-
ipv6)
161-
listen_ipv6
162-
;;
163-
*)
164-
listen-ip
165-
;;
166-
esac
218+
listen-ip
167219
;;
168220
version)
169221
version
170222
;;
171223
reset)
172-
case "${target}" in
173-
domain)
174-
reset_domain
175-
;;
176-
entrance)
177-
reset_entrance
178-
;;
179-
https)
180-
reset_https
181-
;;
182-
ips)
183-
reset_ips
184-
;;
185-
mfa)
186-
reset_mfa
187-
;;
188-
*)
189-
reset
190-
;;
191-
esac
224+
reset
192225
;;
193226
update)
194-
case "${target}" in
195-
username)
196-
update_username
197-
;;
198-
password)
199-
update_password
200-
;;
201-
port)
202-
update_port
203-
;;
204-
*)
205-
update
206-
;;
207-
esac
227+
update
208228
;;
209229
help)
210230
usage
@@ -216,7 +236,9 @@ function main() {
216236
usage
217237
;;
218238
*)
219-
echo "不支持的参数,请使用 help 或 --help 参数获取帮助"
239+
echo "$PANEL_SERVICE_UNSUPPORTED_PARAMETER"
240+
;;
220241
esac
221242
}
243+
222244
main

0 commit comments

Comments
 (0)