Skip to content

Commit

Permalink
refactor(mosip#656): [Pooja|Vijay] refactor methods to get the locali…
Browse files Browse the repository at this point in the history
…zed field Value
  • Loading branch information
vijay151096 committed Mar 28, 2023
1 parent 1368e57 commit 074e77c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 46 deletions.
18 changes: 2 additions & 16 deletions components/VcDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import * as DateFnsLocale from '../lib/date-fns/locale';
import { useTranslation } from 'react-i18next';
import { Image, ImageBackground, View } from 'react-native';
import { Icon } from 'react-native-elements';
import { VC, CredentialSubject, LocalizedField } from '../types/vc';
import { VC, CredentialSubject } from '../types/vc';
import { Button, Column, Row, Text } from './ui';
import { Theme } from './ui/styleUtils';
import { TextItem } from './ui/TextItem';
import { VcItemTags } from './VcItemTags';
import VerifiedIcon from './VerifiedIcon';
import i18n, { getVCDetailsForCurrentLanguage } from '../i18n';
import { getLocalizedField } from '../i18n';

export const VcDetails: React.FC<VcDetailsProps> = (props) => {
const { t, i18n } = useTranslation('VcDetails');
Expand Down Expand Up @@ -346,17 +346,3 @@ function getFullAddress(credential: CredentialSubject) {
.filter(Boolean)
.join(', ');
}

function getLocalizedField(rawField: string | LocalizedField[]) {
if (typeof rawField === 'string') {
return rawField;
}
try {
const locales: LocalizedField[] = JSON.parse(JSON.stringify(rawField));
const currentLanguage = i18n.language;
if (locales.length == 1) return locales[0]?.value;
return getVCDetailsForCurrentLanguage(locales, currentLanguage);
} catch (e) {
return '';
}
}
17 changes: 1 addition & 16 deletions components/VcItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import { Theme } from './ui/styleUtils';
import { RotatingIcon } from './RotatingIcon';
import { GlobalContext } from '../shared/GlobalContext';
import { useTranslation } from 'react-i18next';
import { LocalizedField } from '../types/vc';
import { VcItemTags } from './VcItemTags';
import VerifiedIcon from './VerifiedIcon';
import i18n, { getVCDetailsForCurrentLanguage } from '../i18n';
import { getLocalizedField } from '../i18n';

const getDetails = (arg1, arg2, verifiableCredential) => {
if (arg1 === 'Status') {
Expand Down Expand Up @@ -305,17 +304,3 @@ interface VcItemProps {
onShow?: (vcRef?: ActorRefFrom<typeof vcItemMachine>) => void;
activeTab?: string;
}

function getLocalizedField(rawField: string | LocalizedField) {
if (typeof rawField === 'string') {
return rawField;
}
try {
const locales: LocalizedField[] = JSON.parse(JSON.stringify(rawField));
const currentLanguage = i18n.language;
if (locales.length == 1) return locales[0]?.value;
return getVCDetailsForCurrentLanguage(locales, currentLanguage);
} catch (e) {
return '';
}
}
14 changes: 1 addition & 13 deletions components/VidItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Column, Row, Text } from './ui';
import { Theme } from './ui/styleUtils';
import { RotatingIcon } from './RotatingIcon';
import { GlobalContext } from '../shared/GlobalContext';
import { LocalizedField } from '../types/vc';
import { getLocalizedField } from '../i18n';

export const VidItem: React.FC<VcItemProps> = (props) => {
const { appService } = useContext(GlobalContext);
Expand Down Expand Up @@ -105,15 +105,3 @@ interface VcItemProps {
selected?: boolean;
onPress?: (vcRef?: ActorRefFrom<typeof vcItemMachine>) => void;
}

function getLocalizedField(rawField: string | LocalizedField) {
if (typeof rawField === 'string') {
return rawField;
}
try {
const locales: LocalizedField[] = JSON.parse(JSON.stringify(rawField));
return locales[0].value;
} catch (e) {
return '';
}
}
17 changes: 16 additions & 1 deletion i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage';

const resources = { en, fil, ar, hi, kn, ta };
import { iso6393To1, iso6393To2T } from 'iso-639-3';
import { LocalizedField } from './types/vc';

const languageCodeMap = {};

Expand Down Expand Up @@ -48,8 +49,9 @@ function getLanguageCode(code: string) {
return language;
}

export function getVCDetailsForCurrentLanguage(locales, currentLanguage) {
export function getVCDetailsForCurrentLanguage(locales) {
const supportedLanguages = Object.keys(SUPPORTED_LANGUAGES);
const currentLanguage = i18next.language;
for (const index in supportedLanguages) {
const supportedLanguage = supportedLanguages[index];
if (supportedLanguage == currentLanguage) {
Expand Down Expand Up @@ -85,3 +87,16 @@ function populateLanguageCodeMap() {
));
});
}

export function getLocalizedField(rawField: string | LocalizedField[]) {
if (typeof rawField === 'string') {
return rawField;
}
try {
const locales: LocalizedField[] = JSON.parse(JSON.stringify(rawField));
if (locales.length == 1) return locales[0]?.value;
return getVCDetailsForCurrentLanguage(locales);
} catch (e) {
return '';
}
}

0 comments on commit 074e77c

Please sign in to comment.