From 6478aa2056d372d2a41611be657f496c337ca424 Mon Sep 17 00:00:00 2001
From: miolruoh <miikk@Miikka-PC>
Date: Sun, 6 Feb 2022 15:47:16 +0000
Subject: [PATCH] Modified translations

---
 Dockerfile         |  3 +++
 dogmap/__init__.py | 17 ++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/Dockerfile b/Dockerfile
index 917fee54..f7c8f6b5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -17,6 +17,9 @@ COPY dogmap dogmap
 
 COPY tests tests
 
+COPY babel.cfg .
+RUN pybabel extract -F babel.cfg -o messages.pot . && pybabel update -i messages.pot -d dogmap/translations
+
 ## Entry point for running our app
 ENV FLASK_APP="dogmap"
 
diff --git a/dogmap/__init__.py b/dogmap/__init__.py
index b16c6a12..d36ea027 100644
--- a/dogmap/__init__.py
+++ b/dogmap/__init__.py
@@ -63,13 +63,18 @@ def create_app() -> Flask:
     if mongo_url:
         app.config['MONGODB_SETTINGS'] = {'host': mongo_url}
 
+    babel_default_location = environ.get('BABEL_DEFAULT_LOCALE', 'fi')
+    if babel_default_location:
+        app.config['BABEL_DEFAULT_LOCALE'] = babel_default_location
+
     return app
 
 
 load_dotenv()
 app = create_app()
 
-babel = Babel(app, default_locale=environ.get('BABEL_DEFAULT_LOCALE'))
+# For translations
+babel = Babel(app)
 
 # Integrate migrations for database
 migrations.init_app(app)
@@ -91,6 +96,16 @@ class Page(TypedDict):
     title: str
     details: str
 
+@babel.localeselector
+def get_locale():
+    """
+    Setup default language for user based on their webbrowser preference
+    """
+    # try to guess the language from the user accept
+    # header the browser transmits. The best match wins.
+    locale = request.accept_languages.best_match(['fi', 'en'])
+    return locale
+
 
 def _generate_marker(loc: Location) -> Dict[str, Any]:
     """
-- 
GitLab