Skip to content

ref(device-contexts): Add function that returns a more detailed desc of a language #26558

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {t} from 'app/locale';
import {Event} from 'app/types/event';
import {defined} from 'app/utils';

import {getRelativeTimeFromEventDateCreated} from '../utils';
import {getFullLanguageDescription, getRelativeTimeFromEventDateCreated} from '../utils';

import formatMemory from './formatMemory';
import formatStorage from './formatStorage';
Expand Down Expand Up @@ -182,7 +182,7 @@ function getDeviceKnownDataDetails(
case DeviceKnownDataType.LANGUAGE:
return {
subject: t('Language'),
value: data.language,
value: getFullLanguageDescription(data.language),
};
case DeviceKnownDataType.LOW_MEMORY:
return {
Expand Down
35 changes: 35 additions & 0 deletions static/app/components/events/contexts/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {Fragment} from 'react';
import styled from '@emotion/styled';
import * as Sentry from '@sentry/react';
import moment from 'moment-timezone';

import {t} from 'app/locale';
import plugins from 'app/plugins';
import ConfigStore from 'app/stores/configStore';
import space from 'app/styles/space';
import {defined} from 'app/utils';

Expand Down Expand Up @@ -63,6 +65,39 @@ export function getRelativeTimeFromEventDateCreated(
);
}

// Typescript doesn't have types for DisplayNames yet and that's why the type assertion "any" is needed below.
// There is currently an open PR that intends to introduce the types https://github.com/microsoft/TypeScript/pull/44022
export function getFullLanguageDescription(locale: string) {
const sentryAppLanguageCode = ConfigStore.get('languageCode');

const [languageAbbreviation, countryAbbreviation] = locale.includes('_')
? locale.split('_')
: locale.split('-');
Comment on lines +73 to +75
Copy link
Member

Choose a reason for hiding this comment

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

If you ever get a name like a-b-c, you're gonna be in trouble. Could that happen?


try {
const languageNames = new (Intl as any).DisplayNames(sentryAppLanguageCode, {
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to comment here about the as any cast: microsoft/TypeScript#41338

type: 'language',
});

const languageName = languageNames.of(languageAbbreviation);

if (countryAbbreviation) {
const regionNames = new (Intl as any).DisplayNames(sentryAppLanguageCode, {
type: 'region',
});

const countryName = regionNames.of(countryAbbreviation.toUpperCase());

return `${languageName} (${countryName})`;
}

return languageName;
} catch (error) {
Sentry.captureException(error);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not familiar with how many browsers have Intl.DisplayNames should we check for it first before logging an exception

Copy link
Member

Choose a reason for hiding this comment

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

return locale;
}
}

const RelativeTime = styled('span')`
color: ${p => p.theme.subText};
margin-left: ${space(0.5)};
Expand Down