From 98249bbbd94a6a1f956bae11b6d602b0537b4656 Mon Sep 17 00:00:00 2001 From: tiyn Date: Sun, 14 Apr 2024 01:58:29 +0200 Subject: [PATCH] added language support for german --- Dockerfile | 10 ++ README.md | 3 + src/app.py | 9 +- src/config.py | 3 + src/content.py | 268 ++++++++++++++++++------------------ src/templates/archive.html | 2 +- src/templates/error.html | 2 +- src/templates/index.html | 2 +- src/templates/rss.xml | 2 +- src/templates/template.html | 2 +- 10 files changed, 161 insertions(+), 142 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5effb65..1dd726f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,16 @@ WORKDIR /blog RUN pip3 install -r requirements.txt +RUN apt-get update && \ + apt-get install -y locales && \ + sed -i -e 's/# de_DE.UTF-8 UTF-8/de_DE.UTF-8 UTF-8/' /etc/locale.gen && \ + sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \ + dpkg-reconfigure --frontend=noninteractive locales + +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + VOLUME /blog/templates/entry VOLUME /blog/static/graphics diff --git a/README.md b/README.md index 3ebadd2..93a49e0 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ via plain text files. - [x] Switchable CSS - [x] CSS dark-theme - [x] CSS light-theme +- [x] Language Support + - [x] English + - [x] German - [x] Config file - [x] Docker installation - [x] Logo diff --git a/src/app.py b/src/app.py index 8d947bf..bc1e48c 100644 --- a/src/app.py +++ b/src/app.py @@ -8,33 +8,34 @@ app = Flask(__name__) TITLE = config.TITLE STYLE = config.STYLE +LANGUAGE = config.LANGUAGE DESCRIPTION = config.DESCRIPTION WEBSITE = config.WEBSITE @app.errorhandler(404) def page_not_found(e): - return render_template("error.html", title=TITLE, errorcode="404", style=STYLE), 404 + return render_template("error.html", title=TITLE, errorcode="404", style=STYLE, language=LANGUAGE), 404 @app.route("/") @app.route("/index.html") def index(): content = con_gen.gen_index_string() - return render_template("index.html", title=TITLE, content_string=content, style=STYLE) + return render_template("index.html", title=TITLE, content_string=content, style=STYLE, language=LANGUAGE) @app.route("/archive") @app.route("/archive.html") def archive(): content = con_gen.gen_arch_string() - return render_template("archive.html", title=TITLE, content_string=content, style=STYLE) + return render_template("archive.html", title=TITLE, content_string=content, style=STYLE, language=LANGUAGE) @app.route("/entry/") def entry(path): content = con_gen.gen_stand_string(path) if content != "": - return render_template("standalone.html", title=TITLE, content_string=content, style=STYLE) + return render_template("standalone.html", title=TITLE, content_string=content, style=STYLE, language=LANGUAGE) abort(404) diff --git a/src/config.py b/src/config.py index 049051f..a9add68 100644 --- a/src/config.py +++ b/src/config.py @@ -9,3 +9,6 @@ WEBSITE = "localhost:5000" # Theme for the blog: dark, light STYLE = "dark" + +# Language for the titles: en-us or de-de +LANGUAGE = "en-us" diff --git a/src/content.py b/src/content.py index 90c64fd..e39410f 100644 --- a/src/content.py +++ b/src/content.py @@ -1,97 +1,101 @@ -from datetime import datetime -import markdown +import locale import os -from os import path import pathlib +from datetime import datetime +from os import path import config +import markdown ENTRY_DIR = "templates/entry" +LANGUAGE = config.LANGUAGE +LOCAL = "de_DE.UTF-8" if LANGUAGE == "de-de" else "en_US.UTF-8" + +locale.setlocale(locale.LC_TIME, LOCAL) + +standalone_str = "Artikel" if LANGUAGE == "de-de" else "standalone" + def gen_arch_string(): - """ + """ Creates and returns a archive string of every file in ENTRY_DIR. Returns: string: html-formatted archive-string """ - path_ex = ENTRY_DIR - if path.exists(path_ex): - name_list = os.listdir(path_ex) - full_list = [os.path.join(path_ex, i) for i in name_list] - contents = sorted(full_list, key=os.path.getctime) - content_string = "" - last_month = "" - for file in reversed(contents): - curr_date = datetime.fromtimestamp( - os.path.getctime(file)).strftime("%Y-%m-%d") - curr_month = datetime.fromtimestamp( - os.path.getctime(file)).strftime("%b %Y") - if curr_month != last_month: - if last_month != "": - content_string += "\n" - content_string += "

