Skip to content

fix(translations): Fix translation of time-related strings like "7 seconds ago", "a minute ago", etc #34051

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 0 additions & 14 deletions superset-frontend/src/hooks/useLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ import dayjs from 'dayjs';
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { ExplorePageState } from 'src/explore/types';
import 'dayjs/locale/en';
import 'dayjs/locale/fr';
import 'dayjs/locale/es';
import 'dayjs/locale/it';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/ja';
import 'dayjs/locale/de';
import 'dayjs/locale/pt';
import 'dayjs/locale/pt-br';
import 'dayjs/locale/ru';
import 'dayjs/locale/ko';
import 'dayjs/locale/sk';
import 'dayjs/locale/sl';
import 'dayjs/locale/nl';

/* eslint-disable no-restricted-imports */
import { Locale } from 'antd/es/locale';
Expand Down
14 changes: 14 additions & 0 deletions superset-frontend/src/preamble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ import setupFormatters from './setup/setupFormatters';
import setupDashboardComponents from './setup/setupDashboardComponents';
import { User } from './types/bootstrapTypes';
import getBootstrapData, { applicationRoot } from './utils/getBootstrapData';
import 'dayjs/locale/en';
import 'dayjs/locale/fr';
import 'dayjs/locale/es';
import 'dayjs/locale/it';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/ja';
import 'dayjs/locale/de';
import 'dayjs/locale/pt';
import 'dayjs/locale/pt-br';
import 'dayjs/locale/ru';
import 'dayjs/locale/ko';
import 'dayjs/locale/sk';
import 'dayjs/locale/sl';
import 'dayjs/locale/nl';
Comment on lines +29 to +42

This comment was marked as resolved.


if (process.env.WEBPACK_MODE === 'development') {
setHotLoaderConfig({ logLevel: 'debug', trackTailUpdates: false });
Expand Down
20 changes: 19 additions & 1 deletion superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from flask_appbuilder.models.decorators import renders
from flask_appbuilder.models.mixins import AuditMixin
from flask_appbuilder.security.sqla.models import User
from flask_babel import lazy_gettext as _
from flask_babel import get_locale, lazy_gettext as _
from jinja2.exceptions import TemplateError
from markupsafe import escape, Markup
from sqlalchemy import and_, Column, or_, UniqueConstraint
Expand Down Expand Up @@ -546,10 +546,28 @@

@property
def changed_on_humanized(self) -> str:
locale = str(get_locale() or 'en')

This comment was marked as resolved.

try:
humanize.i18n.activate(locale)
result = humanize.naturaltime(datetime.now() - self.changed_on)
humanize.i18n.deactivate()
return result
Comment on lines 548 to +554

This comment was marked as resolved.

except:
Copy link
Member

Choose a reason for hiding this comment

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

Are we expecting this to fail? In what cases? Not sure if swallowing anything/everything is best here, often if we know something might fail we'd try to swallow just that one particular exception, and maybe logging.exception(e) to handle the rest of the pessimisticism, so it's unexcpected, handled - but logged somewhere.

Otherwise you may just never know that say your particular locale isn't handled by humanize.

pass

Check warning on line 556 in superset/models/helpers.py

View check run for this annotation

Codecov / codecov/patch

superset/models/helpers.py#L555-L556

Added lines #L555 - L556 were not covered by tests
# Fallback to English
return humanize.naturaltime(datetime.now() - self.changed_on)

@property
def created_on_humanized(self) -> str:
locale = str(get_locale() or 'en')
try:
humanize.i18n.activate(locale)
result = humanize.naturaltime(datetime.now() - self.created_on)
humanize.i18n.deactivate()
return result
except:
pass

Check warning on line 569 in superset/models/helpers.py

View check run for this annotation

Codecov / codecov/patch

superset/models/helpers.py#L568-L569

Added lines #L568 - L569 were not covered by tests
# Fallback to English
return humanize.naturaltime(datetime.now() - self.created_on)
Comment on lines 548 to 571

This comment was marked as resolved.


@renders("changed_on")
Expand Down
Loading