Skip to content

Commit

Permalink
feat: 完善多语言支持&优化速度
Browse files Browse the repository at this point in the history
  • Loading branch information
fishros committed Sep 4, 2024
1 parent 1b7f0fa commit dc73c88
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@ class Tool(BaseTool):
- 一键安装nodejs [小鱼](https://github.com/fishros)
- 一键安装vscode [小鱼](https://github.com/fishros)
- 一键安装:Docker(支持amd64和arm64) [@alyssa](https://github.com/alyssa1024)


12 changes: 4 additions & 8 deletions install
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
mkdir -p /tmp/fishinstall/tools/translation/assets
wget http://mirror.fishros.com/install/install.py -O /tmp/fishinstall/install.py 2>>/dev/null
export FISHROS_URL="http://mirror.fishros.com/install"
wget `echo $FISHROS_URL`/install.py -O /tmp/fishinstall/install.py 2>>/dev/null
source /etc/profile
# 强制解锁,太多用户遇到这个问题了,没办法,后续想个办法解决下
# 强解可能会有依赖问题
# sudo rm /var/cache/apt/archives/lock
# sudo rm /var/lib/dpkg/lock
# sudo rm /var/lib/dpkg/lock-frontend
if [ $UID -eq 0 ];then
apt-get install sudo
fi
sudo apt install python3-distro python3-yaml -y
sudo python3 /tmp/fishinstall/install.py
sudo -E python3 /tmp/fishinstall/install.py
sudo rm -rf /tmp/fishinstall/
sudo rm fishros
. ~/.bashrc
. ~/.zshrc 2>/dev/null
. ~/.zshrc 2>/dev/null
30 changes: 7 additions & 23 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
import os
import importlib

url_prefix = 'http://mirror.fishros.com/install/'

url_prefix = os.environ.get('FISHROS_URL')+'/'
base_url = url_prefix+'tools/base.py'

_suported_languages = ['zh_CN', 'en_US']
lang_url = url_prefix+'tools/translation/assets/{}.py'
_translator_files = ['translator']
translator_url = url_prefix+'tools/translation/{}.py'
translator_url = url_prefix+'tools/translation/translator.py'

INSTALL_ROS = 0 # 安装ROS相关
INSTALL_SOFTWARE = 1 # 安装软件
Expand Down Expand Up @@ -65,22 +60,17 @@ def main():
from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask,ChooseWithCategoriesTask
from tools.base import encoding_utf8,osversion,osarch
from tools.base import run_tool_file,download_tools
from tools.base import config_helper
from tools.base import config_helper,tr

# download translations
for files in _translator_files:
os.system("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(translator_url.format(files),translator_url.format(files).replace(url_prefix,'')))

for lang in _suported_languages:
os.system("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(lang_url.format(lang),lang_url.format(lang).replace(url_prefix,'')))
CmdTask("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(translator_url,translator_url.replace(url_prefix,''))).run()
importlib.import_module("tools.translation.translator").Linguist()
from tools.base import tr

# Import translations
tr = importlib.import_module("tools.translation.translator").Linguist()

# PrintUtils.print_delay(f"检测到你的系统版本信息为{osversion.get_codename()},{osarch}",0.001)
# 使用量统计
CmdTask("wget https://fishros.org.cn/forum/topic/1733 -O /tmp/t1733 -q && rm -rf /tmp/t1733").run()

PrintUtils.print_success(tr.tr("已为您切换语言至当前所在国家语言:")+tr._currentLocale)
# check base config
if not encoding_utf8:
print("Your system encoding not support ,will install some packgaes..")
Expand Down Expand Up @@ -122,12 +112,6 @@ def main():
code,result = ChooseWithCategoriesTask(tool_categories, tips=tr.tr("---众多工具,等君来用---"),categories=tools_type_map).run()

if code==0: PrintUtils().print_success(tr.tr("是觉得没有合胃口的菜吗?那快联系的小鱼增加菜单吧~"))
elif code==77:
code,result = ChooseTask(choose_dic, tr.tr("请选择你要测试的程序:")).run()
if code<0 and code>=77: return False
# CmdTask("cp tools/* /tmp/fishinstall/tools/").run
print(tools[code]['tool'].replace(url_prefix,'').replace("/","."))
run_tool_file(tools[code]['tool'].replace(url_prefix,'').replace("/","."))
else:
download_tools(code,tools)
run_tool_file(tools[code]['tool'].replace(url_prefix,'').replace("/","."))
Expand Down
14 changes: 8 additions & 6 deletions tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
encoding = locale.getpreferredencoding()
encoding_utf8 = encoding.find("UTF")>-1

# Import translations
tr = importlib.import_module("tools.translation.translator").Linguist()

tr = None

class ConfigHelper():
def __init__(self,record_file=None):
Expand Down Expand Up @@ -975,12 +975,14 @@ def run_command(self,executable='/bin/sh'):
self.command_thread = threading.Thread(target=self.command_thread)
self.command_thread.start()
time.sleep(0.5) # 等待线程启动
while self.is_command_finish()!=0:
while self.is_command_finish()==-1:
self.bar.update_time()
time.sleep(0.1)

return (self.ret_code,self.ret_out,self.ret_err)

def is_command_finish(self):
# poll 是返回码
if self.sub.poll() == None:
return -1
return self.sub.poll()
Expand Down Expand Up @@ -1411,17 +1413,17 @@ def run_tool_file(file,autorun=True):
return tool

def run_tool_url(url,url_prefix):
os.system("wget {} -O /tmp/fishinstall/tools/{} --no-check-certificate".format(url,url[url.rfind('/')+1:]))
CmdTask("wget {} -O /tmp/fishinstall/tools/{} --no-check-certificate".format(url,url[url.rfind('/')+1:])).run()
run_tool_file(url.replace(url_prefix,'').replace("/","."))

def download_tools(id,tools):
# download tool
url = tools[id]['tool']
os.system("wget {} -O /tmp/fishinstall/tools/{} --no-check-certificate".format(url,url[url.rfind('/')+1:]))
CmdTask("wget {} -O /tmp/fishinstall/tools/{} --no-check-certificate".format(url,url[url.rfind('/')+1:])).run()
# download dep
for dep in tools[id]['dep']:
url = tools[dep]['tool']
os.system("wget {} -O /tmp/fishinstall/tools/{} --no-check-certificate".format(url,url[url.rfind('/')+1:]))
CmdTask("wget {} -O /tmp/fishinstall/tools/{} --no-check-certificate".format(url,url[url.rfind('/')+1:])).run()



Expand Down
17 changes: 13 additions & 4 deletions tools/translation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
import locale
import json
import os
import tools.base
from tools.base import CmdTask

_suported_languages = ['zh_CN', 'en_US']
url_prefix = os.environ.get('FISHROS_URL')
lang_url = url_prefix+'tools/translation/assets/{}.py'


COUNTRY_CODE_MAPPING = {
"CN": "zh_CN",
Expand All @@ -21,9 +28,12 @@ def __init__(self):
self._currentLocale = self.getLocalFromIP()
if self._currentLocale is None:
self._currentLocale = locale.getdefaultlocale()[0]

# Load the translation file.
for lang in _suported_languages:
CmdTask("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(lang_url.format(lang),lang_url.format(lang).replace(url_prefix,''))).run()

self.loadTranslationFlile()
tools.base.tr = self

def loadTranslationFlile(self):
# Load the translation file.
Expand All @@ -47,11 +57,10 @@ def tr(self, string) -> str:
def getLocalFromIP(self) -> str:
local_str = ""
try:
os.system("""wget --header="Accept: application/json" --no-check-certificate "https://ip.renfei.net/" -O /tmp/fishros_check_country.json""")

os.system("""wget --header="Accept: application/json" --no-check-certificate "https://ip.renfei.net/" -O /tmp/fishros_check_country.json -qq""")
with open('/tmp/fishros_check_country.json', 'r') as json_file:
data = json.loads(json_file.read())

self.ip_info = data
if data['location']['countryCode'] in COUNTRY_CODE_MAPPING:
local_str = COUNTRY_CODE_MAPPING[data['location']['countryCode']]
else:
Expand Down

0 comments on commit dc73c88

Please sign in to comment.