" + curr_month + "

\n" - content_string += "\n" - return content_string + path_ex = ENTRY_DIR + if path.exists(path_ex): + name_list = os.listdir(path_ex) + full_list = [os.path.join(path_ex, i) for i in name_list] + contents = sorted(full_list, key=os.path.getctime) + content_string = "" + last_month = "" + for file in reversed(contents): + curr_date = datetime.fromtimestamp(os.path.getctime(file)).strftime("%Y-%m-%d") + curr_month = datetime.fromtimestamp(os.path.getctime(file)).strftime("%B %Y") + if curr_month != last_month: + if last_month != "": + content_string += "\n" + content_string += "

" + curr_month + "

\n" + content_string += "\n" + return content_string def gen_index_string(): - """ + """ Create and returns a string including every file in the ENTRY_DIR as an index. Returns: string: html-formatted index string """ - path_ex = ENTRY_DIR - content_string = "" - if path.exists(path_ex): - name_list = os.listdir(path_ex) - full_list = [os.path.join(path_ex, i) for i in name_list] - contents = sorted(full_list, key=os.path.getctime) - for file in reversed(contents): - filename = pathlib.PurePath(file) - purefile = filename - title = open(filename).readline().rstrip("\n") - text = open(filename).readlines()[1:] - filename = filename.name - if filename[0] != ".": - filename = filename.split(".", 1)[0] - content_string += "
\n" - content_string += "

" + title + "

\n" - content_string += "[" + \ - "standalone" + "]
\n" - if file.endswith(".html"): - for line in text: - content_string += line - content_string += "
" - if file.endswith(".md"): - content_string += gen_md_content(file, 2) - content_string += "" + \ - datetime.fromtimestamp(os.path.getctime( - file)).strftime("%Y-%m-%d") + "" - content_string += "
" - return content_string + path_ex = ENTRY_DIR + content_string = "" + if path.exists(path_ex): + name_list = os.listdir(path_ex) + full_list = [os.path.join(path_ex, i) for i in name_list] + contents = sorted(full_list, key=os.path.getctime) + for file in reversed(contents): + filename = pathlib.PurePath(file) + purefile = filename + title = open(filename).readline().rstrip("\n") + text = open(filename).readlines()[1:] + filename = filename.name + if filename[0] != ".": + filename = filename.split(".", 1)[0] + content_string += "
\n" + content_string += "

" + content_string += "" + \ + title + "" +"

\n" + content_string += "" + \ + datetime.fromtimestamp(os.path.getctime( + file)).strftime("%Y-%m-%d") + "

" + if file.endswith(".html"): + for line in text: + content_string += line + content_string += "
" + if file.endswith(".md"): + content_string += gen_md_content(file, 2) + content_string += "
" + return content_string def gen_stand_string(path_ex): - """ + """ Creates a html-string for a file. If the file is markdown it will convert it. This functions ensures upscaling for future formats. @@ -102,29 +106,29 @@ def gen_stand_string(path_ex): Returns: string: html-formatted string string equivalent to the file """ - filename = os.path.join(ENTRY_DIR, path_ex) - content_string = "" - if path.exists(filename): - title = open(filename).readline().rstrip("\n") - text = open(filename).readlines()[1:] - filename_no_end = filename.split(".", 1)[0] - filename_no_end = filename_no_end.split("/")[-1] - content_string += "

" + title + "

\n" - content_string += "[" - content_string += "" + "link" + "" - content_string += "]
\n" - if filename.endswith(".html"): - for line in text: - content_string += line - content_string += "
" - if filename.endswith(".md"): - content_string += gen_md_content(filename, 1) - return content_string + filename = os.path.join(ENTRY_DIR, path_ex) + content_string = "" + if path.exists(filename): + title = open(filename).readline().rstrip("\n") + text = open(filename).readlines()[1:] + curr_date = datetime.fromtimestamp(os.path.getctime(filename)).strftime("%Y-%m-%d") + filename_no_end = filename.split(".", 1)[0] + filename_no_end = filename_no_end.split("/")[-1] + content_string += "

" + title + "

