8000 Use mdbook-i18n-helpers to support translations by mgeisler · Pull Request #356 · rust-embedded/book · GitHub
[go: up one dir, main page]

Skip to content

Use mdbook-i18n-helpers to support translations #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use mdbook-i18n-helpers to support translations
This PR expands the publishing step to also publish a (very partial)
Danish translation of the book.

The infrastructure uses the GNU Gettext tools, as well as a mdbook
plugin:

    sudo apt install gettext
    cargo install mdbook-i18n-helpers

The translation lives in po/da.po, which was created with

    MDBOOK_OUTPUT='{"xgettext": {"pot-file": "messages.pot"}}' mdbook build -d po
    msginit -l da -i po/messages.pot -o po/da.po

After creating the da.po file, the translation can be served locally
with:

    MDBOOK_BOOK__LANGUAGE=da mdbook serve

The publication flow on GitHub has been expanded to publish
translations into subdirectories named after the two-letter ISO_639-1
language codes. The English source text stays the same.

Links to the new translations are added manually in the index.hbs
theme file[1]. I imported the one from mdbook and added a very simple
language picker in the top right corner.

[1]: https://raw.githubusercontent.com/rust-lang/mdBook/master/src/theme/index.hbs
  • Loading branch information
mgeisler committed May 16, 2023
commit 5e26a4d2cb00da06d788b044d8d60254aa06bcfc
55 changes: 31 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on:
branches: [ staging, trying, master ]
pull_request:

permissions:
contents: read
pages: write
id-token: write

jobs:
build:
runs-on: ubuntu-20.04
Expand All @@ -21,25 +26,9 @@ jobs:
- name: Put pip binary directory into path
run: echo "~/.local/bin" >> $GITHUB_PATH

- name: Cache Cargo installed binaries
uses: actions/cache@v1
id: cache-cargo
with:
path: ~/cargo-bin
key: cache-cargo
- name: Install mdbook
if: steps.cache-cargo.outputs.cache-hit != 'true'
uses: actions-rs/install@v0.1
- uses: moonrepo/setup-rust@v0
with:
crate: mdbook
version: latest
- name: Copy mdbook to cache directory
if: steps.cache-cargo.outputs.cache-hit != 'true'
run: |
mkdir ~/cargo-bin
cp ~/.cargo/bin/mdbook ~/cargo-bin
- name: Put new cargo binary directory into path
run: echo "~/cargo-bin" >> $GITHUB_PATH
bins: mdbook, mdbook-i18n-helpers

- name: Build book
run: mdbook build
Expand All @@ -48,10 +37,28 @@ jobs:
- name: Check links
run: linkchecker book

- name: Deploy book
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
uses: peaceiris/actions-gh-pages@v3
- name: Build translations
# Expand the list of langauges in the for loop as needed.
# Update the language picker in theme/index.hbs when a
# language is complete enough to be found by users.
run: |
for po_lang in da; do
echo "::group::Building $po_lang translation"
MDBOOK_BOOK__LANGUAGE=$po_lang \
MDBOOK_OUTPUT__HTML__SITE_URL=/book/$po_lang/ \
mdbook build -d book/$po_lang
echo "::endgroup::"
done

- name: Setup Pages
uses: actions/configure-pages@v2

- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: book
force_orphan: true
path: book

- name: Deploy to GitHub Pages
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
id: deployment
uses: actions/deploy-pages@v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
book
po/messages.pot
6 changes: 6 additions & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ multilingual = false
src = "src"
title = "The Embedded Rust Book"

[build]
extra-watch-dirs = ["po"]

[preprocessor.gettext]
after = ["links"]

[output.html]
git-repository-url = "https://github.com/rust-embedded/book"
Loading
0