-
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 all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[python: **.py] | ||
[jinja2: **/templates/**.html] | ||
extensions=jinja2.ext.i18n,jinja2.ext.autoescape,jinja2.ext.with_ |
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
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,89 @@ | ||
import gettext | ||
import os | ||
from pathlib import Path | ||
from typing import Any, Generator | ||
|
||
from app import config | ||
from app.dependencies import templates | ||
|
||
LANGUAGE_DIR = "app/locales" | ||
LANGUAGE_DIR_TEST = "../app/locales" | ||
TRANSLATION_FILE = "base" | ||
|
||
|
||
def set_ui_language(language: str = None) -> None: | ||
"""Set the gettext translations to a given language. | ||
If the language requested is not supported, the translations default | ||
to the value of config.WEBSITE_LANGUAGE. | ||
|
||
Args: | ||
language (str, optional): a valid language code that follows RFC 1766. | ||
Defaults to None. | ||
See also the Language Code Identifier (LCID) Reference for a list of | ||
valid language codes. | ||
|
||
.. _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 # noqa: E501 | ||
""" | ||
|
||
# TODO: Connect when user registration is completed. | ||
# if not language: | ||
# language = _get_display_language(user_id: int) | ||
|
||
language_dir = _get_language_directory() | ||
|
||
if language not in _get_supported_languages(language_dir): | ||
language = config.WEBSITE_LANGUAGE | ||
|
||
translations = gettext.translation(TRANSLATION_FILE, | ||
localedir=language_dir, | ||
languages=[language]) | ||
translations.install() | ||
templates.env.install_gettext_translations(translations, newstyle=True) | ||
|
||
|
||
# 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 _get_language_directory() -> str: | ||
"""Get and return the language directory relative path. | ||
|
||
Returns: | ||
str: the language directory relative path. | ||
""" | ||
language_dir = LANGUAGE_DIR | ||
if Path(LANGUAGE_DIR_TEST).is_dir(): | ||
# If running from test, change dir path. | ||
language_dir = LANGUAGE_DIR_TEST | ||
return language_dir | ||
|
||
|
||
def _get_supported_languages(language_dir: str = None) -> \ | ||
Generator[str, Any, None]: | ||
"""Get and return a generator of supported translation languages codes. | ||
|
||
Args: | ||
language_dir (str, optional): the path of the language directory. | ||
Defaults to None. | ||
|
||
Returns: | ||
Generator[str, Any, None]: a generator expression of supported | ||
translation languages codes. | ||
""" | ||
|
||
if not language_dir: | ||
language_dir = _get_language_directory() | ||
|
||
return (language.name for language in | ||
[Path(f.path) for f in os.scandir(language_dir) if f.is_dir()]) |
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,124 @@ | ||
# Translations template for PROJECT. | ||
# Copyright (C) 2021 ORGANIZATION | ||
# This file is distributed under the same license as the PROJECT project. | ||
# FIRST AUTHOR <EMAIL@ADDRESS>, 2021. | ||
# | ||
#, fuzzy | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PROJECT VERSION\n" | ||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" | ||
"POT-Creation-Date: 2021-01-26 21:30+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <LL@li.org>\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=utf-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Generated-By: Babel 2.9.0\n" | ||
|
||
#: app/routers/profile.py:19 | ||
msgid "Not found" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:11 | ||
msgid "From" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:13 | ||
msgid "to" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:33 | ||
msgid "Start date is greater than end date" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:35 | ||
msgid "No events found..." | ||
msgstr "" | ||
|
||
#: app/templates/base.html:18 | ||
msgid "Calendar" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:26 | ||
msgid "Home" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:29 | ||
msgid "Profile" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:32 | ||
msgid "Sign in" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:35 | ||
msgid "Sign up" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:40 | ||
msgid "Agenda" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:53 | ||
msgid "Update name" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:60 app/templates/profile.html:82 | ||
#: app/templates/profile.html:103 app/templates/profile.html:127 | ||
msgid "Save changes" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:74 | ||
msgid "Update email" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:95 | ||
msgid "Update description" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:118 | ||
msgid "Update photo" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:144 | ||
msgid "Settings" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:161 | ||
msgid "Features" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:165 | ||
msgid "Export my calendar" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:168 app/templates/profile.html:171 | ||
msgid "Your feature" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:192 | ||
msgid "Upcoming event on (date)" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:203 | ||
msgid "The Event (event)" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:206 | ||
msgid "Last updated (time) ago" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:223 | ||
msgid "Explore MeetUps near you" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:232 app/templates/profile.html:241 | ||
msgid "Your Card" | ||
msgstr "" | ||
|
||
#: tests/test_language.py:39 | ||
msgid "test python translation" | ||
msgstr "" | ||
|
Binary file not shown.
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,125 @@ | ||
# English translations for PROJECT. | ||
# Copyright (C) 2021 ORGANIZATION | ||
# This file is distributed under the same license as the PROJECT project. | ||
# FIRST AUTHOR <EMAIL@ADDRESS>, 2021. | ||
# | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PROJECT VERSION\n" | ||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" | ||
"POT-Creation-Date: 2021-01-26 21:30+0200\n" | ||
"PO-Revision-Date: 2021-01-26 21:31+0200\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language: en\n" | ||
"Language-Team: en <LL@li.org>\n" | ||
"Plural-Forms: nplurals=2; plural=(n != 1)\n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=utf-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Generated-By: Babel 2.9.0\n" | ||
|
||
#: app/routers/profile.py:19 | ||
msgid "Not found" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:11 | ||
msgid "From" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:13 | ||
msgid "to" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:33 | ||
msgid "Start date is greater than end date" | ||
msgstr "" | ||
|
||
#: app/templates/agenda.html:35 | ||
msgid "No events found..." | ||
msgstr "" | ||
|
||
#: app/templates/base.html:18 | ||
msgid "Calendar" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:26 | ||
msgid "Home" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:29 | ||
msgid "Profile" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:32 | ||
msgid "Sign in" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:35 | ||
msgid "Sign up" | ||
msgstr "" | ||
|
||
#: app/templates/base.html:40 | ||
msgid "Agenda" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:53 | ||
msgid "Update name" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:60 app/templates/profile.html:82 | ||
#: app/templates/profile.html:103 app/templates/profile.html:127 | ||
msgid "Save changes" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:74 | ||
msgid "Update email" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:95 | ||
msgid "Update description" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:118 | ||
msgid "Update photo" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:144 | ||
msgid "Settings" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:161 | ||
msgid "Features" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:165 | ||
msgid "Export my calendar" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:168 app/templates/profile.html:171 | ||
msgid "Your feature" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:192 | ||
msgid "Upcoming event on (date)" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:203 | ||
msgid "The Event (event)" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:206 | ||
msgid "Last updated (time) ago" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:223 | ||
msgid "Explore MeetUps near you" | ||
msgstr "" | ||
|
||
#: app/templates/profile.html:232 app/templates/profile.html:241 | ||
msgid "Your Card" | ||
msgstr "" | ||
|
||
#: tests/test_language.py:39 | ||
msgid "test python translation" | ||
msgstr "" | ||
|
Binary file not shown.
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.