搜索插件、代码仓与 APP 分类

插件详情v0.9.1

devicelocale

一个可用于提取设备上当前定义的区域设置的 Flutter 包,其中当前区域设置被置于列表首位。

AndroidiOSWebmacOSLinux
近 30 天下载
12.6万
喜欢数
151
Pub 分数
150

安装插件

flutter pub add devicelocale

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

devicelocale 包是 Flutter 的一个实用工具,可检索用户设备上配置的区域设置列表,当前系统区域设置排在首位。它支持 Android、iOS、Web、macOS 和 Linux 等多个平台,非常适合需要本地化感知行为的应用程序。通过访问原生设备设置,它使应用程序能够动态适应用户偏好,而无需手动配置。该包轻量且可无缝集成到国际化工作流程中。

适合场景

  • 多语言应用程序中的动态语言检测
  • 启动时自动选择用户首选语言
  • 支持基于设备设置的应用程序本地化
  • 构建区域感知特征
  • 同步 UI 语言与系统偏好设置

主要特点

  • 返回设备定义的区域设置,当前区域设置在前
  • 跨平台支持,适用于主要操作系统
  • 用于检索区域设置列表的简单 API
  • 与 Flutter 的本地化框架集成
  • 轻量且无依赖

适合对象

  • 面向全球受众的应用
  • 本地化密集型应用
  • 使用 Flutter 的 intl 包的开发者
  • 需要自动区域设置回退的项目
  • 实施响应式国际化

使用建议

  • 依赖设备的原生设置
  • 不处理自定义区域设置覆盖
  • 如果没有设置区域设置,可能会返回空列表
  • 某些系统需要特定平台的权限
  • 仅限读取,无法修改设备区域设置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

devicelocale

Gets the device locale data, independent of the app locale settings.

Usage

import 'package:devicelocale/devicelocale.dart';

then

List languages = await Devicelocale.preferredLanguages;
String locale = await Devicelocale.currentLocale;

this should return a list of the preferred/current language locales setup on the device, with the current one being the first in the list or just the currently set device locale.

Android specific implementation (no-op on other platforms if supported)

Added per-app language preferences for Android (https://developer.android.com/about/versions/13/features/app-languages#kotlin) You can check if your current device is supported isLanguagePerAppSettingSupported And after checking if your device is supported you can set the per-app language preference with setLanguagePerApp(Locale) this will return true if success

Note for Linux

Since GNU/Linux and POSIX doesn't provide a standard API for getting the preferred languages, Devicelocale.preferredLanguages always returns the current locale.

Swift Package Manager (iOS / macOS)

Both CocoaPods and Swift Package Manager are supported; Flutter ≥ 3.44 uses SwiftPM automatically. To verify SwiftPM locally on Xcode 26+, clone the repository into a directory matching the pubspec name, otherwise the example app build fails with a SwiftPM identity-mismatch error (pub.dev users are unaffected):

git clone https://github.com/magnatronus/flutter-devicelocale.git devicelocale

Updates

Sept 2023

merged pull request for compatibility with AGP 8

Feb 2023

Added 2 new functions for this iOS issue

  • defaultLocale
  • defaultAsLocale
July 2022

Added support for per-app language preferences on Android (https://developer.android.com/about/versions/13/features/app-languages#kotlin) You can now set the per-app language if you have an in app language picker. Updated the demo app

May 2022

Merged pull requests

Oct 2021

Updated the code and APIs used by Flutter for an Android plugin - this was the source of the Deprecated API warning in the Android build.

July 2021

Add beta support for Linux

March 2021

Added test support for web

Updated for null safety

Jul 2020 Locale update v0.3.1

This is an update from a received request, where prior to 0.3.1 the 2 methods:

  • preferredLanguages
  • currentLocale

returned string values.

There are now 2 equivilent methods

  • preferredLanguagesAsLocales
  • currentAsLocale

that now also return a Locale object rather than a string.

releases
  • March 2021 1.0.0 Updated to null safety

  • July 2020 0.3.0 Updated Android to support Android Embedding V2

  • Apr 2020 0.2.3 Updated Android code from contributions from @ened

  • Mar 2020 0.2.2 Updated iOS so that if detected locale is null then it is not included in return value

  • Sep 2019 0.2.0 Updated Android following feedback from https://github.com/huzhirento . Fallback to the currentLocale when attempting to get a list of locales fro Android 7 and below as the getLocales() call is a feature of API 24 and failed for Android 7 and below.

Getting Started

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目