Skip to content

Commit

Permalink
fix( #80 ): contextProcessor and beforeRequest now works general
Browse files Browse the repository at this point in the history
  • Loading branch information
DogukanUrker committed Jul 16, 2024
1 parent 29975e4 commit 8f7b907
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 63 deletions.
9 changes: 8 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
isLogin, # A function that checks LOG_IN constant
recaptchaBadge, # A function that checks RECAPTCHA_BADGE constant
isRegistration, # A function that checks REGISTRATION constant
browserLanguage, # A function that sets the app language based on the browser's preferred language
injectTranslations, # A function that injects translations into the context of the application
returnUserProfilePicture, # A function that returns the user's profile picture
)

Expand Down Expand Up @@ -225,7 +227,12 @@
app.context_processor(
returnUserProfilePicture
) # A context processor that adds the getProfilePicture variable to the template context

app.context_processor(
injectTranslations
) # A context processor that adds the translations variable to the template context
app.before_request(
browserLanguage
) # A function that sets the app language based on the browser's preferred language

# Match WERKZEUG_LOGGER status
match WERKZEUG_LOGGER:
Expand Down
6 changes: 6 additions & 0 deletions modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
# Importing the 'recaptchaBadge' context processor from the 'utils.contextProcessor.recaptchaBadge' module
from utils.contextProcessor.recaptchaBadge import recaptchaBadge

# Importing the 'injectTranslations' function from the 'utils.contextProcessor.translations' module
from utils.contextProcessor.translations import injectTranslations

# Importing the 'browserLanguage' function from the 'utils.beforeRequest.browserLanguage' module
from utils.beforeRequest.browserLanguage import browserLanguage

# Importing the 'returnUserProfilePicture' context processor from the 'utils.contextProcessor.returnUserProfilePicture' module
from utils.contextProcessor.returnUserProfilePicture import returnUserProfilePicture

Expand Down
62 changes: 0 additions & 62 deletions routes/setLanguage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,6 @@
) # Pass the name of the blueprint and the current module name as arguments


@setLanguageBlueprint.context_processor
def injectTranslations():
"""
Inject translations into the context of the application.
Parameters:
None
Returns:
dict: A dictionary containing the translations for the current language.
"""
language = session.get(
"language", "en"
) # Get the current language from the session
translations = loadTranslations(
language
) # Load the translations for the current language
Log.info(f"Injecting translations for language: {language}")
return dict(translations=translations) # Return the translations as a dictionary


@setLanguageBlueprint.route("/setLanguage/<language>")
def setLanguage(language):
"""
Expand All @@ -59,47 +38,6 @@ def setLanguage(language):
return language


@setLanguageBlueprint.before_request
def beforeRequest():
"""
Set the user's language based on the browser's preferred language.
Parameters:
None
Returns:
None
"""
if "language" not in session:
# Check if the user's language is not already set in the session
browserLanguage = request.headers.get(
"Accept-Language"
) # Get the browser's Accept-Language header
if browserLanguage:
# If the Accept-Language header is present, parse the first preferred language
browserLanguage = browserLanguage.split(",")[0].split("-")[0]
match browserLanguage:
case lang if lang in LANGUAGES:
session["language"] = (
lang # Set the session language to the browser's preferred language
)
Log.info(f"Browser language detected and set to: {lang}")
case _:
session["language"] = (
"en" # Default to English if the preferred language is not supported
)
Log.warning(
f"Browser language '{browserLanguage}' not supported. Defaulting to English."
)
else:
session["language"] = (
"en" # Default to English if the Accept-Language header is not present
)
Log.warning("No browser language detected. Defaulting to English.")
else:
Log.info(f"Language already set in session: {session['language']}")


@setLanguageBlueprint.route("/changeLanguage")
def changeLanguage():
"""
Expand Down
41 changes: 41 additions & 0 deletions utils/beforeRequest/browserLanguage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from modules import LANGUAGES, session, request, Log # Import necessary modules


def browserLanguage():
"""
Set the user's language based on the browser's preferred language.
Parameters:
None
Returns:
None
"""
if "language" not in session:
# Check if the user's language is not already set in the session
browserLanguage = request.headers.get(
"Accept-Language"
) # Get the browser's Accept-Language header
if browserLanguage:
# If the Accept-Language header is present, parse the first preferred language
browserLanguage = browserLanguage.split(",")[0].split("-")[0]
match browserLanguage:
case lang if lang in LANGUAGES:
session["language"] = (
lang # Set the session language to the browser's preferred language
)
Log.info(f"Browser language detected and set to: {lang}")
case _:
session["language"] = (
"en" # Default to English if the preferred language is not supported
)
Log.warning(
f"Browser language '{browserLanguage}' not supported. Defaulting to English."
)
else:
session["language"] = (
"en" # Default to English if the Accept-Language header is not present
)
Log.warning("No browser language detected. Defaulting to English.")
else:
Log.info(f"Language already set in session: {session['language']}")
24 changes: 24 additions & 0 deletions utils/contextProcessor/translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from modules import session, Log # Import the session and Log from the modules file
from ..translations import (
loadTranslations,
) # Load translations for the current language


def injectTranslations():
"""
Inject translations into the context of the application.
Parameters:
None
Returns:
dict: A dictionary containing the translations for the current language.
"""
language = session.get(
"language", "en"
) # Get the current language from the session
translations = loadTranslations(
language
) # Load the translations for the current language
Log.info(f"Injecting translations for language: {language}")
return dict(translations=translations) # Return the translations as a dictionary

0 comments on commit 8f7b907

Please sign in to comment.