Skip to content

Commit

Permalink
feat(mosip#656): [Pooja] refactor to get language details from i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooja Babusing authored and Pooja Babusing committed Mar 24, 2023
1 parent 690ce79 commit 3ab5c3f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
5 changes: 3 additions & 2 deletions components/VcDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Theme } from './ui/styleUtils';
import { TextItem } from './ui/TextItem';
import { VcItemTags } from './VcItemTags';
import VerifiedIcon from './VerifiedIcon';
import { getLanguageDetails } from './VcItem';
import i18n, { getLanguageDetails } from '../i18n';

export const VcDetails: React.FC<VcDetailsProps> = (props) => {
const { t, i18n } = useTranslation('VcDetails');
Expand Down Expand Up @@ -353,7 +353,8 @@ function getLocalizedField(rawField: string | LocalizedField[]) {
}
try {
const locales: LocalizedField[] = JSON.parse(JSON.stringify(rawField));
return getLanguageDetails(locales);
const currentLanguage = i18n.language;
return getLanguageDetails(locales, currentLanguage);
} catch (e) {
return '';
}
Expand Down
21 changes: 3 additions & 18 deletions components/VcItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useTranslation } from 'react-i18next';
import { LocalizedField } from '../types/vc';
import { VcItemTags } from './VcItemTags';
import VerifiedIcon from './VerifiedIcon';
import i18n, { SUPPORTED_LANGUAGES } from '../i18n';
import i18n, { getLanguageDetails } from '../i18n';

const getDetails = (arg1, arg2, verifiableCredential) => {
if (arg1 === 'Status') {
Expand Down Expand Up @@ -312,24 +312,9 @@ function getLocalizedField(rawField: string | LocalizedField) {
}
try {
const locales: LocalizedField[] = JSON.parse(JSON.stringify(rawField));
return getLanguageDetails(locales);
const currentLanguage = i18n.language;
return getLanguageDetails(locales, currentLanguage);
} catch (e) {
return '';
}
}

export function getLanguageDetails(locales) {
const supportedLanguages = Object.keys(SUPPORTED_LANGUAGES);
const currentLanguage = i18n.language;
if (locales.length > 1) {
for (const language in supportedLanguages) {
if (currentLanguage == language) {
const languageDetails = locales.filter(
(obj) => obj.language === language
);
return languageDetails[0].value;
}
}
}
return locales[0].value;
}
15 changes: 15 additions & 0 deletions i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ function getLanguageCode(code: string) {
const [language] = code.split('-');
return language;
}

export function getLanguageDetails(locales, currentLanguage) {
const supportedLanguages = Object.keys(SUPPORTED_LANGUAGES);
if (locales.length > 1) {
for (const language in supportedLanguages) {
if (currentLanguage == language) {
const languageDetails = locales.filter(
(obj) => obj.language === language
);
return languageDetails[0].value;
}
}
}
return locales[0].value;
}

0 comments on commit 3ab5c3f

Please sign in to comment.