Skip to content

开发小程序和RN的时候怎么获取当前系统设置的语言 #15662

Answered by AimLuo
AimLuo asked this question in Q & A
Discussion options

You must be logged in to vote

src/lib/utils/getSystemLocale.rn.ts

import { NativeModules, Platform } from "react-native";

export type Locale = 'en' | 'zh'

export async function getSystemLocale() {
  let language = 'zh'
  if (Platform.OS === "ios") {
    language = NativeModules.SettingsManager.settings.AppleLocale
    language = language || NativeModules.SettingsManager.settings.AppleLanguages[0]
  } else {
    language = NativeModules.I18nManager.localeIdentifier;
  }
  language = getAbbreviations(language)
  return language as Locale;
}

function getAbbreviations(language: string): Locale {
  return language.startsWith("en") ? "en" : "zh"
}

src/lib/utils/getSystemLocale.ts

import Taro from "@tarojs/taro";


export t…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by AimLuo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q & A
Labels
None yet
1 participant