Skip to content

Commit

Permalink
Merge pull request #28 from ravikp/demographic-fix
Browse files Browse the repository at this point in the history
fix(#656): Resident demographic data are not rendered in requested language
  • Loading branch information
tilak-puli authored Mar 30, 2023
2 parents ac26ebe + 52aff6a commit 46f3318
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 39 deletions.
15 changes: 2 additions & 13 deletions components/VcDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +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 { getLocalizedField } from '../i18n';

export const VcDetails: React.FC<VcDetailsProps> = (props) => {
const { t, i18n } = useTranslation('VcDetails');
Expand Down Expand Up @@ -345,15 +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));
return locales[0].value;
} catch (e) {
return '';
}
}
14 changes: 1 addition & 13 deletions components/VcItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +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 { getLocalizedField } from '../i18n';

const getDetails = (arg1, arg2, verifiableCredential) => {
if (arg1 === 'Status') {
Expand Down Expand Up @@ -304,15 +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));
return locales[0].value;
} 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 '';
}
}
52 changes: 52 additions & 0 deletions i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import ta from './locales/tam.json';
import AsyncStorage from '@react-native-async-storage/async-storage';

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

const languageCodeMap = {};

export const SUPPORTED_LANGUAGES = {
en: 'English',
Expand All @@ -34,6 +38,7 @@ i18next
const language = await AsyncStorage.getItem('language');
if (language !== i18next.language) {
i18next.changeLanguage(language);
populateLanguageCodeMap();
}
});

Expand All @@ -43,3 +48,50 @@ function getLanguageCode(code: string) {
const [language] = code.split('-');
return language;
}

export function getVCDetailsForCurrentLanguage(locales) {
const currentLanguage = i18next.language;
const vcDetailsForCurrentLanguage = locales.filter(
(obj) => obj.language === languageCodeMap[currentLanguage]
);
return vcDetailsForCurrentLanguage[0]?.value
? vcDetailsForCurrentLanguage[0].value
: locales[0]?.value;
}

// This method gets the value from iso-639-3 package, which contains key value pairs of three letter language codes[key] and two letter langugae code[value]. These values are according to iso standards.
// The response received from the server is three letter language code and the value in the inji code base is two letter language code. Hence the conversion is done.
function getThreeLetterLanguageCode(twoLetterLanguageCode) {
return Object.keys(iso6393To1).find(
(key) => iso6393To1[key] === twoLetterLanguageCode
);
}

function populateLanguageCodeMap() {
const supportedLanguages = Object.keys(SUPPORTED_LANGUAGES);
supportedLanguages.forEach((languageCode) => {
let threeLetterLanguageCode = languageCode;

if (isTwoLetterLanguageCode(languageCode)) {
threeLetterLanguageCode = getThreeLetterLanguageCode(languageCode);
}
languageCodeMap[languageCode] = threeLetterLanguageCode;
});
}

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 '';
}
}

function isTwoLetterLanguageCode(languageCode) {
return languageCode.length == 2;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"expo-status-bar": "~1.2.0",
"expo-updates": "~0.11.6",
"i18next": "^21.6.16",
"iso-639-3": "^3.0.1",
"mosip-inji-face-sdk": "^0.1.11",
"node-forge": "^1.3.1",
"react": "17.0.1",
Expand Down

0 comments on commit 46f3318

Please sign in to comment.