Skip to content

Commit

Permalink
👷 Add action.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed May 31, 2023
1 parent 25d2277 commit 9a4e522
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
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"]
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,76 @@ $ xsel -o | trans --format json | jq -r '"《\(.results[].paraphrase)》的英
Just for Fun is 纯娱乐 in Chinese.
```

### CI/CD

#### Github Action

This repo provides an action to translate `*.po` of a repository. See
[inputs](https://github.com/Freed-Wu/translate-shell/blob/main/action.yml).
For example, you have a repository which contains translations of another
project's documents (upstream), you can write a github workflow to detect if
upstream has update. If a new version exist, update the version and
generate new
[`.po`](https://www.gnu.org/software/gettext/manual/gettext.html#PO-Files)s,
then translate the changed `.po`s and `git commit`.

Examples:

- [tmux-zh](https://github.com/Freed-Wu/tmux-zh/blob/main/.github/workflows/version.yml)

```yaml
on:
schedule:
# Run this CI/CD at 0:00 on Friday
- cron: 0 0 * * 5
workflow_dispatch:

jobs:
translate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Generate new .po
id: version
run: |
# update version
# then use perl / sed / ... to replace the version string of your file
# then generate new .po
echo VERSION=XXX > $GITHUB_OUTPUT
- name: Translate your *.po
uses: Freed-Wu/translate-shell@main
- name: Git commit
run: |
git add **.po
git config --global user.name 'Github Actions'
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
git commit -m ":bookmark: Dump version to $VERSION"
git tag "$VERSION"
git remote set-url origin "https://x-access-token:$GH_TOKEN@github.com/$GITHUB_REPOSITORY"
git push
git push --tags
env:
VERSION: ${{steps.version.outputs.VERSION}}
GH_TOKEN: ${{secrets.GH_TOKEN}}
```
You can use the following commands to get the new version:
```bash
# get a github repo's version:
curl https://api.github.com/repos/user/repo/releases/latest | jq -r .tag_name
# get a gitlab repo's version
curl 'https://gitlab.com/api/v4/projects/41218592/repository/tags?per_page=1' |
jq -r '.[].name'
```

You can use the following tools to generate the new `.po`s:

- [sphinx-intl](https://sphinx-intl.readthedocs.io): Generate `.po` for any
project using sphinx to generate document.
- [po4a](https://po4a.org): Generate `.po` for any project which use markdown,
`LaTeX`, man, ... to write document.

## Similar Projects

See [comparison](https://translate-shell.readthedocs.io/en/latest/resources/translator.html).
Expand Down
30 changes: 30 additions & 0 deletions action.yml
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
42 changes: 42 additions & 0 deletions scripts/entrypoint.py
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.

0 comments on commit 9a4e522

Please sign in to comment.