Debian Codegraph @

docs/dev-guide/translations.md

raw
1 # Translations
2
3 Theme localization guide.
4
5 ---
6
7 The [built-in themes] that are included with MkDocs provide support for
8 translations. This is a guide for translators, which documents the process for
9 contributing new translations and/or updating existing translations. For
10 guidance on modifying the existing themes, see the [Contributing Guide][update
11 themes]. To enable a specific translation see the documentation about the
12 specific theme you are using in the [User Guide][built-in themes]. For
13 translations of third-party themes, please see the documentation for those
14 themes. For a third-party theme to make use of MkDocs' translation tools and
15 methods, that theme must be properly [configured] to make use of those tools.
16
17 NOTE:
18 Translations only apply to text contained within a theme's template, such
19 as "next" and "previous" links. The Markdown content of a page is not
20 translated. If you wish to create multilingual documentation, you need to
21 combine theme localization with a third-party
22 internationalization/localization plugin.
23
24 [built-in themes]: ../user-guide/choosing-your-theme.md
25 [update themes]: ../about/contributing.md#submitting-changes-to-the-builtin-themes
26 [configured]: themes.md#supporting-theme-localizationtranslation
27
28 ## Localization tooling prerequisites
29
30 Theme localization makes use of the [babel][babel] project for generation and
31 compilation of localization files. You will need to be working from the
32 git working tree on your local machine to make use of the translation commands.
33
34 See the [Contributing Guide] for direction on how to [Install for Development]
35 and [Submit a Pull Request]. The instructions in this document assume that you
36 are working from a properly configured development environment.
37
38 Make sure translation requirements are installed in your environment:
39
40 ```bash
41 pip install 'mkdocs[i18n]'
42 ```
43
44 [babel]: https://babel.pocoo.org/en/latest/cmdline.html
45 [Contributing Guide]: ../about/contributing.md
46 [Install for Development]: ../about/contributing.md#installing-for-development
47 [Submit a Pull Request]: ../about/contributing.md#submitting-pull-requests
48
49 ## Adding language translations to themes
50
51 If your favorite language locale is not yet supported on one (or both) of the
52 built-in themes (`mkdocs` and `readthedocs`), you can easily contribute a
53 translation by following the steps below.
54
55 Here is a quick summary of what you'll need to do:
56
57 1. [Fork and clone the MkDocs repository](#fork-and-clone-the-mkdocs-repository) and then [install MkDocs for development](../about/contributing.md#installing-for-development) for adding and testing translations.
58 2. [Initialize new localization catalogs](#initializing-the-localization-catalogs) for your language (if a translation for your locale already exists, follow the instructions for [updating theme localization files](#updating-the-translation-catalogs) instead).
59 3. [Add a translation](#translating-the-mkdocs-themes) for every text placeholder in the localized catalogs.
60 4. [Locally serve and test](#testing-theme-translations) the translated themes for your language.
61 5. [Update the documentation](#updating-theme-documentation) about supported translations for each translated theme.
62 6. [Contribute your translation](#contributing-translations) through a Pull Request.
63
64 NOTE:
65 Translation locales are usually identified using the [ISO-639-1] (2-letter)
66 language codes. While territory/region/county codes are also supported,
67 location specific translations should only be added after the general
68 language translation has been completed and the regional dialect requires
69 use of a term which differs from the general language translation.
70
71 [ISO-639-1]: https://en.wikipedia.org/wiki/ISO_639-1
72
73 ### Fork and clone the MkDocs repository
74
75 In the following steps you'll work with a fork of the MkDocs repository. Follow
76 the instructions for [forking and cloning the MkDocs
77 repository](../about/contributing.md#installing-for-development).
78
79 To test the translations you also need to [install MkDocs for
80 development](../about/contributing.md#installing-for-development) from your fork.
81
82 ### Initializing the localization catalogs
83
84 The templates for each theme contain text placeholders that have been extracted
85 into a Portable Object Template (`messages.pot`) file, which is present in each
86 theme's folder.
87
88 Initializing a catalog consists of running a command which will create a
89 directory structure for your desired language and prepare a Portable Object
90 (`messages.po`) file derived from the `pot` file of the theme.
91
92 Use the `init_catalog` command on each theme's directory and provide the appropriate language code (`-l <language>`).
93
94 The language code is almost always just two lowercase letters, such as `sv`, but in some cases it needs to be further disambiguated.
95
96 See:
97
98 * [Already translated languages for built-in themes](../user-guide/choosing-your-theme.md#mkdocs-locale)
99 * [ISO 639 Language List](https://www.localeplanet.com/icu/iso639.html)
100 * [Language subtag registry](https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry)
101
102 In particular, the way to know that the `pt` language should be disambiguated as `pt_PT` and `pt_BR` is that the *Language subtag registry* page contains `pt-` if you search for it. Whereas `sv` should remain just `sv`, because that page does *not* contain `sv-`.
103
104 So, if we pick `es` (Spanish) as our example language code, to add a translation for it to both built-in themes, run these commands:
105
106 ```bash
107 pybabel init --input-file mkdocs/themes/mkdocs/messages.pot --output-dir mkdocs/themes/mkdocs/locales -l es
108 pybabel init --input-file mkdocs/themes/readthedocs/messages.pot --output-dir mkdocs/themes/readthedocs/locales -l es
109 ```
110
111 The above command will create a file structure as follows:
112
113 ```text
114 mkdocs/themes/mkdocs/locales
115 ├── es
116 │   └── LC_MESSAGES
117 │   └── messages.po
118 ```
119
120 You can now move on to the next step and [add a
121 translation](#translating-the-mkdocs-themes) for every text placeholder in the
122 localized catalog.
123
124 ## Updating a theme translation
125
126 If a theme's `messages.pot` template file has been [updated][update themes]
127 since the `messages.po` was last updated for your locale, follow the steps
128 below to update the theme's `messages.po` file:
129
130 1. [Update the theme's translation catalog](#updating-the-translation-catalogs) to refresh the translatable text placeholders of each theme.
131 2. [Translate](#translating-the-mkdocs-themes) the newly added translatable text placeholders on every `messages.po` catalog file language you can.
132 3. [Locally serve and test](#testing-theme-translations) the translated themes for your language.
133 4. [Contribute your translation](#contributing-translations) through a Pull Request.
134
135 ### Updating the translation catalogs
136
137 This step should be completed after a theme template have been [updated][update
138 themes] for each language that you are comfortable contributing a translation
139 for.
140
141 To update the `fr` translation catalog of both built-in themes, use the following commands:
142
143 ```bash
144 pybabel update --ignore-obsolete --input-file mkdocs/themes/mkdocs/messages.pot --output-dir mkdocs/themes/mkdocs/locales -l fr
145 pybabel update --ignore-obsolete --input-file mkdocs/themes/readthedocs/messages.pot --output-dir mkdocs/themes/readthedocs/locales -l fr
146 ```
147
148 You can now move on to the next step and [add a translation] for every updated
149 text placeholder in the localized catalog.
150
151 [add a translation]: #translating-the-mkdocs-themes
152
153 ### Translating the MkDocs themes
154
155 Now that your localized `messages.po` files are ready, all you need to do is
156 add a translation in each `msgstr` item for each `msgid` item in the file.
157
158 ```text
159 msgid "Next"
160 msgstr "Siguiente"
161 ```
162
163 WARNING:
164 Do not modify the `msgid` as it is common to all translations. Just add
165 its translation in the `msgstr` item.
166
167 Once you have finished translating all of the terms listed in the `po` file,
168 you'll want to [test your localized theme](#testing-theme-translations).
169
170 ### Testing theme translations
171
172 To test a theme with translations, you need to first compile the `messages.po`
173 files of your theme into `messages.mo` files. The following commands will compile
174 the `es` translation for both built-in themes:
175
176 ```bash
177 pybabel compile --statistics --directory mkdocs/themes/mkdocs/locales -l es
178 pybabel compile --statistics --directory mkdocs/themes/readthedocs/locales -l es
179 ```
180
181 The above command results in the following file structure:
182
183 ```text
184 mkdocs/themes/mkdocs/locales
185 ├── es
186 │   └── LC_MESSAGES
187 │   ├── messages.mo
188 │   └── messages.po
189 ```
190
191 Note that the compiled `messages.mo` file was generated based on the
192 `messages.po` file that you just edited.
193
194 Then modify the `mkdocs.yml` file at the root of the project to test the new
195 and/or updated locale:
196
197 ```yaml
198 theme:
199 name: mkdocs
200 locale: es
201 ```
202
203 Finally, run `mkdocs serve` to check out your new localized version of the theme.
204
205 > NOTE:
206 > The build and release process takes care of compiling and distributing
207 > all locales to end users so you only have to worry about contributing the
208 > actual text translation `messages.po` files (the rest is ignored by git).
209 >
210 > After you have finished testing your work, be sure to undo the change to
211 > the `locale` setting in the `mkdocs.yml` file before submitting your
212 > changes.
213
214 ## Updating theme documentation
215
216 The page [Choosing your theme](../user-guide/choosing-your-theme.md) updates by itself with all available locale options.
217
218 ## Contributing translations
219
220 It is now time for you to [contribute](../about/contributing.md) your nice work
221 to the project. Thank you!