使用react-native-localize获取设备国家名称异常:身处印度却返回美国的问题求助
react-native-localize获取设备国家名称异常:身处印度却返回美国的问题求助
各位好,我最近在React Native项目里尝试用react-native-localize搭配iso-3166-1-alpha-2获取设备所在的国家名称,结果碰到了个诡异的问题——我明明在印度,但获取到的国家名称却是美国,试了好几次都这样,实在找不到问题出在哪,想请大家帮忙看看!
下面是我相关的代码片段:
import { View, Text } from 'react-native' import React, { useEffect, useState } from 'react' import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import LogInviaPhone from '../Screens/LogInviaPhone'; import LogInviaEmail from '../Screens/LogInviaEmail'; import OTPScreen from '../Screens/OTPScreen'; import OTPScreenEmail from '../Screens/OTPScreenEmail'; import HomeScreen from '../Screens/HomeScreen'; import * as RNLocalize from 'react-native-localize'; import ISO3166 from 'iso-3166-1-alpha-2'; const Stack=createNativeStackNavigator(); export default function Navigation() { const [country,setCountry]=useState(null); useEffect(()=>{ const fetchCountry=async()=>{ try{ const locales = RNLocalize.getLocales(); const countryCode = locales[0]?.countryCode; const countryName = ISO3166.getCountry(countryCode); setCountry(countryName); console.log('获取到的国家名称:', countryName); // 这里打印出来是美国 }catch(error){ console.error('获取国家信息失败:', error); } } fetchCountry(); },[]) return ( <NavigationContainer> <Stack.Navigator initialRouteName="LogInviaPhone"> <Stack.Screen name="LogInviaPhone" component={LogInviaPhone} /> <Stack.Screen name="LogInviaEmail" component={LogInviaEmail} /> <Stack.Screen name="OTPScreen" component={OTPScreen} /> <Stack.Screen name="OTPScreenEmail" component={OTPScreenEmail} /> <Stack.Screen name="HomeScreen" component={HomeScreen} /> </Stack.Navigator> </NavigationContainer> ) }
我已经确认设备的系统区域设置确实是印度,也给了应用相关的位置权限,但结果还是不对。有没有朋友遇到过类似的情况?或者能指点下我哪里可能出错了吗?
备注:内容来源于stack exchange,提问作者Muskan Verma
相关产品推荐
相关产品推荐

