Skip to content

Commit

Permalink
chore: Simplify gettext implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Aug 23, 2024
1 parent 8ed580f commit 503f494
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 356 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- run: pip install -r requirements.txt .
- name: Update catalogs
run: |
pybabel extract -F babel.cfg -o messages.pot .
pybabel extract -k '_ i' -o messages.pot .
pybabel update -N -i messages.pot -d locale
- name: Count incomplete translations
shell: bash
Expand Down
32 changes: 0 additions & 32 deletions app/babel.py

This file was deleted.

7 changes: 7 additions & 0 deletions app/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@
def _(message: str, language: str | None = None, **kwargs: Any) -> str:
translator = translators.get(language or app_settings.email_template_lang, gettext.NullTranslations())
return translator.gettext(message) % kwargs


def i(message: str) -> str:
"""
An identity function, used to extract messages only.
"""
return message
91 changes: 46 additions & 45 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlalchemy.sql.expression import nulls_last, true
from sqlmodel import Field, Relationship, SQLModel, col

from app.i18n import i
from app.settings import app_settings


Expand Down Expand Up @@ -126,15 +127,15 @@ def create_or_update(cls, session: Session, filters: list[bool | ColumnElement[B


class BorrowerDocumentType(StrEnum):
INCORPORATION_DOCUMENT = "INCORPORATION_DOCUMENT"
SUPPLIER_REGISTRATION_DOCUMENT = "SUPPLIER_REGISTRATION_DOCUMENT"
BANK_NAME = "BANK_NAME"
BANK_CERTIFICATION_DOCUMENT = "BANK_CERTIFICATION_DOCUMENT"
FINANCIAL_STATEMENT = "FINANCIAL_STATEMENT"
SIGNED_CONTRACT = "SIGNED_CONTRACT"
SHAREHOLDER_COMPOSITION = "SHAREHOLDER_COMPOSITION"
CHAMBER_OF_COMMERCE = "CHAMBER_OF_COMMERCE"
THREE_LAST_BANK_STATEMENT = "THREE_LAST_BANK_STATEMENT"
INCORPORATION_DOCUMENT = i("INCORPORATION_DOCUMENT")
SUPPLIER_REGISTRATION_DOCUMENT = i("SUPPLIER_REGISTRATION_DOCUMENT")
BANK_NAME = i("BANK_NAME")
BANK_CERTIFICATION_DOCUMENT = i("BANK_CERTIFICATION_DOCUMENT")
FINANCIAL_STATEMENT = i("FINANCIAL_STATEMENT")
SIGNED_CONTRACT = i("SIGNED_CONTRACT")
SHAREHOLDER_COMPOSITION = i("SHAREHOLDER_COMPOSITION")
CHAMBER_OF_COMMERCE = i("CHAMBER_OF_COMMERCE")
THREE_LAST_BANK_STATEMENT = i("THREE_LAST_BANK_STATEMENT")


# https://github.com/open-contracting/credere-backend/issues/39
Expand All @@ -159,48 +160,48 @@ class ApplicationStatus(StrEnum):
#: Credere sends an invitation to the borrower.
#:
#: (:typer:`python-m-app-fetch-awards`)
PENDING = "PENDING"
PENDING = i("PENDING")
#: Borrower declines the invitation.
#:
#: (``/applications/decline``)
DECLINED = "DECLINED"
DECLINED = i("DECLINED")
#: Borrower accepts the invitation.
#:
#: (``/applications/access-scheme``)
ACCEPTED = "ACCEPTED"
ACCEPTED = i("ACCEPTED")
#: Borrower submits its application.
#:
#: (``/applications/submit``)
SUBMITTED = "SUBMITTED"
SUBMITTED = i("SUBMITTED")
#: Lender start reviewing the application.
#:
#: (``/applications/{id}/start``)
STARTED = "STARTED"
STARTED = i("STARTED")
#: Lender rejects the application, after the borrower either submits its application, updates a document,
#: or uploads its contract and final contract amount.
#:
#: (``/applications/{id}/reject-application``)
REJECTED = "REJECTED"
REJECTED = i("REJECTED")
#: Lender requests the borrower to update a document.
#:
#: (``/applications/email-sme/{id}``)
INFORMATION_REQUESTED = "INFORMATION_REQUESTED"
INFORMATION_REQUESTED = i("INFORMATION_REQUESTED")
#: Borrower doesn't accept, or doesn't submit the application or information requested.
#:
#: (:typer:`python-m-app-update-applications-to-lapsed`)
LAPSED = "LAPSED"
LAPSED = i("LAPSED")
#: Lender pre-approves the application, and Credere asks the borrower to upload its contract.
#:
#: (``/applications/{id}/approve-application``)
APPROVED = "APPROVED"
APPROVED = i("APPROVED")
#: Borrower uploads its contract and final contract amount.
#:
#: (``/applications/confirm-upload-contract``)
CONTRACT_UPLOADED = "CONTRACT_UPLOADED"
CONTRACT_UPLOADED = i("CONTRACT_UPLOADED")
#: Lender sets the final credit disbursed.
#:
#: (``/applications/{id}/complete-application``)
COMPLETED = "COMPLETED"
COMPLETED = i("COMPLETED")


class BorrowerStatus(StrEnum):
Expand Down Expand Up @@ -284,34 +285,34 @@ class ApplicationActionType(StrEnum):


class BorrowerSize(StrEnum):
NOT_INFORMED = "NOT_INFORMED"
MICRO = "MICRO"
SMALL = "SMALL"
MEDIUM = "MEDIUM"
BIG = "BIG"
NOT_INFORMED = i("NOT_INFORMED")
MICRO = i("MICRO")
SMALL = i("SMALL")
MEDIUM = i("MEDIUM")
BIG = i("BIG")


class BorrowerSector(StrEnum):
AGRICULTURA = "agricultura"
MINAS = "minas"
MANUFACTURA = "manufactura"
ELECTRICIDAD = "electricidad"
AGUA = "agua"
CONSTRUCCION = "construccion"
TRANSPORTE = "transporte"
ALOJAMIENTO = "alojamiento"
COMUNICACIONES = "comunicaciones"
ACTIVIDADES_FINANCIERAS = "actividades_financieras"
ACTIVIDADES_INMOBILIARIAS = "actividades_inmobiliarias"
ACTIVIDADES_PROFESIONALES = "actividades_profesionales"
ACTIVIDADES_SERVICIOS_ADMINISTRATIVOS = "actividades_servicios_administrativos"
ADMINISTRACION_PUBLICA = "administracion_publica"
EDUCACION = "educacion"
ATENCION_SALUD = "atencion_salud"
ACTIVIDADES_ARTISTICAS = "actividades_artisticas"
OTRAS_ACTIVIDADES = "otras_actividades"
ACTIVIDADES_HOGARES = "actividades_hogares"
ACTIVIDADES_ORGANIZACIONES_EXTRATERRITORIALES = "actividades_organizaciones_extraterritoriales"
AGRICULTURA = i("agricultura")
MINAS = i("minas")
MANUFACTURA = i("manufactura")
ELECTRICIDAD = i("electricidad")
AGUA = i("agua")
CONSTRUCCION = i("construccion")
TRANSPORTE = i("transporte")
ALOJAMIENTO = i("alojamiento")
COMUNICACIONES = i("comunicaciones")
ACTIVIDADES_FINANCIERAS = i("actividades_financieras")
ACTIVIDADES_INMOBILIARIAS = i("actividades_inmobiliarias")
ACTIVIDADES_PROFESIONALES = i("actividades_profesionales")
ACTIVIDADES_SERVICIOS_ADMINISTRATIVOS = i("actividades_servicios_administrativos")
ADMINISTRACION_PUBLICA = i("administracion_publica")
EDUCACION = i("educacion")
ATENCION_SALUD = i("atencion_salud")
ACTIVIDADES_ARTISTICAS = i("actividades_artisticas")
OTRAS_ACTIVIDADES = i("otras_actividades")
ACTIVIDADES_HOGARES = i("actividades_hogares")
ACTIVIDADES_ORGANIZACIONES_EXTRATERRITORIALES = i("actividades_organizaciones_extraterritoriales")


class CreditType(StrEnum):
Expand Down
5 changes: 0 additions & 5 deletions babel.cfg

This file was deleted.

11 changes: 2 additions & 9 deletions docs/contributing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ Setup
alembic upgrade head
#. Install the entry point for Babel:

.. code-block:: bash
pip install -e .
#. Compile message catalogs:

.. code-block:: bash
Expand All @@ -64,7 +58,6 @@ Repository structure
├── __main__.py # Typer commands
├── auth.py # Permissions and JWT token verification
├── aws.py # Amazon Web Services API clients
├── babel.py # Babel extractors
├── db.py # SQLAlchemy database operations and session management
├── dependencies.py # FastAPI dependencies
├── exceptions.py # Definitions of exceptions raised by this application
Expand Down Expand Up @@ -198,7 +191,7 @@ Update translations

.. code-block:: bash
pybabel extract -F babel.cfg -o messages.pot .
pybabel extract -k '_ i' -o messages.pot .
pybabel update -N -i messages.pot -d locale
#. Compile the message catalogs (in development):
Expand All @@ -209,7 +202,7 @@ Update translations
.. note::

The ``babel.cfg`` file lists from which ``StrEnum`` classes to extract messages. If Credere is deployed in English, we need to add an ``en`` locale to translate these. Otherwise, the translations will be database values like "MICRO", not "0 to 10".
Some messags are extracted from ``StrEnum`` classes. If Credere is deployed in English, we need to add an ``en`` locale to translate these. Otherwise, the translations will be database values like "MICRO", not "0 to 10".

.. admonition:: Reference

Expand Down
Loading

0 comments on commit 503f494

Please sign in to comment.