Skip to content
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

Add option to localize books in multiple languages #1306

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e4b443c
Add language config section
Ruin0x11 Aug 27, 2020
24e6d6b
Change book source root depending on language
Ruin0x11 Aug 27, 2020
3049d9f
Actually, don't change source root
Ruin0x11 Aug 27, 2020
96d9271
Specify language for book in command line args
Ruin0x11 Aug 28, 2020
8869c2c
Build multiple books from localizations at once
Ruin0x11 Aug 28, 2020
85ab4d3
Redirect to translation index page in serve command
Ruin0x11 Aug 28, 2020
282fdaa
Redirect to a 404 page when serving translated
Ruin0x11 Aug 28, 2020
e17ce64
Fix test using create_missing
Ruin0x11 Aug 28, 2020
5e223e0
Support localizing book title/description
Ruin0x11 Aug 28, 2020
a042cfc
Make `mdbook init` output multilingual structure
Ruin0x11 Aug 28, 2020
ee740ac
Remove 'default' property on languages, use book.language instead
Ruin0x11 Aug 28, 2020
c72ce18
Rewrite links in Markdown to point to fallback if missing in translation
Ruin0x11 Aug 28, 2020
98c3a04
Move example book to multilingual structure
Ruin0x11 Aug 28, 2020
8d1c086
Fix {{#include}} directives for default language
Ruin0x11 Aug 28, 2020
09a8b66
Improve robustness of link rewriting
Ruin0x11 Aug 29, 2020
5fed5e8
Update mdBook manual to have information about translations
Ruin0x11 Sep 15, 2021
d6c27ab
Implement translation fallback of files included with preprocessing
Ruin0x11 Aug 29, 2020
92ec3dd
[localization] Fixes for latest master
Ruin0x11 Sep 15, 2021
56e72a2
[localization] rustfmt
Ruin0x11 Sep 15, 2021
9d8147c
Remove extra `localization.md`
Ruin0x11 Sep 16, 2021
7305e8c
Merge remote-tracking branch 'upstream/master' into localization
Ruin0x11 Feb 25, 2022
e74fdb1
Make `chapter_titles` optional in Book
Ruin0x11 Feb 25, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ guide/book

.vscode
tests/dummy_book/book/
tests/localized_book/book/
test_book/book/

# Ignore Jetbrains specific files.
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ chrono = "0.4"
clap = "2.24"
env_logger = "0.7.1"
handlebars = "4.0"
http = "0.2.4"
lazy_static = "1.0"
log = "0.4"
memchr = "2.0"
Expand Down
3 changes: 3 additions & 0 deletions guide/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ heading-split-level = 2

[output.html.redirect]
"/format/config.html" = "configuration/index.html"

[language.en]
name = "English"
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions guide/src/SUMMARY.md → guide/src/en/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [General](format/configuration/general.md)
- [Preprocessors](format/configuration/preprocessors.md)
- [Renderers](format/configuration/renderers.md)
- [Localization](format/configuration/localization.md)
- [Environment Variables](format/configuration/environment-variables.md)
- [Theme](format/theme/README.md)
- [index.hbs](format/theme/index-hbs.md)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 10 additions & 4 deletions guide/src/cli/init.md → guide/src/en/cli/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ up for you:
```bash
book-test/
├── book
├── book.toml
└── src
├── chapter_1.md
└── SUMMARY.md
└── en
├── chapter_1.md
└── SUMMARY.md
```

- The `src` directory is where you write your book in markdown. It contains all
the source files, configuration files, etc.
- The `src` directory is where you write your book in Markdown. It contains all
the source files for each translation of your book. By default, a directory
for the English translation is created, `src/en`.

- The `book.toml` file holds configuration about how your book gets rendered.
See the [configuration](../format/config.md) section for more details.

- The `book` directory is where your book is rendered. All the output is ready
to be uploaded to a server to be seen by your audience.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ be adapted for other preprocessors.
```rust
// nop-preprocessors.rs

{{#include ../../../examples/nop-preprocessor.rs}}
{{#include ../../../../examples/nop-preprocessor.rs}}
```
</details>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ This section details the configuration options available in the ***book.toml***:
- **[General]** configuration including the `book`, `rust`, `build` sections
- **[Preprocessor]** configuration for default and custom book preprocessors
- **[Renderer]** configuration for the HTML, Markdown and custom renderers
- **[Localization]** configuration for books written in more than one language
- **[Environment Variable]** configuration for overriding configuration options in your environment

[General]: general.md
[Preprocessor]: preprocessors.md
[Renderer]: renderers.md
[Environment Variable]: environment-variables.md
[Localization]: localization.md
[Environment Variable]: environment-variables.md
86 changes: 86 additions & 0 deletions guide/src/en/format/configuration/localization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Localization

It's possible to write your book in more than one language and bundle all of its
translations into a single output folder, with the ability for readers to switch
between each one in the rendered output. The available languages for your book
are defined in the `[language]` table:

```toml
[language.en]
name = "English"

[language.ja]
name = "日本語"
title = "本のサンプル"
description = "この本は実例です。"
authors = ["Ruin0x11"]
```

Each language must have a human-readable `name` defined. Also, if the
`[language]` table is defined, you must define `book.language` to be a key of
this table, which will indicate the language whose files will be used for
fallbacks if a page is missing in a translation.

The `title` and `description` fields, if defined, will override the ones set in
the `[book]` section. This way you can translate the book's title and
description. `authors` provides a list of this translation's authors.

After defining a new language like `[language.ja]`, add a new subdirectory
`src/ja` and create your `SUMMARY.md` and other files there.

> **Note:** Whether or not the `[language]` table is defined changes the format
> of the `src` directory that mdBook expects to see. If there is no `[language]`
> table, mdBook will treat the `src` directory as a single translation of the
> book, with `SUMMARY.md` at the root:
>
> ```
> ├── book.toml
> └── src
> ├── chapter
> │ ├── 1.md
> │ ├── 2.md
> │ └── README.md
> ├── README.md
> └── SUMMARY.md
> ```
>
> If the `[language]` table is defined, mdBook will instead expect to find
> subdirectories under `src` named after the keys in the table:
>
> ```
> ├── book.toml
> └── src
> ├── en
> │ ├── chapter
> │ │ ├── 1.md
> │ │ ├── 2.md
> │ │ └── README.md
> │ ├── README.md
> │ └── SUMMARY.md
> └── ja
> ├── chapter
> │ ├── 1.md
> │ ├── 2.md
> │ └── README.md
> ├── README.md
> └── SUMMARY.md
> ```

If the `[language]` table is used, you can pass the `-l <language id>` argument
to commands like `mdbook build` to build the book for only a single language. In
this example, `<language id>` can be `en` or `ja`.

Some extra notes on translations:

- In a translation's `SUMMARY.md` or inside Markdown files, you can link to
pages, images or other files that don't exist in the current translation, but
do exist in the default translation. This is so you can have a fallback in
case new pages get added in the default language that haven't been translated
yet.
- Each translation can have its own `SUMMARY.md` with differing content from
other translations. Even if the translation's summary goes out of sync with
the default language, the links will continue to work so long as the pages
exist in either translation.
- Each translation can have its own pages listed in `SUMMARY.md` that don't
exist in the default translation at all, in case extra information specific to
that language is needed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ shout-out to them!
- Vivek Akupatni ([apatniv](https://github.com/apatniv))
- Eric Huss ([ehuss](https://github.com/ehuss))
- Josh Rotenberg ([joshrotenberg](https://github.com/joshrotenberg))
- [Ruin0x11](https://github.com/Ruin0x11)

If you feel you're missing from this list, feel free to add yourself in a PR.
Loading