\n" + content_string += "" + curr_date + "" + content_string += "
\n" + if filename.endswith(".html"): + for line in text: + content_string += line + content_string += "
" + if filename.endswith(".md"): + content_string += gen_md_content(filename, 1) + return content_string def gen_md_content(path_ex, depth): - """ + """ Convert a markdown file to a html string. Parameters: @@ -134,54 +138,52 @@ def gen_md_content(path_ex, depth): Returns: string: html-formatted string string equivalent to the markdown file """ - content_string = "" - if path.exists(path_ex): - filename = path_ex.split(".", 1) - fileend = filename[len(filename) - 1] - header = "#" - for i in range(depth): - header += "#" - header += " " - markdown_lines = open(path_ex, "r").readlines()[1:] - markdown_text = "" - for line in markdown_lines: - markdown_text += line.replace("# ", header) - content_string = markdown.markdown( - markdown_text, extensions=["fenced_code", "tables"] - ) - return content_string + content_string = "" + if path.exists(path_ex): + filename = path_ex.split(".", 1) + fileend = filename[len(filename) - 1] + header = "#" + for i in range(depth): + header += "#" + header += " " + markdown_lines = open(path_ex, "r").readlines()[1:] + markdown_text = "" + for line in markdown_lines: + markdown_text += line.replace("# ", header) + content_string = markdown.markdown(markdown_text, extensions=["fenced_code", "tables"]) + return content_string def get_rss_string(): - """ + """ Create a rss-string of the blog and return it. Returns: string: rss-string of everything that is in the ENTRY_DIR. """ - path_ex = ENTRY_DIR - if path.exists(path_ex): - name_list = os.listdir(path_ex) - full_list = [os.path.join(path_ex, i) for i in name_list] - contents = sorted(full_list, key=os.path.getctime) - content_string = "" - for file in reversed(contents): - filename = pathlib.PurePath(file) - title = open(filename).readline().rstrip("\n") - text = open(filename).readlines()[1:] - filename = filename.name - if filename[0] != ".": - filename = filename.split(".", 1)[0] - content_string += "\n" - content_string += "" + title + "\n" - content_string += "" + config.WEBSITE + \ - "/index.html#" + filename + "\n" - content_string += "" + \ - datetime.fromtimestamp(os.path.getctime(file)).strftime( - "%Y-%m-%d") + "\n" - content_string += "" - for line in text: - content_string += line - content_string += "\n" - content_string += "\n" - return content_string + path_ex = ENTRY_DIR + if path.exists(path_ex): + name_list = os.listdir(path_ex) + full_list = [os.path.join(path_ex, i) for i in name_list] + contents = sorted(full_list, key=os.path.getctime) + content_string = "" + for file in reversed(contents): + filename = pathlib.PurePath(file) + title = open(filename).readline().rstrip("\n") + text = open(filename).readlines()[1:] + filename = filename.name + if filename[0] != ".": + filename = filename.split(".", 1)[0] + content_string += "\n" + content_string += "" + title + "\n" + content_string += "" + config.WEBSITE + \ + "/index.html#" + filename + "\n" + content_string += "" + \ + datetime.fromtimestamp(os.path.getctime(file)).strftime( + "%Y-%m-%d") + "\n" + content_string += "" + for line in text: + content_string += line + content_string += "\n" + content_string += "\n" + return content_string diff --git a/src/templates/archive.html b/src/templates/archive.html index bd3f2ae..c0b6bd8 100644 --- a/src/templates/archive.html +++ b/src/templates/archive.html @@ -2,7 +2,7 @@ {% block content %}
-

Archive


+

{% if language=="de-de" %}Archiv{% else %}Archive{% endif %}


{% autoescape off %} {{ content_string }} {% endautoescape %} diff --git a/src/templates/error.html b/src/templates/error.html index 84571ce..f03923e 100644 --- a/src/templates/error.html +++ b/src/templates/error.html @@ -2,7 +2,7 @@ {% block content %}
- Error
+ {% if language=="de-de" %}Fehler{% else %}Error{% endif %}
{{ errorcode }}
diff --git a/src/templates/index.html b/src/templates/index.html index db050ab..16399e4 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -2,7 +2,7 @@ {% block content %}
-

Index


+

Feed


{% autoescape off %} {{ content_string }} {% endautoescape %} diff --git a/src/templates/rss.xml b/src/templates/rss.xml index 9f8b6b1..9877aef 100644 --- a/src/templates/rss.xml +++ b/src/templates/rss.xml @@ -4,7 +4,7 @@ {{ title }} {{ description }} -en-us + {{ language }} {{ website }}/feed.xml diff --git a/src/templates/template.html b/src/templates/template.html index 77d4d05..e7e8db9 100644 --- a/src/templates/template.html +++ b/src/templates/template.html @@ -14,7 +14,7 @@