Files
inji-wallet/i18n.ts
KiruthikaJeyashankar 8c11cb1ecf feat(INJI-366): display clientName as per app lang in QR login flow (#924)
* feat(INJI-366): display clientName as per app lang in QR login consent and success screen

In case the app language is not supported, fallback client name is displayed

Co-authored-by: Swati Goel <meet2swati@gmail.com>
Signed-off-by: Kiruthika Jeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* refactor(INJI-366): merge localized field function for different data types

Signed-off-by: Kiruthika Jeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* refactor(INJI-366): fix type warnings while getting localized value

Signed-off-by: Kiruthika Jeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

* fix(INJI-366): modify type check in getLocalizedField

Signed-off-by: Kiruthika Jeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>

---------

Signed-off-by: Kiruthika Jeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>
Signed-off-by: Swati Goel <meet2swati@gmail.com>
Co-authored-by: Swati Goel <meet2swati@gmail.com>
2023-10-13 16:03:23 +05:30

130 lines
3.7 KiB
TypeScript

import i18next from 'i18next';
import * as Localization from 'expo-localization';
import {initReactI18next} from 'react-i18next';
import en from './locales/en.json';
import fil from './locales/fil.json';
import ar from './locales/ara.json';
import hi from './locales/hin.json';
import kn from './locales/kan.json';
import ta from './locales/tam.json';
import Storage from './shared/storage';
import {iso6393To1} from 'iso-639-3';
import {LocalizedField} from './types/VC/ExistingMosipVC/vc';
const resources = {en, fil, ar, hi, kn, ta};
const locale = Localization.locale;
const languageCodeMap = {} as {[key: string]: string};
export const SUPPORTED_LANGUAGES = {
en: 'English',
fil: 'Filipino',
ar: 'عربى',
hi: 'हिंदी',
kn: 'ಕನ್ನಡ',
ta: 'தமிழ்',
};
i18next
.use(initReactI18next)
.init({
compatibilityJSON: 'v3',
resources,
lng: getLanguageCode(locale),
fallbackLng: getLanguageCode,
supportedLngs: Object.keys(SUPPORTED_LANGUAGES),
})
.then(async () => {
const language = await Storage.getItem('language');
if (language !== i18next.language) {
i18next.changeLanguage(language);
populateLanguageCodeMap();
}
if (!Object.keys(SUPPORTED_LANGUAGES).includes(i18next.language)) {
i18next.changeLanguage('en');
populateLanguageCodeMap();
}
});
export default i18next;
export function getLanguageCode(code: string) {
const [language] = code.split('-');
return language;
}
export function getValueForCurrentLanguage(
localizedData: LocalizedField[] | Object,
defaultLanguage = '@none',
) {
const currentLanguage = i18next.language;
const currentLanguageCode = languageCodeMap[currentLanguage];
if (Array.isArray(localizedData)) {
const valueForCurrentLanguage = localizedData.filter(
obj => obj.language === currentLanguageCode,
);
return valueForCurrentLanguage[0]?.value
? valueForCurrentLanguage[0].value
: localizedData[0]?.value;
} else {
const localizedDataObject = localizedData as {[key: string]: string};
return localizedDataObject.hasOwnProperty(currentLanguageCode)
? localizedDataObject[currentLanguageCode]
: localizedDataObject[defaultLanguage];
}
}
// 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: string) {
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[] | Object,
) {
if (typeof rawField === 'string') {
return rawField;
}
if (Array.isArray(rawField)) {
try {
if (rawField.length == 1) return rawField[0]?.value;
return getValueForCurrentLanguage(rawField);
} catch (e) {
return '';
}
}
try {
if (Object.keys(rawField).length === 1) {
return Object.values(rawField)[0];
}
return getValueForCurrentLanguage(rawField);
} catch (e) {
return '';
}
}
function isTwoLetterLanguageCode(languageCode: string) {
return languageCode.length == 2;
}