-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env -S docker build -tfreedwu/translate-shell:main . -f | ||
# docker run -it --rm -e GITHUB_WORKSPACE=/mnt -v $PWD:/mnt freedwu/translate-shell:main | ||
FROM python | ||
|
||
LABEL org.opencontainers.image.title=translate-shell | ||
LABEL org.opencontainers.image.authors="Wu Zhenyu" | ||
LABEL org.opencontainers.image.vendor="Wu Zhenyu" | ||
LABEL org.opencontainers.image.url=https://ghcr.io/Freed-Wu/translate-shell | ||
# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#labelling-container-images | ||
LABEL org.opencontainers.image.source=https://github.com/Freed-Wu/translate-shell | ||
LABEL org.opencontainers.image.description="Translate .po of one repo" | ||
LABEL org.opencontainers.image.licenses=GPL-3.0 | ||
|
||
RUN pip install polib translate_shell | ||
COPY scripts/entrypoint.py / | ||
|
||
ENTRYPOINT ["/entrypoint.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
name: translate-shell | ||
description: Translate .po of one repo | ||
inputs: | ||
files: | ||
description: files need to be translated, support glob | ||
required: false | ||
default: "**/*.po" | ||
target_lang: | ||
description: target language, po's metadata's Language override it | ||
required: false | ||
default: zh-cn | ||
source_lang: | ||
description: source language | ||
required: false | ||
default: en | ||
translator: | ||
description: translator | ||
required: false | ||
default: google | ||
wrapwidth: | ||
description: wrap the width by polib | ||
required: false | ||
default: "76" | ||
branding: | ||
icon: check | ||
color: yellow | ||
runs: | ||
using: docker | ||
image: Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python | ||
r"""Refer ``action.yml``.""" | ||
import os | ||
from difflib import Differ | ||
from glob import glob | ||
|
||
import polib | ||
|
||
from translate_shell.translate import translate | ||
|
||
fnames = os.getenv("INPUT_FILES", "**/*.po") | ||
default_target_lang = os.getenv("INPUT_TARGET_LANG", "zh-cn") | ||
source_lang = os.getenv("INPUT_SOURCE_LANG", "en") | ||
translator = os.getenv("INPUT_TRANSLATOR", "google") | ||
wrapwidth = int(os.getenv("INPUT_WRAPWIDTH", "76")) | ||
|
||
workspace = os.getenv("GITHUB_WORKSPACE", ".") | ||
differ = Differ() | ||
files = sum( | ||
[ | ||
glob(os.path.join(workspace, fname), recursive=True) | ||
for fname in fnames.splitlines() | ||
], | ||
[], | ||
) | ||
pos = [polib.pofile(file, wrapwidth=wrapwidth) for file in files] | ||
|
||
for po in pos: | ||
target_lang = po.metadata.get("Language", "en") | ||
untranslated_entries = po.untranslated_entries() | ||
print(f"{po.fpath}: {len(untranslated_entries)}") | ||
for entry in untranslated_entries: | ||
old = str(entry).splitlines() | ||
entry.msgstr = translate( | ||
entry.msgid, target_lang, source_lang, [translator] | ||
).results[0]["paraphrase"] | ||
entry.fuzzy = False # type: ignore | ||
new = str(entry).splitlines() | ||
diff = differ.compare(old, new) | ||
print("\n".join(diff)) | ||
if len(untranslated_entries): | ||
po.save() |
File renamed without changes.