-
Notifications
You must be signed in to change notification settings - Fork 52
feat: add i18n support #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
0f8b20c
feat: add i18n support
sMOKIK 3cecc97
fix(i18n): correct flake8 line length errors
sMOKIK 65db418
fix(i18n): attempt to fix build crash at get_translations_dicts() and…
sMOKIK feba1bb
fix(i18n): simplify check of language file path between live and testing
sMOKIK 72f3a2b
fix(i18n): replace glob with path.glob
sMOKIK 8d85279
fix(i18n): revert attempted code improvement
sMOKIK d75fd94
fix(i18n): added missing language_id to placeholder user
sMOKIK 86925ad
fix(i18n): added simple test for home
sMOKIK eaf1107
fix(i18n): code review fixes
sMOKIK b69042d
fix(i18n): split the html result dict into two sub-dicts, "variables"…
sMOKIK abc7641
fix(i18n): fixed CR request to not use global state for translations
sMOKIK 6f5728d
fix(i18n): add test to complete code coverage
sMOKIK e1caaa4
fix(i18n): add missing documentation
sMOKIK ade7c25
fix(i18n): i18n v2.0
sMOKIK ae49fce
fix(i18n): temporarily disable F821 errors on built-in _() function
sMOKIK c6a0c3b
fix(i18n): Change function name
sMOKIK 4bf46ee
fix(i18n): change function name
sMOKIK c00d714
fix(i18n): add test with no language code and with invalid arguments
sMOKIK 92b585b
fix(i18n): html and documentation fixes
sMOKIK f4861a8
fix(i18n): add missing documentation fixes from previous commit
sMOKIK 80da4a1
fix(i18n): move 'jinja2.ext.i18n' extension setting to dependencies.py
sMOKIK 0e7d2e7
fix(i18n): code review fixes
sMOKIK b0a1358
fix(i18n): lint error line length
sMOKIK 5a4e5d3
fix(i18n): hopefully fix code coverage
sMOKIK 46d078e
fix(i18n): small doc fix
sMOKIK b1d7d30
fix(i18n): testing if this fix works
sMOKIK a34d3ce
fix(i18n): another fix test
sMOKIK f96f221
fix(i18n): remove test, didn't work
sMOKIK 5784a09
fix(i18n): try another method of monkeypatch
sMOKIK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| from functools import lru_cache | ||
| import glob | ||
| import json | ||
| from pathlib import PureWindowsPath | ||
| from typing import Dict, Union | ||
|
|
||
| from app import config | ||
|
|
||
| LANGUAGE_FILES_PATH = "app/languages/*.json" | ||
| LANGUAGE_FILES_PATH_TEST = "../app/languages/*.json" | ||
|
|
||
|
|
||
| @lru_cache() | ||
| def get_translation_words(display_language: str = None) -> \ | ||
| Dict[str, Union[str, Dict[str, str]]]: | ||
| """Gets and returns the translation words for a given language. | ||
| The returned object is a dictionary of the translated words in either | ||
| the user's language setting, or the default app setting. | ||
|
|
||
| Using the @lru_cache() decorator makes the function return the same | ||
| translation for a given language that was previously used, instead of | ||
| computing it again, executing the code of the function every time. | ||
|
|
||
| Args: | ||
| display_language (str): a valid code that follows RFC 1766. | ||
| See also the Language Code Identifier (LCID) Reference for a list of | ||
| valid codes. | ||
|
|
||
| Returns: | ||
| Dict[str, Union[str, Dict[str, str]]]: a dictionary of string keys and | ||
| their translation as their values. The value can either be a string, | ||
| or a nested dictionary for plural translations. | ||
|
|
||
| .. _RFC 1766: | ||
| https://tools.ietf.org/html/rfc1766.html | ||
|
|
||
| .. _Language Code Identifier (LCID) Reference: | ||
| https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c?redirectedfrom=MSDN # noqa: E501 | ||
| """ | ||
|
|
||
| if display_language: | ||
| return _populate_with_language(display_language) | ||
| else: | ||
| # TODO: Waiting for user registration. Restore when done. | ||
| # display_language = _get_display_language(user_id) | ||
| # return populate_with_language(display_language) | ||
| return _populate_with_language(config.WEBSITE_LANGUAGE) | ||
|
|
||
|
|
||
| # TODO: Waiting for user registration. Add doc. | ||
| # def _get_display_language(user_id: int) -> str: | ||
| # # TODO: handle user language setting: | ||
| # # If user is logged in, get language setting. | ||
| # # If user is not logged in, get default site setting. | ||
| # | ||
| # if db_user: | ||
| # return db_user.language | ||
| # return config.WEBSITE_LANGUAGE | ||
|
|
||
|
|
||
| def _populate_with_language(display_language: str) -> \ | ||
| Dict[str, Union[str, Dict[str, str]]]: | ||
| """Updates the translation_words to the requested language. | ||
| If the language code is not supported by the applications, the dictionary | ||
| defaults to the config.WEBSITE_LANGUAGE setting. | ||
|
|
||
| Args: | ||
| display_language (str): a valid code that follows RFC 1766. | ||
| See also the Language Code Identifier (LCID) Reference for a list of | ||
| valid codes. | ||
|
|
||
| Returns: | ||
| Dict[str, Union[str, Dict[str, str]]]: a dictionary of string keys and | ||
| their translation as their values. The value can either be a string, | ||
| or a nested dictionary for plural translations. | ||
| """ | ||
| translation_words_all_languages = _get_translation_words_all_languages() | ||
| if display_language in translation_words_all_languages: | ||
| return translation_words_all_languages[display_language] | ||
| return translation_words_all_languages[config.WEBSITE_LANGUAGE] | ||
|
|
||
|
|
||
| def _get_translation_words_all_languages() -> \ | ||
| Dict[str, Dict[str, Union[str, Dict[str, str]]]]: | ||
| """Gets and returns a dictionary of nested language dictionaries from | ||
| the language translation files. | ||
|
|
||
| Returns: | ||
| Dict[str, Dict[str, Union[str, Dict[str, str]]]]: a dictionary of | ||
| language codes as string keys, and nested dictionaries of translations | ||
| as their values. | ||
| """ | ||
| language_translations = {} | ||
| language_files = (glob.glob(LANGUAGE_FILES_PATH) | ||
| or glob.glob(LANGUAGE_FILES_PATH_TEST)) | ||
| for language_file in language_files: | ||
| language_code = PureWindowsPath(language_file).stem | ||
| with open(language_file, 'r', encoding='utf8') as file: | ||
| language_translations[language_code] = json.load(file) | ||
| return language_translations |
This file contains hidden or 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,30 @@ | ||
| from typing import Any, Dict | ||
|
|
||
| from fastapi import Request | ||
| from fastapi.templating import Jinja2Templates | ||
|
|
||
| from app.dependencies import templates | ||
| from app.internal import languages | ||
|
|
||
|
|
||
| def get_template_response(html_file_name: str, request: Request, | ||
| variables: Dict[str, Any] = None) -> Jinja2Templates: | ||
| """Creates and returns a Jinja2Templates object with a result dictionary | ||
| containing three parts: the request object, a variables dictionary, | ||
| and a translation dictionary. | ||
|
|
||
| Args: | ||
| html_file_name (str): the name of the html file. | ||
| request (Request): a FastApi Request object. | ||
| variables (Dict[str, Any]): an optional variables dictionary used | ||
| in the html file. | ||
|
|
||
| Returns: | ||
| Jinja2Templates: a Jinja2Templates response object. | ||
| """ | ||
| translations = languages.get_translation_words() | ||
| result = {"request": request, | ||
| "variables": variables, | ||
| "localized": translations, | ||
| } | ||
| return templates.TemplateResponse(html_file_name, result) |
This file contains hidden or 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,31 @@ | ||
| { | ||
| "website_title": "Calendar", | ||
| "calendar_button": "Calendar", | ||
| "home_button": "Home", | ||
| "profile_button": "Profile", | ||
| "sign_in_button": "Sign In", | ||
| "sign_up_button": "Sign Up", | ||
| "agenda_button": "Agenda", | ||
| "edit_full_name_button": "Edit full name", | ||
| "edit_email_button": "Edit email", | ||
| "edit_photo_button": "Edit photo", | ||
| "edit_description_button": "Edit description", | ||
| "update_full_name_title": "Update full name", | ||
| "update_email_title": "Update email", | ||
| "update_photo_title": "Upload photo", | ||
| "update_description_title": "Update description", | ||
| "save_changes_button": "Save changes", | ||
| "settings_button": "Settings", | ||
| "features_title": "Features", | ||
| "export_calendar_button": "Export my calendar", | ||
| "upcoming_event_on_date": "Upcoming event on {date}", | ||
| "from_button": "From", | ||
| "to_button": "to", | ||
| "get_agenda_button": "Get Agenda", | ||
| "agenda_today": "Today", | ||
| "agenda_next_week": "Next week", | ||
| "agenda_next_month": "Next month", | ||
| "agenda_invalid_date": "Start date is greater than end date", | ||
| "no_events_found": "No events found...", | ||
| "test_word": "test" | ||
| } |
This file contains hidden or 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,31 @@ | ||
| { | ||
| "website_title": "Calendar", | ||
| "calendar_button": "Calendar", | ||
| "home_button": "Home", | ||
| "profile_button": "פרופיל", | ||
| "sign_in_button": "Sign In", | ||
| "sign_up_button": "Sign Up", | ||
| "agenda_button": "Agenda", | ||
| "edit_full_name_button": "ערוך שם מלא", | ||
| "edit_email_button": "ערוך אימייל", | ||
| "edit_photo_button": "ערוך תמונה", | ||
| "edit_description_button": "ערוך תיאור", | ||
| "update_full_name_title": "עדכן שם מלא", | ||
| "update_email_title": "עדכן אימייל", | ||
| "update_photo_title": "עדכן תמונה", | ||
| "update_description_title": "עדכן תיאור", | ||
| "save_changes_button": "שמור שינויים", | ||
| "settings_button": "הגדרות", | ||
| "features_title": "Features", | ||
| "export_calendar_button": "Export my calendar", | ||
| "upcoming_event_on_date": "Upcoming event on {date}", | ||
| "from_button": "מ", | ||
| "to_button": "עד", | ||
| "get_agenda_button": "Get Agenda", | ||
| "agenda_today": "היום", | ||
| "agenda_next_week": "שבוע הבא", | ||
| "agenda_next_month": "חודש הבא", | ||
| "agenda_invalid_date": "Start date is greater than end date", | ||
| "no_events_found": "לא נמצאו אירועים...", | ||
| "test_word": "בדיקה" | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.