11

I am trying to get current country of device in but didn't find anything. Is there something to do so in React Native? I tried using react-native-device-info but it is also not supporting but in previous version it can be get by getDeviceCountry(). Now for the latest version it is showing error:

TypeError: _reactNativeDeviceInfo.default.getDeviceCountry is not a function. (In '_reactNativeDeviceInfo.default.getDeviceCountry()', '_reactNativeDeviceInfo.default.getDeviceCountry' is undefined)

Bill P
  • 3,622
  • 10
  • 20
  • 32
Shivam Tiwari
  • 550
  • 1
  • 7
  • 22

5 Answers5

19

According to the documentation of react-native-device-info for latest version, they have moved some of their apis to react-native-localize to reduce duplication in the react-native-community modules. react-native-localize worked perfectly for me.

Setup:

$ npm install --save react-native-localize
# --- or ---
$ yarn add react-native-localize

Usage:

import * as RNLocalize from "react-native-localize";

console.log(RNLocalize.getLocales());
console.log(RNLocalize.getCurrencies());
console.log(RNLocalize.getCountry());
console.log(RNLocalize.getCalendar());
console.log(RNLocalize.getTemperatureUnit());
console.log(RNLocalize.getTimeZone());
console.log(RNLocalize.uses24HourClock());

and many more. For detailed description please visit their official documentation by the given link: react-native-localize

Shivam Tiwari
  • 550
  • 1
  • 7
  • 22
2

The accepted answer didn't work for me, maybe due to https://github.com/zoontek/react-native-localize/issues/81.

However, Expo has something from its ecosystem, which worked out perfectly:

  1. expo install expo-localization
  2. import { getLocales } from 'expo-localization';
  3. console.log(getLocales()[0].regionCode);

Edit: docs here https://docs.expo.io/versions/latest/sdk/localization/#localizationregion

Engin
  • 755
  • 7
  • 20
Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
1

Its solved my problem;

BREAKING CHANGE: remove is24Hour, getTimezone, isAutoTimeZone and isAutoDateAndTime, getDeviceLocale, getDeviceCountry, getPreferredLocales

This was the result of a survey. It removes API duplication in the react-native-community modules

Related PR: https://github.com/react-native-community/react-native-localize/pull/65

Use yarn add

https://github.com/mikehardy/react-native-localize.git#e062f0d2dc3171dc18fdb7b7139d347ad03933dc to maintain isAutoTimeZone + isAutoDateAndTime until merged

sandip
  • 533
  • 9
  • 29
Ibrahim Yolbir
  • 139
  • 2
  • 14
0

Looks like it's a bug in the React Native. Please check their Troubleshooting section

Here is what they advice:

Seems to be a bug caused by react-native link. You can manually delete libRNDeviceInfo-tvOS.a in Xcode -> [Your iOS build target] -> Build Phrases -> Link Binary with Libraries.

szhuravel
  • 51
  • 1
  • 4
0

Please use this package to get device country . to has different type of configs.

react-native-device-country

Some Examples

import DeviceCountry, {
  TYPE_ANY,
  TYPE_TELEPHONY,
  TYPE_CONFIGURATION,
} from 'react-native-device-country';

DeviceCountry.getCountryCode()
  .then((result) => {
    console.log(result);
    // {"code": "BY", "type": "telephony"}
  })
  .catch((e) => {
    console.log(e);
  });


DeviceCountry.getCountryCode(TYPE_TELEPHONY)
  .then((result) => {
    console.log(result);
    // {"code": "BY", "type": "telephony"}
  })
  .catch((e) => {
    console.log(e);
  });


DeviceCountry.getCountryCode(TYPE_CONFIGURATION)
  .then((result) => {
    console.log(result);
    // {"code": "BY", "type": "config"}
  })
  .catch((e) => {
    console.log(e);
  });
Engr.Aftab Ufaq
  • 3,356
  • 3
  • 21
  • 47