Skip to content
cypherstackPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

cs_locale

Reads the device's locale settings from the operating system and tells when they change.

A locale's name can't tell you how the user writes numbers: it doesn't carry the number format chosen in the system settings. cs_locale asks the system for the separators themselves, and for the sizes of the groups of digits.

import 'package:cs_locale/cs_locale.dart';

const locale = CsLocale();

final settings = await locale.current();
print(settings.locale); // en_DE
print(settings.appLanguage); // en
print(settings.systemLanguage); // de
print(settings.region); // DE
print(settings.decimalSeparator); // ,
print(settings.groupingSeparator); // .
print(settings.groupSizes); // [3]

// The settings as they are now, then again each time one of them changes:
final subscription = locale.settings.listen(
  (settings) {},
  onError: (Object error) {},
);

// One of them, as it is now and then each time it changes:
final separators = locale.select((settings) => settings.decimalSeparator);

A value is null where the system has no answer. groupingSeparator is empty where numbers are written ungrouped, and so is groupSizes.

groupSizes lists the sizes of the groups the grouping separator divides, nearest the decimal separator first, and the last size repeats: [3] writes 1,234,567, and India's [3, 2] writes 12,34,567. A last 0 puts all the digits left over in one group, as Windows can be set to: [3, 0] writes 1234,567. A separator alone can't tell those apart.

It is a GroupSizes, read as a list is: by index, in a loop, with length and isEmpty. Unlike two lists, two of them are equal when their sizes are, so they compare as the other values do, also in a record.

On Android current throws where the plugin's native side can't be reached, as in a build without it. The other platforms read without it. The streams report a read that fails as an error, and also a system whose changes can't be followed.

Listening

settings starts with the settings as they are, so no change can fall between reading them and listening for the next. That gap is there between current and a later listen.

Every CsLocale reads the same system and shares the one stream, so it doesn't matter how many there are.

Listen on the app's main isolate, after runApp or WidgetsFlutterBinding.ensureInitialized(). A listener that comes too early is told, and still gets the settings as they are. Following the system is tried again when the next listener joins.

select makes a new stream on each call. Keep the stream rather than calling it while building a widget. To follow several values at once, select a record:

final grouping = locale.select(
  (settings) => (settings.groupingSeparator, settings.groupSizes),
);

The two languages

systemLanguage is the user's first language in the system settings.

appLanguage is the language the operating system gives the app. Each system chooses it its own way:

Platform The app's language
iOS, macOS The first of the user's languages that the app's bundle declares, or the language the user set for the app. A bundle that declares only English gets en on a German device.
Android The system's first language, or of several the first one the app has resources for, or the language the user set for the app. The same English-only app gets de.
Windows The display language, or one the app set for its own process.
Linux The system's language: the desktop has no setting per app.

It is not the language Flutter shows. Flutter picks that itself from the app's supportedLocales: ask Localizations.localeOf(context).

locale is the app's language together with the region. Android has no region or number format apart from its languages, so there both belong to the locale the app is given.

Where the answers come from

Platform App language System language Region Number separators Group sizes
iOS, macOS Current CFLocale First of the system's AppleLanguages preference Current CFLocale Current CFLocale, including the Number Format setting A CFNumberFormatter for the current CFLocale
Android Locale.getDefault() LocaleManager.getSystemLocales(), or LocaleList.getDefault() before Android 13 Locale.getDefault() DecimalFormatSymbols.getInstance() ICU's DecimalFormat for the same locale, as java.text knows only the first size
Windows First of GetThreadPreferredUILanguages First of GetUserPreferredUILanguages The regional format, GetUserDefaultLocaleName GetLocaleInfoEx, including what is customized in the Region settings GetLocaleInfoEx, LOCALE_SGROUPING
Linux The system language First language of LANGUAGE, else of LC_ALL, LC_MESSAGES or LANG, the variables Flutter's languages come from LC_ALL, LC_NUMERIC or LANG, else the language's region, else LC_MESSAGES or LANG where LC_ALL is not set nl_langinfo_l for the locale that LC_ALL, LC_NUMERIC or LANG names nl_langinfo_l, GROUPING, for the same locale

