GitHub user dosubot[bot] added a comment to the discussion: Translation of time
passed
<!-- Answer -->
To localize the changed_on_delta_humanized field in Superset’s backend, you
need to replace the use of humanize.naturaltime (which always returns English)
with Flask-Babel’s format_timedelta, which supports translations based on the
current locale.
Here’s how you can implement this:
1. In superset/models/helpers.py, locate the AuditMixinNullable class. The
changed_on_humanized property currently looks like this:
```python
@property
def changed_on_humanized(self) -> str:
return humanize.naturaltime(datetime.now() - self.changed_on)
```
2. Replace humanize.naturaltime with Flask-Babel’s format_timedelta:
```python
from flask_babel import format_timedelta
from datetime import datetime
@property
def changed_on_humanized(self) -> str:
if self.changed_on:
delta = datetime.utcnow() - self.changed_on
return format_timedelta(delta, granularity='second')
return ""
```
You can adjust the granularity ('second', 'minute', 'hour', etc.) to match your
needs. format_timedelta will automatically use the user’s locale if Flask-Babel
is configured correctly in your Superset instance.
3. Do the same for created_on_humanized if you want that field localized as
well.
4. Make sure your Superset backend is configured for Babel and your desired
languages are enabled in superset/config.py:
```python
BABEL_DEFAULT_LOCALE = "en"
BABEL_DEFAULT_FOLDER = "superset/translations"
LANGUAGES = {
"en": {"flag": "us", "name": "English"},
"pt_BR": {"flag": "br", "name": "Brazilian Portuguese"},
# ... other languages ...
}
```
5. If you haven’t already, generate and update your translation files using
pybabel:
```sh
pybabel extract -F superset/translations/babel.cfg -o
superset/translations/messages.pot .
pybabel update -i superset/translations/messages.pot -d superset/translations
pybabel compile -d superset/translations
```
If you need to add a new language:
```sh
pybabel init -i superset/translations/messages.pot -d superset/translations -l
pt_BR
```
6. Restart your Superset backend and test the API (e.g., /api/v1/chart/) with
your user or browser set to pt_BR. The changed_on_delta_humanized field should
now be localized.
References:
- [AuditMixinNullable
implementation](https://github.com/apache/superset/blob/235c9d2ebf2b14af7afac854b5b15a877695d1b1/superset/models/helpers.py)
- [pybabel translation
management](https://github.com/apache/superset/issues/31396)
- [Superset i18n backend
config](https://github.com/apache/superset/discussions/31444)
Let me know if you need a more detailed code example or run into any issues!
<!-- Dosu Comment Footer -->
*To reply, just mention my name [@dosu](https://go.dosu.dev/dosubot).*
---
<sup>How did I do?
[Good](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=great_response)
|
[Irrelevant](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=irrelevant_answer)
|
[Incorrect](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=incorrect_sources)
|
[Verbose](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=too_verbose)
|
[Hallucination](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=hallucination)
| [Report
🐛](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=bug_report)
|
[Other](https://app.dosu.dev/response-feedback/c3cd4cc4-0cb4-4c88-a5f9-0d48c336db99?feedback_type=other)</sup> [](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/33630)
GitHub link:
https://github.com/apache/superset/discussions/33630#discussioncomment-13321869
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]