Skip to content

Commit c45ae32

Browse files
committed
Preliminary support multilingual
mdBook does not natively support multilingual documentation. A common approach is to use a third-party library like mdbook-i18n-helper, which relies on a Gettext-based workflow to enable multilingual support. However, I believe such a complex system is unnecessary for our case. Therefore, this commit follows the approach suggested in the official mdBook documentation: overriding the 'index.hbs' template to add a button that redirects users to the corresponding documentation in another language, thus achieving basic multilingual support. This commit introduces the following three files under the theme directory: - 'index.hbs': Adds a redirect button for switching languages. - 'lang-toggle.css': Defines the styling for the button and the dropdown menu. - 'lang-toggle.js': Implements the main logic behind the button's behavior. As a result, two separate books need to be built. However, since mdBook’s 'book.toml' does not support multiple '[book]'' sections in a single configuration, this commit introduces a CMake script to build different language versions of the book and place them in their respective directories. You can test this using command 'cmake -P build.cmake'. If you want to use 'mdbook serve', you can test it by adding the '-DSERVE=ON' flag. This will build all the languages listed in the build script and copy them into the directory generated by mdbook serve. Due to mdBook's behavior, the 'src' field in 'book.toml' determines the root URL. Therefore, if the theme directory is placed above the src directory, mdBook will copy it relative to the 'src' path during the build, which will cause the theme folder to become unreachable. Since English is the primary language of this documentation, the default behavior supports running 'mdbook' command directly from the guide directory, such as with 'mdbook serve'. However, this will only generate the English version of the documentation, other languages will not be built. To build other language versions using the mdbook command directly, it must manually adjust their respective book.toml files. All translations are placed under 'guide/translations'. To add a new language, it need to follow the same folder structure, update the CMake script, and register the new language in 'theme/index.hbs'. [1]: https://github.com/google/mdbook-i18n-helpers
1 parent 5c1613c commit c45ae32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2801
-2
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: build book
2525
run: |
2626
cd guide
27-
mdbook build
27+
cmake -P build.cmake
2828
ls book
2929
- name: setup pages
3030
uses: actions/configure-pages@v4

guide/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
book
2+
build/*

guide/book.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
[book]
22
authors = ["Karn Kaul"]
33
language = "en"
4-
multilingual = false
54
src = "src"
65
title = "Learn Vulkan"
6+
7+
[output.html]
8+
theme = "theme"
9+
additional-js = ["theme/lang-toggle.js"]
10+
additional-css = ["theme/lang-toggle.css"]

guide/build.cmake

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Build the target languages
2+
function(BuildBook LANGUAGE SOURCE_DIR TARGET_DIR)
3+
set(LANGUAGE "${LANGUAGE}")
4+
5+
if(NOT EXISTS "${SOURCE_DIR}/src/SUMMARY.md")
6+
message(WARNING "Skipping '${LANGUAGE}' – SUMMARY.md not found at ${SOURCE_DIR}")
7+
return()
8+
endif()
9+
10+
if(NOT EXISTS "${SOURCE_DIR}/book.toml")
11+
message(WARNING "Skipping '${LANGUAGE}' – book.toml not found at ${SOURCE_DIR}")
12+
return()
13+
endif()
14+
15+
message(STATUS "Building book for language: ${LANGUAGE}")
16+
execute_process(
17+
COMMAND mdbook build -d ${TARGET_DIR}
18+
WORKING_DIRECTORY ${SOURCE_DIR}
19+
)
20+
endfunction()
21+
22+
# Check if the translations theme folder exists
23+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/translations/theme")
24+
message(STATUS "Translations theme folder found.")
25+
else()
26+
message(WARNING "Translations theme folder not found. Copy it from the guide folder.")
27+
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/theme" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/translations")
28+
endif()
29+
30+
# For 'mdbook serve' usage
31+
if(NOT DEFINED SERVE)
32+
set(SERVE OFF)
33+
endif()
34+
35+
if(SERVE)
36+
# Dont need to build Enslish version, it would be built when running 'mdbook serve'
37+
BuildBook("zh-TW" "${CMAKE_CURRENT_SOURCE_DIR}/translations/zh-TW" "${CMAKE_CURRENT_SOURCE_DIR}/.tmp-output/zh-TW") # would copy these translation to the book folder later
38+
39+
message(STATUS "Running 'mdbook serve'")
40+
if(WIN32)
41+
execute_process(
42+
COMMAND powershell -Command "Start-Process mdbook -ArgumentList 'serve','-d','${CMAKE_CURRENT_SOURCE_DIR}/book'"
43+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
44+
)
45+
else()
46+
execute_process(
47+
COMMAND bash -c "mdbook serve -d \"${CMAKE_CURRENT_SOURCE_DIR}/book\" &"
48+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
49+
)
50+
endif()
51+
52+
# Move the zh-TW output folder into book/
53+
message(STATUS "Moving the translation folder to final location...")
54+
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep 2)
55+
56+
# Move the translation folder to the book folder
57+
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/book")
58+
file(RENAME
59+
"${CMAKE_CURRENT_SOURCE_DIR}/.tmp-output/zh-TW"
60+
"${CMAKE_CURRENT_SOURCE_DIR}/book/zh-TW"
61+
)
62+
63+
# Remove the temporary output folder
64+
message(STATUS "Removed temporary folder: .tmp-output")
65+
file(REMOVE_RECURSE "${CMAKE_CURRENT_SOURCE_DIR}/.tmp-output")
66+
else()
67+
# Only Build the target languages for `mdbook build` usage
68+
BuildBook("en" "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/book")
69+
BuildBook("zh-TW" "${CMAKE_CURRENT_SOURCE_DIR}/translations/zh-TW" "${CMAKE_CURRENT_SOURCE_DIR}/book/zh-TW")
70+
endif()

0 commit comments

Comments
 (0)