On iOS a language given on the command line, as Xcode's App Language option gives one, can't be told from the system's, and is reported as both.

On Linux:

  • A value is null when the environment sets no locale for it.
  • C and POSIX name no language and no region. Numbers are written with . and ungrouped where they are set for numbers.
  • LC_ALL stands for every category: where it is set, LC_MESSAGES, LC_NUMERIC and LANG are not read.
  • Entries that name no language are passed over, and aliases like german are not looked up, though Flutter is handed the first and GLib resolves the second.
  • The separators and the group sizes are null when the locale the environment names is not installed. The region is still reported.
  • A separator is converted from the character set of its locale, as the rest of the desktop reads it. A set without the locale's own separator has a stand-in for it: fr_FR.ISO-8859-1 gives a no-break space where fr_FR.UTF-8 gives a narrow one.
  • A few locales group digits with nothing written between the groups, as bg_BG does: the grouping separator is empty, and the group sizes are not.

On the web the package compiles and reads nothing: every value is null.

How changes are noticed

The system's own notification is followed on every platform that has one, so a change is reported while the app stays in the foreground, as when the settings sit beside it on a tablet or a folding phone.

Platform The system's notification
iOS, macOS NSLocale.currentLocaleDidChangeNotification
Android ACTION_LOCALE_CHANGED and locale configuration changes
Windows WM_SETTINGCHANGE for the international settings, or naming no setting
Linux None: the environment is fixed at launch, so nothing can change

The plugin's native side watches for it only while someone listens.

For a new number format the notification on iOS and macOS was seen in the iOS simulator, after the system's own announcement of that change was posted from outside. It has not been seen with the Settings app beside the app.

Where the system announces changes, the settings are also read again as a safety net when the app returns to the foreground, which covers a change made while the app was suspended, and when Flutter reports new languages. Flutter does that on iOS, macOS and Android. On Windows it is given its languages once, at startup, so WM_SETTINGCHANGE is the only signal while the app keeps the focus. On Linux nothing can change, so nothing is read again: each listener gets the settings as they are when it starts.

Each new set of settings is reported once, and only when a value differs from the last one read.

In tests

CsLocale is a plain class with nothing in it for tests. Hand it to the code that needs it, and hand that code a subclass or a mock instead in its tests:

class GermanLocale extends CsLocale {
  static const german = LocaleSettings(
    locale: Locale('de', 'DE'),
    decimalSeparator: ',',
    groupingSeparator: '.',
  );

  @override
  Future<LocaleSettings> current() async => german;

  // Stays open after its first settings, like the real one.
  @override
  Stream<LocaleSettings> get settings =>
      Stream.multi((listener) => listener.add(german));
}

select builds on settings, so a subclass has it for free.

A mock from a mocking library inherits nothing, so stub select along with current and settings. With mocktail the mock is class MockLocale extends Mock implements CsLocale {}. Mockito needs a generated one.

A CsLocale that is not replaced reads the system the test runs on, whatever defaultTargetPlatform says. On a Mac or on Windows its streams start with an error, as a test has no native side to follow.

What has run where

