Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: 03 locale preferences api #4

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## 1.1.1

- breaking change: `getSecondsFromGMT` renamed to `getOffsetFromGMT` which now returns a `Duration` instead of an `int`.

## 1.1.0

- Add the `patchNumberSeperators` function to change decimal and group seperators on all locales.
- Get TimeZone Identifier feature implemented.
- Get TimeZone Identifier feature implemented.

## 1.0.1

Expand Down
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

LocalePlus allows easy access to native device locale data in Flutter apps. Includes language, country code, time zone, and number formatting preferences.

# Table of Content
## Table of Content

- [Overview](#overview)
- [Requirements](#requirements)
- [Installation](#installation)
Expand All @@ -11,37 +12,40 @@ LocalePlus allows easy access to native device locale data in Flutter apps. Incl
- [Contributors](#contributors)
- [LICENSE](#license)

# Overview
## Overview

LocalePlus is a Flutter package that allows developers to easily retrieve data from the native device locale settings. With LocalePlus, developers can access information such as the device's language, country code, and time zone, as well as the device's number formatting preferences such as grouping and decimal separator. This makes it easy to build internationalized apps that provide a personalized experience for each user. Whether you're building a financial app that needs to display numbers in the user's local format or a social media app that needs to display timestamps in the user's time zone, LocalePlus has you covered. With a simple and intuitive API, LocalePlus is the perfect tool for any Flutter developer looking to add localization to their app.

# Requirements
## Requirements

- Dart sdk: ">=2.16.2 <3.0.0"
- Flutter: ">=2.5.0"
- Android: minSdkVersion 16
- iOS 9.0+

# Installation
## Installation

with Flutter:
with Flutter:

```
$ flutter pub add locale_plus
```bash
flutter pub add locale_plus
```

This will add a line like this to your package's pubspec.yaml (and run an implicit `flutter pub get`):

```
```yaml
dependencies:
locale_plus: ^1.1.0
```

# Usage
## Usage

## Patch the locales with the users' group and decimal seperators

This function patches all locales in flutter, so that you can use the decimal seperator and group seperator from `numberFormatSymbols` after the patch is complete.
The patchNumberSeperators function patches the locales on android and ios.
It is safe to call on MacOS, Windows, Linux and web.
It is safe to call on MacOS, Windows, Linux and web.

```Dart
import 'package:locale_plus/locale_plus.dart';

Expand All @@ -51,55 +55,55 @@ Future<void> main() async {
runApp(const MyApp());
}
```
## Get Decimal & Grouping Separator

### Get Decimal & Grouping Separator

```Dart
final decimalSeparator = await LocalePlus().getDecimalSeparator();
final groupingSeparator = await LocalePlus().getGroupingSeparator();
```

## Get Language & Region Code
### Get Language & Region Code

```Dart
final regionCode = await LocalePlus().getRegionCode();
final languageCode = await LocalePlus().getLanguageCode();
```

## Get Seconds from GMT
### Get Time (Duration) from GMT

```Dart
final secondsFromGMT = await LocalePlus().getSecondsFromGMT();
```
final Duration offsetFromGMT = await LocalePlus().getOffsetFromGMT();
```

## Is Device Using 24 hour time and Current Locale AM PM Symbols
### Is Device Using 24 hour time and Current Locale AM PM Symbols

```Dart
final is24HourTime = await LocalePlus().is24HourTime();
final amSymbol = await LocalePlus().getAmSymbol();
final pmSymbol = await LocalePlus().getPmSymbol();
```

## Is Device Using Metric System
### Is Device Using Metric System

```Dart
final usesMetricSystem = await LocalePlus().usesMetricSystem();
```
## Get TimeZone Identifier

### Get TimeZone Identifier

```Dart
final timeZoneIdentifier = await LocalePlus().getTimeZoneIdentifier();
```

# Author
## Author

[Gökberk Bardakçı](https://www.github.com/gokberkbar), [Uygar İşiçelik](https://www.github.com/uygar)

# Contributors
## Contributors

[Bent Engbers](https://github.com/BentEngbers)

# License
## License

LocalePlus is available under the MIT license. See the LICENSE file for more info.

14 changes: 11 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.android.tools.build:gradle:7.1.3'
}
}

Expand All @@ -22,14 +22,22 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 31
compileSdkPreview "UpsideDownCake"
// compileSdkVersion 33

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
minSdkVersion 16
targetSdkPreview "UpsideDownCake"
}
}

dependencies {
def core_version = "1.12.0-alpha03"

// Java language implementation
implementation "androidx.core:core:$core_version"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.example.locale_plus;

import static androidx.core.text.util.LocalePreferences.HourCycle;

import android.content.Context;
import android.os.Build;
import android.text.format.DateFormat;

import androidx.annotation.NonNull;
import androidx.core.text.util.LocalePreferences;
import androidx.core.util.*;

import java.text.DateFormatSymbols;
import java.text.DecimalFormatSymbols;
Expand Down Expand Up @@ -48,9 +53,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
result.success(languageCode);
}else if (call.method.equals(MethodNames.usesMetricSystem.getText())) {
result.success(usesMetricSystem(currentLocale));
}else if (call.method.equals(MethodNames.is24HourTime.getText())) {
boolean is24HourTime = DateFormat.is24HourFormat(mContext);
result.success(is24HourTime);
}else if (call.method.equals(MethodNames.getHourCycle.getText())) {
result.success(getHourCycle(currentLocale));
} else if (call.method.equals(MethodNames.getAmSymbol.getText())) {
String[] amPmStrings = DateFormatSymbols.getInstance(currentLocale).getAmPmStrings();
if (amPmStrings.length > 0) {
Expand Down Expand Up @@ -87,6 +91,24 @@ private boolean usesMetricSystem(Locale locale) {
}
}

private String getHourCycle(Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return LocalePreferences.getHourCycle(locale);
}
boolean is24HourTime = DateFormat.is24HourFormat(mContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return is24HourTime ? HourCycle.H24 : HourCycle.H12;
}
return is24HourTime ? "h24" : "h12";
}

private int getTemperatureUnit(Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
LocalePreferences.getTemperatureUnit(locale);
}
return -1;
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum MethodNames {
getRegionCode("getRegionCode"),
getLanguageCode("getLanguageCode"),
usesMetricSystem("usesMetricSystem"),
is24HourTime("is24HourTime"),
getTemperatureUnit("getTemperatureUnit"),
getHourCycle("getHourCycle"),
getAmSymbol("getAmSymbol"),
getPmSymbol("getPmSymbol"),
getTimeZoneIdentifier("getTimeZoneIdentifier");
Expand Down
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ android {
signingConfig signingConfigs.debug
}
}
namespace 'com.example.locale_plus_example'
}

flutter {
Expand Down
3 changes: 1 addition & 2 deletions example/android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locale_plus_example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The INTERNET permission is required for development. Specifically,
the Flutter tool needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
Expand Down
3 changes: 1 addition & 2 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locale_plus_example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="locale_plus_example"
android:name="${applicationName}"
Expand Down
3 changes: 1 addition & 2 deletions example/android/app/src/profile/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locale_plus_example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The INTERNET permission is required for development. Specifically,
the Flutter tool needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>11.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/locale_plus/ios"

SPEC CHECKSUMS:
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
locale_plus: 89c741a60ee3bad68ddbb86c172ed45db98a5ae3

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.11.3
COCOAPODS: 1.11.2
10 changes: 6 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -221,6 +221,7 @@
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand All @@ -235,6 +236,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -339,7 +341,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -417,7 +419,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -466,7 +468,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
Loading