Updated documentation generation, now translations should work again

This commit is contained in:
Manuel Cortez 2021-12-31 18:21:44 -06:00
parent 99279a8dab
commit cf68b524a8
No known key found for this signature in database
GPG Key ID: 262CC30FA01B5CBF
3 changed files with 71 additions and 48 deletions

View File

@ -1,4 +1,4 @@
# Changelog Changelog
## News in this version ## News in this version

View File

@ -4,59 +4,82 @@ import os
import locale import locale
import markdown import markdown
import shutil import shutil
from codecs import open as _open
from importlib import reload from importlib import reload
def change_language(name, language): # Languages already translated or translating the documentation.
global _ documentation_languages = ["ru", "en", "es"]
os.environ["lang"] = language
_ = gettext.install(name, os.path.join(os.getcwd(), "locales"))
languages = ["en", "ru"] # Changelog translated languages.
changelog_languages = ["ru", "en", "es"]
def generate_document(language, document_type="documentation"): # this function will help us to have both strings.py and changelog.py without issues by installing a global dummy translation function.
translation_file = "socializer-documentation" def install_null_translation(name):
change_language(translation_file, language) _ = gettext.NullTranslations()
if document_type == "documentation": _.install()
reload(strings) return
markdown_file = markdown.markdown("\n".join(strings.documentation[1:]), extensions=["markdown.extensions.toc"])
title = strings.documentation[0][1:] def get_translations(name):
filename = "manual.html" """ Create translation instances for every language of the translated document. """
elif document_type == "changelog": translations = {}
reload(changelog) if "documentation" in name:
markdown_file = markdown.markdown("\n".join(changelog.documentation[1:]), extensions=["markdown.extensions.toc"]) langs = documentation_languages
title = changelog.documentation[0][1:] else:
filename = "changelog.html" langs = changelog_languages
first_html_block = """<!doctype html> for l in langs:
<html lang="%s"> if l != "en":
<head> _ = gettext.translation(name, os.path.join(os.getcwd(), "locales"), languages=[l])
<title>%s</title> translations[l] = _
<meta charset="utf-8"> else:
</head> _ = gettext.NullTranslations()
<body> translations[l] = _
<header><h1>%s</h1></header> return translations
""" % (language, title, title)
first_html_block = first_html_block+ markdown_file def generate_document(lang, lang_name, document_type="documentation"):
first_html_block = first_html_block + "\n</body>\n</html>" """ Generates a document by using the provided lang object, which should be a translation, and lang_name, which should be the two letter code representing the language. """
if not os.path.exists(os.path.join("documentation", language)): if document_type == "documentation":
os.mkdir(os.path.join("documentation", language)) translation_file = "socializer-documentation"
mdfile = _open(os.path.join("documentation", language, filename), "w", encoding="utf-8") markdown_file = markdown.markdown("\n".join([lang.gettext(s) if s != "" else s for s in strings.documentation[1:]]), extensions=["markdown.extensions.toc"])
mdfile.write(first_html_block) title = lang.gettext(strings.documentation[0])
mdfile.close() filename = "manual.html"
elif document_type == "changelog":
translation_file = "socializer-documentation"
markdown_file = markdown.markdown("\n".join([lang.gettext(s) if s != "" else s for s in changelog.documentation[1:]]), extensions=["markdown.extensions.toc"])
title = lang.gettext(changelog.documentation[0])
filename = "changelog.html"
first_html_block = """<!doctype html>
<html lang="%s">
<head>
<title>%s</title>
<meta charset="utf-8">
</head>
<body>
<header><h1>%s</h1></header>
""" % (lang_name, title, title)
first_html_block = first_html_block+ markdown_file
first_html_block = first_html_block + "\n</body>\n</html>"
if not os.path.exists(os.path.join("documentation", lang_name)):
os.mkdir(os.path.join("documentation", lang_name))
mdfile = open(os.path.join("documentation", lang_name, filename), "w", encoding="utf-8")
mdfile.write(first_html_block)
mdfile.close()
def create_documentation(): def create_documentation():
print("Creating documentation in the supported languages...\n") changelog_translations = get_translations("socializer-documentation")
if not os.path.exists("documentation"): documentation_translations = get_translations("socializer-documentation")
os.mkdir("documentation") print("Creating documentation in the supported languages...\n")
if os.path.exists(os.path.join("documentation", "license.txt")) == False: if not os.path.exists("documentation"):
shutil.copy(os.path.join("..", "license.txt"), os.path.join("documentation", "license.txt")) os.mkdir("documentation")
for i in languages: if os.path.exists(os.path.join("documentation", "license.txt")) == False:
print("Creating documentation for: %s" % (i,)) shutil.copy(os.path.join("..", "license.txt"), os.path.join("documentation", "license.txt"))
generate_document(i) for i in documentation_languages:
generate_document(i, "changelog") print("Creating documentation for: %s" % (i,))
print("Done") generate_document(lang_name=i, lang=documentation_translations.get(i))
for i in changelog_languages:
print("Creating changelog for: %s" % (i,))
generate_document(lang_name=i, lang=changelog_translations.get(i), document_type="changelog")
print("Done")
change_language("socializer-documentation", "en") install_null_translation("twblue-documentation")
import strings import strings
import changelog import changelog
create_documentation() create_documentation()

View File

@ -1,4 +1,4 @@
# Socializer's manual Socializer's manual
## Introduction ## Introduction