Skip to content

Commit 160dd33

Browse files
author
luting
committed
🎉 init(superset): 4.1.1-py311
1 parent 4c1f1aa commit 160dd33

File tree

11 files changed

+47583
-2
lines changed

11 files changed

+47583
-2
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
generate_locales.py
2+
generate_messages.py
3+
.gitea
4+
locales
5+
target

.github/workflows/docker-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: ci
1+
name: DockerHub Publish
22

33
on:
44
workflow_dispatch:
@@ -12,7 +12,7 @@ on:
1212
- 'main'
1313

1414
jobs:
15-
docker:
15+
build-push:
1616
runs-on: ubuntu-latest
1717
steps:
1818
-

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM apache/superset:4.1.1-py311
2+
3+
COPY messages.json /app/superset/translations/zh/LC_MESSAGES/messages.json
4+
COPY messages.po /app/superset/translations/zh/LC_MESSAGES/messages.po
5+
6+
USER root
7+
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
8+
export DEBIAN_FRONTEND=noninteractive &&\
9+
apt-get update &&\
10+
apt-get -y install --no-install-recommends --no-install-suggests wget pkg-config gcc &&\
11+
apt-get -y clean && rm -rf /var/lib/apt/lists/* &&\
12+
# 安装数据库驱动
13+
pip install psycopg2 mysqlclient &&\
14+
# 默认语言
15+
sed -i "s/*BABEL_DEFAULT_LOCALE*/BABEL_DEFAULT_LOCALE = \"zh\"/" /app/superset/config.py &&\
16+
sed -i "s/LANGUAGES = {}/LANGUAGES = {\"zh\": {\"flag\": \"cn\", \"name\": \"简体中文\"}, \"en\": {\"flag\": \"us\", \"name\": \"English\"}}/" /app/superset/config.py &&\
17+
# 清理不需要的翻译
18+
cd /app/superset/translations &&\
19+
rm -rf ar de es fr it ja ko nl pt pt_BR ru sk sl tr uk zh-TW &&\
20+
# 翻译
21+
pybabel compile -d /app/superset/translations || true
22+
23+
USER superset

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Superset 汉化/中文版
2+
3+
![Version](https://img.shields.io/docker/v/lutinglt/superset-zh/latest?arch=amd64&sort=semver&color=066da5) ![Docker Pulls](https://img.shields.io/docker/pulls/lutinglt/superset-zh.svg?style=flat&label=pulls&logo=docker) ![Docker Size](https://img.shields.io/docker/image-size/lutinglt/superset-zh/latest?color=066da5&label=size) ![License](https://img.shields.io/github/license/lutinglt/superset-zh)
4+
5+
## 简介
6+
7+
[PR: 29476](https://github.com/apache/superset/pull/29476) 这个提交中删除了大量的中文翻译,导致 Superset 的中文翻译质量大幅下降, 在这次提交后便没有人再对中文翻译进行维护。
8+
9+
本项目基于 [PR: 27922](https://github.com/apache/superset/pull/27922) 的最后一次中文翻译提交,用 Python 构建了翻译脚本, 脚本改变了汉化步骤, 先生成 messages.json, 再通过 json 生成 messages.po, 此过程修复了一些翻译文件无法正常生效的问题.
10+
11+
## 使用方法
12+
13+
### Docker 镜像
14+
15+
#### 下载镜像
16+
17+
基于官方镜像生成, 修复了汉化问题, 仅保留中文和英文两种语言并且默认显示中文, 默认时区上海, 并添加了 Postgres 和 MySQL 数据库驱动, 其他配置与官方镜像相同.
18+
19+
```bash
20+
docker pull lutinglt/superset-zh
21+
```
22+
23+
#### 手动构建
24+
25+
FROM 关键字后替换自己需要的官方镜像标签
26+
27+
```bash
28+
git clone https://github.com/lutinglt/superset-zh.git
29+
cd superset-zh
30+
docker build -t lutinglt/superset-zh .
31+
```
32+
33+
### 手动汉化
34+
35+
找到 Superset 安装目录下的 `translations` 目录, 找到 `zh/LC_MESSAGES` 目录, 直接将项目仓库里的 `messages.json``messages.po` 文件复制到 `zh/LC_MESSAGES` 目录下, 然后运行:
36+
37+
```bash
38+
# 替换成自己的安装目录下的 translations 目录
39+
pybabel compile -d superset/translations
40+
```
41+
42+
重启 Superset 查看汉化效果.
43+
44+
> [!IMPORTANT]
45+
>
46+
> config.py 里的 `BABEL_DEFAULT_LOCALE` 变量会影响标题栏的汉化, 默认为 `en`, 修改为 `zh` 重新编译即可.
47+
48+
> [!IMPORTANT]
49+
>
50+
> config.py 里的 `LANGUAGES` 变量为空会关闭语言选择框, 默认为空, 参考配置:
51+
52+
```python
53+
BABEL_DEFAULT_LOCALE = "zh"
54+
LANGUAGES = {
55+
"zh": {"flag": "cn", "name": "简体中文"},
56+
"en": {"flag": "us", "name": "English"},
57+
}
58+
```
59+
60+
> [!NOTE]
61+
>
62+
> superset_config.py 会覆盖 config.py 里的配置, 优先级更高.
63+
64+
> [!TIP]
65+
>
66+
> Superset 2.1.0 之后安装的默认安全选项更为严格, 部署后登录不上, 或无法启动推荐添加以下配置
67+
>
68+
> ```python
69+
> SECRET_KEY = 'superset' # 安全密钥, 启动必须进行配置
70+
> WTF_CSRF_ENABLED = False # 关闭 CSRF 验证
71+
> TALISMAN_ENABLED = False # 关闭 TALISMAN 安全选项
72+
> CONTENT_SECURITY_POLICY_WARNING = False # 关闭内容安全策略警告
73+
> ```
74+
75+
## 脚本说明
76+
77+
### `generate_locales.py`
78+
79+
基于 Superset 项目下的 `superset/translations/messages.pot``superset/translations/zh/LC_MESSAGES/messages.po` 生成最新的需要翻译的内容, 然后取本项目下 `messages.json` 已翻译的部分覆盖需要翻译的内容, 生成全部翻译条目(包含未翻译)和筛选出未翻译的条目的 json 文件, 进行手动校验翻译过程, 方便补充新翻译和修改已翻译内容, 具体查看脚本文档注释内容.
80+
81+
### `generate_messages.py`
82+
83+
根据已经翻译的内容生成 Superset 前端需要的 `messages.json``messages.po`, 具体查看脚本文档注释内容.
84+
85+
## 贡献
86+
87+
欢迎提交 PR, 修复汉化问题, 补充汉化内容或者优化翻译脚本.
88+
89+
汉化贡献仅需提交最新的 `messages.json` 文件即可

generate_locales.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/python
2+
# -*- coding:utf-8 -*-
3+
"""
4+
所有文件统一使用 utf-8 编码, 行尾统一使用 LF
5+
运行前请下载最新的 messages.pot 和 messages.po 文件, 并放置在 locales 目录下 (鉴于网络原因, 不使用脚本请手动下载)
6+
https://github.com/apache/superset/blob/master/superset/translations/messages.pot
7+
https://github.com/apache/superset/blob/master/superset/translations/zh/LC_MESSAGES/messages.po
8+
先在项目的当前目录运行该脚本, 生成 target 待翻译目录
9+
target/tmp/messages.json : 保存所有翻译, 用于修改现有翻译(如果补充翻译会被filter_messages.json覆盖)
10+
target/tmp/filter_messages.json : 保存未翻译的, 用于补充翻译
11+
target/tmp/plural_messages.json : 保存复数翻译, 标记文件, 请勿修改
12+
完善翻译后, 在项目的当前目录运行 generate_messages.py
13+
"""
14+
15+
import json
16+
import os
17+
18+
import polib
19+
from babel.messages.frontend import CommandLineInterface
20+
21+
if __name__ == "__main__":
22+
# 根据最新的翻译模版 messages.pot 生成新的 messages.po
23+
argv = ["pybabel", "init", "-i", "locales/messages.pot", "-d", "target/locales", "-l", "en"]
24+
CommandLineInterface().run(argv)
25+
26+
# 读取旧的 messages.json
27+
with open("messages.json", "r", encoding="utf-8") as f:
28+
old_trans = json.load(f)["locale_data"]["superset"]
29+
del old_trans[""]
30+
31+
# 读取新的 messages.po
32+
po = polib.pofile("target/locales/en/LC_MESSAGES/messages.po")
33+
new_trans = {entry.msgid: entry.msgstr for entry in po}
34+
plural_trans = {entry.msgid: entry.msgid_plural for entry in po if entry.msgstr_plural}
35+
36+
# 新的 messages.po 合并新的中文 messages.po 和旧的 messages.json
37+
zh_po = polib.pofile("locales/zh/LC_MESSAGES/messages.po")
38+
new_trans.update({zh_entry.msgid: zh_entry.msgstr for zh_entry in zh_po})
39+
new_trans.update(old_trans)
40+
41+
# 生成新的 messages.json
42+
os.makedirs("target/tmp", exist_ok=True)
43+
with open("target/tmp/messages.json", "w", encoding="utf-8") as f:
44+
json.dump(new_trans, f, ensure_ascii=False, indent=2, sort_keys=True)
45+
46+
with open("target/tmp/filter_messages.json", "w", encoding="utf-8") as f:
47+
filter_trans = {k: v for k, v in new_trans.items() if v == "" or v == [""]}
48+
json.dump(filter_trans, f, ensure_ascii=False, indent=2, sort_keys=True)
49+
50+
with open("target/tmp/plural_messages.json", "w", encoding="utf-8") as f:
51+
json.dump(plural_trans, f, ensure_ascii=False, indent=2, sort_keys=True)

generate_messages.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/python
2+
# -*- coding:utf-8 -*-
3+
"""
4+
该脚本运行后,会生成 target/messages.json, 并将 target/messages.json 转换为 target/messages.po
5+
检查无误后, 请使用默认配置的 prettier 格式化 target/messages.json
6+
手动覆盖根目录下的 messages.json 和 messages.po, 之后提交代码
7+
"""
8+
9+
import json
10+
11+
import polib
12+
13+
if __name__ == "__main__":
14+
# 读取翻译文件
15+
with open("target/tmp/messages.json", "r", encoding="utf-8") as f:
16+
new_trans: dict[str, str | list[str]] = json.load(f)
17+
18+
# 读取翻译补充文件
19+
with open("target/tmp/filter_messages.json", "r", encoding="utf-8") as f:
20+
new_trans.update(json.load(f))
21+
22+
# 读取复数翻译标记文件
23+
with open("target/tmp/plural_messages.json", "r", encoding="utf-8") as f:
24+
plural_trans: dict[str, str] = json.load(f)
25+
26+
# 读取现有的翻译文件
27+
with open("messages.json", "r", encoding="utf-8") as f:
28+
trans: dict = json.load(f)
29+
trans["locale_data"]["superset"].update(new_trans)
30+
# superset 前端翻译的值如果是字符串, 只会显示第一个字符, 所以需要转换为列表
31+
for k, v in trans["locale_data"]["superset"].items():
32+
if isinstance(v, str):
33+
trans["locale_data"]["superset"][k] = [v]
34+
35+
# 生成 messages.json
36+
with open("target/messages.json", "w", encoding="utf-8") as f:
37+
json.dump(trans, f, ensure_ascii=False, indent=2, sort_keys=True)
38+
39+
# 生成 messages.po
40+
zh_po = polib.pofile("locales/zh/LC_MESSAGES/messages.po")
41+
po = polib.POFile()
42+
po.metadata = zh_po.metadata
43+
po_trans = {k: v[0] if isinstance(v, list) else v for k, v in trans["locale_data"]["superset"].items()}
44+
del po_trans[""]
45+
for msgid, msgstr in po_trans.items():
46+
if msgid in plural_trans:
47+
entry = polib.POEntry(
48+
msgid=msgid,
49+
msgid_plural=plural_trans[msgid],
50+
msgstr_plural={0: msgstr},
51+
)
52+
else:
53+
entry = polib.POEntry(
54+
msgid=msgid,
55+
msgstr=msgstr,
56+
)
57+
po.append(entry)
58+
po.save("target/messages.po")

0 commit comments

Comments
 (0)