-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix( #80 ): contextProcessor and beforeRequest now works general
- Loading branch information
1 parent
29975e4
commit 8f7b907
Showing
5 changed files
with
79 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |