开发小程序和RN的时候怎么获取当前系统设置的语言
#15662
-
小程序可以通过 getSystemInfo 获取, |
Beta Was this translation helpful? Give feedback.
Answered by
AimLuo
May 7, 2024
Replies: 3 comments
-
|
Beta Was this translation helpful? Give feedback.
0 replies
-
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 type Locale = 'en' | 'zh'
export async function getSystemLocale() {
let language = (await Taro.getSystemInfo()).language;
language = getAbbreviations(language)
return language as Locale;
}
function getAbbreviations(language: string): Locale {
return language.startsWith("en") ? "en" : "zh"
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
AimLuo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
src/lib/utils/getSystemLocale.rn.ts
src/lib/utils/getSystemLocale.ts