toolforge_i18n documentation
toolforge_i18n is a library for making Wikimedia Toolforge tools written in Python translatable. It’s especially geared towards Flask based tools – if your tool uses Flask, please see Using toolforge_i18n in a Flask tool. If your tool does not use Flask, see Using toolforge_i18n in a non-Flask tool. You can also review the API Reference at your convenience.
Features
Make your tool translatable into dozens, potentially hundreds of languages!
Easy integration with translatewiki.net by reusing MediaWiki message file syntax.
Full support for the magic words
{{GENDER:}}
and{{PLURAL:}}
, as well as for hyperlink syntax ([url text]
) and list formatting.By default, support for a MediaWiki-like
?uselang=
URL parameter, including?uselang=qqx
to see message keys.Correct conversion between MediaWiki language codes and HTML language codes / IETF BCP 47 language tags; for instance,
?uselang=simple
produces<html lang="en-simple">
.Correct
lang=
anddir=
in the face of language fallback: messages that (due to language fallback) don’t match the surrounding markup are automatically wrapped in a<span>
with the right attributes. (Even MediaWiki doesn’t do this! Though, admittedly, MediaWiki doesn’t have the luxury of assuming that every message can be wrapped in a<span>
– many MediaWiki messages are block elements that would rather need a<div>
.)Includes checks to ensure all translations are safe, without unexpected elements (e.g.
<script>
) or attributes (e.g.onclick=
), to protect against XSS attacks from translations. The tests are automatically registered via a pytest plugin and also run at tool initialization time.
Contents:
- API Reference
CommaSeparatedListFormatter
GenderFormatter
HyperlinkFormatter
I18nFormatter
PluralFormatter
ToolforgeI18n
TranslationsConfig
UnknownMessageWarning
add_lang_if_needed()
get_gender_by_user_name()
get_user_agent()
interface_language_code_from_request()
lang_autonym()
lang_bcp47_to_mw()
lang_dir()
lang_fallbacks()
lang_mw_to_bcp47()
language_code_to_babel()
load_translations()
message()
pop_html_lang()
push_html_lang()
set_user_agent()
- Using toolforge_i18n in a Flask tool
- Using toolforge_i18n in a non-Flask tool