Skip to content

Commit 0daae68

Browse files
fix: app crashing due to undefined settings constant accessor
- add check for undefined settings constant - add null coalescing operation to use getConstants to prevent undefined settings object
1 parent 9d4e225 commit 0daae68

File tree

1 file changed

+11
-8
lines changed
  • packages/sdk/react-native/src/platform

1 file changed

+11
-8
lines changed
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { NativeModules, Platform } from 'react-native';
2-
32
/**
4-
* Ripped from:
5-
* https://dev.to/medaimane/localization-and-internationalization-in-react-native-reaching-global-audiences-3acj
3+
* Apps opted into Fabric (the new architecture of React Native)
4+
* may not have access to the SettingsManager.settings.AppleLocale property.
5+
* It is now common to use the `getConstants` method to access these constant properties with Fabric enabled apps.
66
*/
7-
const locale =
8-
Platform.OS === 'ios'
9-
? NativeModules.SettingsManager?.settings?.AppleLocale // iOS
10-
: NativeModules.I18nManager?.localeIdentifier;
7+
const getAppleLocale = () => {
8+
const settings = NativeModules.SettingsManager?.settings ?? NativeModules.SettingsManager?.getConstants()?.settings;
9+
return settings?.AppleLocale;
10+
}
1111

12-
export default locale;
12+
export default Platform.select({
13+
ios: getAppleLocale(),
14+
android: NativeModules.I18nManager?.localeIdentifier,
15+
})

0 commit comments

Comments
 (0)