Skip to content

Commit

Permalink
Add translation support to base application
Browse files Browse the repository at this point in the history
  • Loading branch information
rudigiesler committed May 12, 2021
1 parent 5a35ee0 commit 6ae10d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ service.

`APPLICATION_CLASS` - Python path to the application that you want to run. Defaults to
`vaccine.vaccine_eligibility.Application`


## Translations
To extract all the strings for translations, run
```bash
pygettext.py -o locales/base.pot <input files>
```
This will create a template file, which you can use to create the PO files. PO files
should be located inside `locales/<language>/LC_MESSAGES/messages.po`

To compile the translations, run
```bash
msgfmt.py <po files>
```
16 changes: 15 additions & 1 deletion vaccine/base_application.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gettext
import logging
from typing import Any, List, Optional

Expand All @@ -23,6 +24,17 @@ def __init__(self, user: User):
self.answer_events: List[Answer] = []
self.messages: List[Message] = []
self.inbound: Optional[Message] = None
self.set_language(self.user.lang)

def set_language(self, language):
self.user.lang = language
self.translation = gettext.translation(
"messages",
localedir="locales",
languages=[self.user.lang or ""],
fallback=True,
)
self._ = self.translation.gettext

async def get_current_state(self):
if not self.state_name:
Expand Down Expand Up @@ -93,4 +105,6 @@ def send_message(self, content, continue_session=True, **kw):
self.messages.append(self.inbound.reply(content, continue_session, **kw))

async def state_error(self):
return EndState(self, text="Something went wrong. Please try again later.")
return EndState(
self, text=self._("Something went wrong. Please try again later.")
)

0 comments on commit 6ae10d9

Please sign in to comment.