Skip to content
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

feat: Initialize Internationalization #18

Merged
merged 20 commits into from
Aug 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refractor
sumit-158 committed Aug 21, 2022
commit 4f573936d476c8e9247d872deabd827dd91aa986
15 changes: 15 additions & 0 deletions app/i18n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import gettext


class TranslationStore:
def lang(text=str):
text = "fr"
return text

def load():
lang_translations = gettext.translation(
"knowledge-panel", localedir="i18n", languages=[TranslationStore.lang()]
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexgarel Just a question what does stack_info=True do?

Copy link
Member

@alexgarel alexgarel Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it prints the stack trace along with the log message (like for exceptions).
Here I added it, because if I just log the developer won't see where the translation function was called, whereas here he sees the lines.

Eg:

knowledge-panel | Looking up a translation while language was not set. Use active_translation context manager
knowledge-panel | Stack (most recent call last):
knowledge-panel |   File "/usr/local/lib/python3.9/threading.py", line 937, in _bootstrap
knowledge-panel |     self._bootstrap_inner()
knowledge-panel |   File "/usr/local/lib/python3.9/threading.py", line 980, in _bootstrap_inner
knowledge-panel |     self.run()
knowledge-panel |   File "/usr/local/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 867, in run
knowledge-panel |     result = context.run(func, *args)
knowledge-panel |   File "/code/./app/main.py", line 45, in knowledge_panel
knowledge-panel |     panels.append(last_edits_kp(facet=facet_tag, value=value_tag, country=country))
knowledge-panel |   File "/code/./app/knowledge_panels.py", line 121, in last_edits_kp
knowledge-panel |     expected_html, t_description, t_title = last_edit(url=url, query=query)
knowledge-panel |   File "/code/./app/off.py", line 80, in last_edit
knowledge-panel |     description = _("last-edits issues related to")
knowledge-panel |   File "/code/./app/i18n.py", line 66, in translate
knowledge-panel |     return get_current_translation().gettext(message)
knowledge-panel |   File "/code/./app/i18n.py", line 55, in get_current_translation
knowledge-panel |     log.warning(

So you see the problem is in off.py line 80

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh cool! sorry but can I know how to get that log message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the activate_translation line in main.py :-)

lang_translations.install()
_ = lang_translations.gettext
return _
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 11 additions & 19 deletions app/knowledge_panels.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from turtle import title
from typing import Union
from urllib.parse import urlencode
from .models import HungerGameFilter, country_to_ISO_code, facet_plural
from .off import data_quality, last_edit
from .off import data_quality, hungergame, last_edit


def hunger_game_kp(
@@ -27,9 +26,8 @@ def hunger_game_kp(
questions_url = "https://hunger.openfoodfacts.org/questions"
if query:
questions_url += f"?{urlencode(query)}"
description = "Answer robotoff questions about {description}".format(
description=description
)
t_description = hungergame()
description = f"{t_description} {description}"
html = f"<p><a href='{questions_url}'>{description}</a></p>\n"
return {
"hunger-game": {
@@ -63,7 +61,7 @@ def data_quality_kp(
country_code = country_to_ISO_code(value=country)
url = f"https://{country_code}-en.openfoodfacts.org"
path = ""
description += country
description += f"{country} "
if country is None:
url = "https://world.openfoodfacts.org/"
if facet is not None:
@@ -72,16 +70,14 @@ def data_quality_kp(
if value is not None:
path += f"/{value}"
description += f" {value}"
description = "Data-quality issues related to {description}".format(
description=description
(quality_html, source_url, t_description, t_title) = data_quality(
url=url, path=path
)
title = "Data-quality issues"
(quality_html, source_url) = data_quality(url=url, path=path)

return {
"Quality": {
"title": title,
"subtitle": description,
"title": t_title,
"subtitle": f"{t_description} {description}",
"source_url": f"{source_url}/data-quality",
"elements": [
{
@@ -122,16 +118,12 @@ def last_edits_kp(
if value is not None:
query[f"{facet_plural(facet=facet)}_tags_en"] = value
description += f" {value}"
description = "last-edits issues related to {description}".format(
description=description
)
title = "Last-edites"
expected_html = last_edit(url=url, query=query)
expected_html, t_description, t_title = last_edit(url=url, query=query)

return {
"LastEdits": {
"title": title,
"subtitle": description,
"title": t_title,
"subtitle": f"{t_description} {description}",
"source_url": f"{url}/{facet}/{value}?sort_by=last_modified_t",
"elements": [
{
26 changes: 20 additions & 6 deletions app/off.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from turtle import title
from urllib.parse import urljoin

from .i18n import TranslationStore
import requests

_ = TranslationStore.load()


def data_quality(url, path):
"""
@@ -19,13 +24,13 @@ def data_quality(url, path):
"name": tag["name"],
}
html.append(f'<li><a herf={tag["url"]}>')
html.append(("{products} products with {name}").format(**info))
html.append(_("{products} products with {name}").format(**info))
html.append("</a></li>")

html = (
[
"<ul><p>",
("The total number of issues are {total_issues}").format(
_("The total number of issues are {total_issues}").format(
total_issues=total_issues
),
"</p>",
@@ -34,8 +39,10 @@ def data_quality(url, path):
+ ["</ul>"]
)
text = "".join(html)
description = _("Data-quality issues related to")
title = _("Data-quality issues")

return text, source_url
return text, source_url, description, title


def last_edit(url, query):
@@ -58,20 +65,27 @@ def last_edit(url, query):
}
html.append("<li>")
html.append(
("{product_name} ({code}) edited by {last_editor} on {edit_date}").format(
_("{product_name} ({code}) edited by {last_editor} on {edit_date}").format(
**info
)
)
html.append("</li>")
html = (
[
"<ul><p>",
("Total number of edits {counts}").format(counts=counts),
_("Total number of edits {counts}").format(counts=counts),
"</p>",
]
+ html
+ ["</ul>"]
)
text = "".join(html)
description = _("last-edits issues related to")
title = _("Last-edits")
return text, description, title


return text
def hungergame():
"""Helper function for making Translation easy"""
description = _("Answer robotoff questions about")
return description
9 changes: 0 additions & 9 deletions i18n.py

This file was deleted.

46 changes: 41 additions & 5 deletions i18n/en/LC_MESSAGES/knowledge-panel.po
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-08-16 20:04+0530\n"
"PO-Revision-Date: 2022-08-16 20:07+0530\n"
"POT-Creation-Date: 2022-08-21 16:36+0530\n"
"PO-Revision-Date: 2022-08-21 16:42+0530\n"
"Last-Translator: <EMAIL@ADDRESS>\n"
"Language-Team: English\n"
"Language: en\n"
@@ -17,6 +17,42 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: i18n.py:9
msgid "Hello World"
msgstr "Hello World"
#: app/off.py:27
#, python-brace-format
msgid "{products} products with {name}"
msgstr "{products} products with {name}"

#: app/off.py:33
#, python-brace-format
msgid "The total number of issues are {total_issues}"
msgstr "The total number of issues are {total_issues}"

#: app/off.py:42
msgid "Data-quality issues related to"
msgstr "Data-quality issues related to"

#: app/off.py:43
msgid "Data-quality issues"
msgstr "Data-quality issues"

#: app/off.py:68
#, python-brace-format
msgid "{product_name} ({code}) edited by {last_editor} on {edit_date}"
msgstr "{product_name} ({code}) edited by {last_editor} on {edit_date}"

#: app/off.py:76
#, python-brace-format
msgid "Total number of edits {counts}"
msgstr "Total number of edits {counts}"

#: app/off.py:83
msgid "last-edits issues related to"
msgstr "last-edits issues related to"

#: app/off.py:84
msgid "Last-edits"
msgstr "Last-edits"

#: app/off.py:90
msgid "Answer robotoff questions about"
msgstr "Answer robotoff questions about"
48 changes: 42 additions & 6 deletions i18n/fr/LC_MESSAGES/knowledge-panel.po
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# French translations for PACKAGE package
# Traductions fran�aises du paquet PACKAGE.
# Traductions fran�aises du paquet PACKAGE.
# Copyright (C) 2022 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# <EMAIL@ADDRESS>, 2022.
@@ -8,8 +8,8 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-08-16 20:04+0530\n"
"PO-Revision-Date: 2022-08-16 20:06+0530\n"
"POT-Creation-Date: 2022-08-21 16:36+0530\n"
"PO-Revision-Date: 2022-08-21 16:45+0530\n"
"Last-Translator: <EMAIL@ADDRESS>\n"
"Language-Team: French\n"
"Language: fr\n"
@@ -18,6 +18,42 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: i18n.py:9
msgid "Hello World"
msgstr "Bonjour something"
#: app/off.py:27
#, python-brace-format
msgid "{products} products with {name}"
msgstr ""

#: app/off.py:33
#, python-brace-format
msgid "The total number of issues are {total_issues}"
msgstr ""

#: app/off.py:42
msgid "Data-quality issues related to"
msgstr ""

#: app/off.py:43
msgid "Data-quality issues"
msgstr ""

#: app/off.py:68
#, python-brace-format
msgid "{product_name} ({code}) edited by {last_editor} on {edit_date}"
msgstr ""

#: app/off.py:76
#, python-brace-format
msgid "Total number of edits {counts}"
msgstr ""

#: app/off.py:83
msgid "last-edits issues related to"
msgstr ""

#: app/off.py:84
msgid "Last-edits"
msgstr ""

#: app/off.py:90
msgid "Answer robotoff questions about"
msgstr ""
46 changes: 41 additions & 5 deletions i18n/knowledge-panel.pot
Original file line number Diff line number Diff line change
@@ -8,15 +8,51 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-08-16 20:04+0530\n"
"POT-Creation-Date: 2022-08-21 16:36+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Last-Translator: SUMIT KASHYAP <mr.sumitkrr@gmail.com>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: i18n.py:9
msgid "Hello World"
#: app/off.py:27
#, python-brace-format
msgid "{products} products with {name}"
msgstr ""

#: app/off.py:33
#, python-brace-format
msgid "The total number of issues are {total_issues}"
msgstr ""

#: app/off.py:42
msgid "Data-quality issues related to"
msgstr ""

#: app/off.py:43
msgid "Data-quality issues"
msgstr ""

#: app/off.py:68
#, python-brace-format
msgid "{product_name} ({code}) edited by {last_editor} on {edit_date}"
msgstr ""

#: app/off.py:76
#, python-brace-format
msgid "Total number of edits {counts}"
msgstr ""

#: app/off.py:83
msgid "last-edits issues related to"
msgstr ""

#: app/off.py:84
msgid "Last-edits"
msgstr ""

#: app/off.py:90
msgid "Answer robotoff questions about"
msgstr ""