Skip to content
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

Fix for app crashing in dev environment while changing the language. #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
68 changes: 49 additions & 19 deletions src/lib/i18n/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type TranslateOptions from 'i18next';
import i18n from 'i18next';
import memoize from 'lodash.memoize';
import { useCallback } from 'react';
import { I18nManager, NativeModules, Platform } from 'react-native';
import { I18nManager, Platform } from 'react-native';
import { useMMKVString } from 'react-native-mmkv';
import RNRestart from 'react-native-restart';

Expand All @@ -15,40 +15,70 @@ export type TxKeyPath = RecursiveKeyOf<DefaultLocale>;

export const LOCAL = 'local';

export const getLanguage = () => storage.getString(LOCAL); // 'Marc' getItem<Language | undefined>(LOCAL);
export const getLanguage = (): Language | undefined => {
return storage.getString(LOCAL) as Language | undefined;
};

export const translate = memoize(
(key: TxKeyPath, options = undefined) =>
i18n.t(key, options) as unknown as string,
(key: TxKeyPath, options: typeof TranslateOptions) =>
options ? key + JSON.stringify(options) : key
);

export const changeLanguage = (lang: Language) => {
i18n.changeLanguage(lang);
if (lang === 'ar') {
I18nManager.forceRTL(true);
} else {
I18nManager.forceRTL(false);
}
if (Platform.OS === 'ios' || Platform.OS === 'android') {
if (__DEV__) NativeModules.DevSettings.reload();
else RNRestart.restart();
} else if (Platform.OS === 'web') {
window.location.reload();
) as unknown as {
(key: TxKeyPath, options?: typeof TranslateOptions): string;
cache: {
clear(): void;
};
};

export const changeLanguage = async (lang: Language) => {
try {
await i18n.changeLanguage(lang);

if (lang === 'ar') {
I18nManager.forceRTL(true);
} else {
I18nManager.forceRTL(false);
}

translate.cache.clear();

if (Platform.OS === 'ios' || Platform.OS === 'android') {
RNRestart.restart();
} else if (Platform.OS === 'web') {
setTimeout(() => {
window.location.reload();
}, 100);
}
} catch (error) {
console.error('Error changing language:', error);
}
};

export const useSelectedLanguage = () => {
const [language, setLang] = useMMKVString(LOCAL);

const setLanguage = useCallback(
(lang: Language) => {
setLang(lang);
if (lang !== undefined) changeLanguage(lang as Language);
async (lang: Language) => {
try {
setLang(lang);
if (lang) {
await changeLanguage(lang);
}
} catch (error) {
console.error('Error setting language:', error);
}
},
[setLang]
);

return { language: language as Language, setLanguage };
};

i18n.on('languageChanged', () => {
console.log('Language changed successfully');
});

i18n.on('missingKey', (lng, ns, key) => {
console.error(`Missing translation key: ${key} in language: ${lng}`);
});