Platform Status
iOS Run in an iOS 27 simulator, not on a device: the reads, the integration tests, a hot restart, a language set for the app alone, and a number format and a system language changed while the app stayed in the foreground. The changes were made from outside the simulator, followed by the notification the system posts for them. In an iOS 27.2 simulator the integration tests passed, and the group sizes were [3] for English (Canada) and for Germany, and [3, 2] for English (India), also with separators set apart from that format. Those formats, other than the simulator's own English (Canada), were given to the app alone on its command line. In an iOS 18.6 simulator the integration tests passed, and formats given on the command line were read as in iOS 27: German, Swiss German, French, Bulgarian, English (India) with [3, 2], and separators set apart from the format. In an iOS 26.5 simulator an app built with Flutter 3.44.9 read them too.
macOS Run: the reads against what defaults reports, a language set for the app alone, and the locale notification reaching Dart. The notification was posted by the test; no setting of the Mac was changed. In macOS 27.2 the group sizes were [3] for the Mac's own format, English (Canada). Read the same way for formats by name, they were [3, 2] for English and Hindi in India, and [3] for German, Swiss German, French, Bulgarian, Portuguese, Spanish, Chinese in Taiwan, Arabic in Egypt and Japanese. In macOS 27.0 a number format set apart from the region was read when given to the app alone on its command line, as AppleICUNumberSymbols; one chosen in System Settings has not been tried. On macOS 26.6 an app built with Flutter 3.44.9 read the formats the same way. On macOS 27.0 an app taking the plugin through CocoaPods, in place of Swift Package Manager, read them too, and built for iOS.
Android Run in Android 17 (API 37) and Android 12L (API 32) emulators: the integration tests, and an app logging each report, in a debug build and in a release build shrunk by R8. In Android 17, changed with adb shell cmd locale while the app stayed in the foreground: a language set for the app alone, the system language with and without one, the app's language cleared again, and Arabic with its own digits and then with Latin ones, a change Flutter itself doesn't report. In Android 12L the system language was changed in the Settings app. In Android 17 the group sizes were [3, 2] for English and Hindi in India, also with English (India) set for the app alone, and [3] for German, Swiss German and Arabic. A change made while the app was in the background was reported once, and the plugin stopped watching when the last listener left. On an Android 12 phone (DOOGEE V Max, arm64) the integration tests passed, and a system language changed from adb while the app stayed in the foreground was reported at once, as was the change back. In Android 16 (API 36) and Android 13 (API 33) emulators the integration tests passed, and the system language was changed from adb while the app stayed in the foreground: to German, Arabic with its own digits and with Latin ones, English (India) and French, also while a language was set for the app alone. Each change was reported once. There a change made while the app was in the background was reported twice: when it was made, with the new system language but not yet the app's new locale, and again on the app's return, with both. The Kotlin side has also run in unit tests on a desktop JVM, through Robolectric against the framework classes of Android 12L and Android 13, with stand-ins for Android's system services. It passes Android Lint.
Windows Run on Windows 11 26H2 x64, in debug and release builds of the desktop app: the package's tests, the integration tests, and a UI language set for the app's process. Regional formats chosen in the Settings app while the app ran were each announced and read at once, as Windows reports them to other processes: English (United States), (United Kingdom), (Germany), (Switzerland), (India) and (Finland). On that Windows the Region dialog stored a format, and separators customized in its Additional settings, and announced them, but Windows kept reporting what it had before, to every process. On Windows 11 build 26300 separators and group sizes customized with SetLocaleInfoW, no digit grouping among them, were read by a new process, and by the running app as soon as the change was announced. A process with no window to receive the announcement, like PowerShell, kept answering what it had read before. Regional formats set there with Set-Culture were read too: German (Germany), English (India) with [3, 2], Finnish, German (Switzerland) and Bulgarian. A new display language takes signing out, so it is read at the next launch. The group sizes were read for English (United States). What Windows answers for other formats by name, like 3;2;0 for India, is read in the package's tests.
Linux Run on Ubuntu 24.04, x64 with glibc 2.39: the package's tests, and the example's integration tests in a debug build of the desktop app, which hold the settings against the languages GLib hands Flutter and the separators and group sizes glibc's locale program reports, also with English (India) and Bulgarian numbers. A release build of the example reported the same for English (United Kingdom), English (India) and Portuguese. The separators and group sizes agree with that program for all 509 locales glibc supports, built with localedef: [3], [3, 2] in India, [4] in Taiwan, [2, 2, 2, 3] and ungrouped among them. On arm64 they agreed for all 509 too, with glibc 2.41 in a Debian 13 container emulated by QEMU, where the package's dart:ffi calls ran in Dart alone. The desktop app has not run on arm64.
Web Compiles.

The code that has not run is marked with a TODO.

Every platform but Linux has native code, which announces the changes. The reads go through dart:ffi on iOS, macOS, Windows and Linux, and through the native side on Android.

Trying it

The example app shows the settings and each time they are reported, next to a number field. Its integration test runs against the real system:

cd example
flutter test integration_test -d <device>

On iOS and macOS the checkout must be in a directory named cs_locale. Swift Package Manager names the plugin's package after its directory, and refuses a directory named otherwise, like cs_locale-review, as the example's cs_locale. An app taking the package as a git dependency built fine.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages