Skip to content

Commit

Permalink
Merge pull request #99 from jama5262/update-local-functionality
Browse files Browse the repository at this point in the history
Update locale functionality
  • Loading branch information
jama5262 authored Mar 4, 2021
2 parents afa69a0 + 00a8691 commit a5dee97
Show file tree
Hide file tree
Showing 33 changed files with 748 additions and 384 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
5. Added Persian `fa` locale contributed by [mozaffari](https://github.com/mozaffari)
6. Added Norwegian `nb` locale contributed by [Yilmaz Guleryuz](https://github.com/zeusbaba)
7. Added `de_DE` mapping locale contributed by [Sebastian Rutofski](https://github.com/SebRut)

_**More changes to come, not yet ready for publish**_
8. Fixed minor locale bugs

## 3.0.1

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Jiffy([2010, 1, 31])..add(months: 1); // This is February 28
```dart
// The locale method always return a future
// To get locale (The default locale is English)
await Jiffy.locale(); // en
await (Jiffy.locale()).code; // en
// To set locale
await Jiffy.locale("fr");
Expand All @@ -87,7 +87,7 @@ Jiffy().yMMMMEEEEdjm; // samedi 19 octobre 2019 19:25
await Jiffy.locale("ar");
Jiffy().yMMMMEEEEdjm; // السبت، ١٩ أكتوبر ٢٠١٩ ٧:٢٧ م
await Jiffy.locale("zh-cn");
await Jiffy.locale("zh_cn");
Jiffy().yMMMMEEEEdjm; // 2019年10月19日星期六 下午7:28
```

Expand Down
28 changes: 17 additions & 11 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,30 +530,28 @@ Jiffy.isDateTime(Jiffy()); // false

# Locale Support

Since Jiffy runs on top of Intl Dateformat, locale for all regions are almost supported. Example
```dart
await Jiffy.locale("fr");
Jiffy().yMMMMEEEEdjm; // samedi 19 octobre 2019 19:25
```
But for relative time e.g `fromNow() and from()` are written manually in Jiffy

Below are the locales that are supported for relative time. More will be added
Below are the locales that are supported in Jiffy. More will be added

| Key | Locale |
| ------------- | ------------- |
| English | en / en-sg / en-au / en-ca / en-gb / en-ie / en-il / en-nz |
| Spanish | es / es-do / es-us |
| Chinese | zh / zh-cn / zh-hk / zh-tw |
| English | en / en_sg / en_au / en_ca / en_gb / en_ie / en_il / en_nz |
| Spanish | es / es_do / es_us |
| Chinese | zh / zh_cn / zh_hk / zh_tw |
| Japanese | ja |
| German | de / de-at / de-ch |
| French | fr / fr-ch / fr-ca |
| German | de / de_at / de_ch |
| French | fr / fr_ch / fr_ca |
| Indonesian | id |
| Italian | it / it-ch |
| Italian | it / it_ch |
| Korean | ko |
| Russian | ru |
| Hindi | hi |
| Arabic | ar / ar-ly / ar-dz / ar-kw / ar-sa / ar-ma / ar-tn |
| Portuguese | pt / pt-br |
| Arabic | ar / ar_ly / ar_dz / ar_kw / ar_sa / ar_ma / ar_tn |
| Portuguese | pt / pt_br |
| Polish | pl |
| Turkish | tr |
| Swedish | sv |
Expand All @@ -576,3 +574,11 @@ Jiffy().yMMMMEEEEdjm; // السبت، ١٩ أكتوبر ٢٠١٩ ٧:٢٧ م
await Jiffy.locale("zh-cn");
Jiffy().yMMMMEEEEdjm; // 2019年10月19日星期六 下午7:28
```

### Get all available locales

To get all available locales in Jiffy run the following

```dart
Jiffy.getAllAvailableLocales() // returns all locales
```
14 changes: 6 additions & 8 deletions example/jiffy_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Future<int> main() async {
Jiffy().format('yyyy [escaped] yyyy'); // 2021 escaped 2021
Jiffy().format(); // 2021-03-02T15:18:29.922343

// Not passing a string pattern for format method will return an ISO Date format
// Not passing a string pattern for format method will return an ISO Date format
Jiffy().format(); // 2021-03-02T15:18:29.922343

// Using lists
// Using lists
Jiffy([2019, 10, 19]).yMMMMd; // January 19, 2021

// Using maps
Expand Down Expand Up @@ -70,19 +70,17 @@ Future<int> main() async {
..subtract(minutes: 30, months: 1);
jiffy11.yMMMMEEEEdjm; // Friday, September 20, 2019 9:50 PM

// LOCALES
// The locale method always return a future
// To get locale (The default locale is English)
// LOCALES
// The locale method always return a future
// To get locale (The default locale is English)
await Jiffy.locale(); // en
// To set locale
await Jiffy.locale('fr');
Jiffy().yMMMMEEEEdjm; // samedi 19 octobre 2019 19:25
await Jiffy.locale('ar');
Jiffy().yMMMMEEEEdjm; // السبت، ١٩ أكتوبر ٢٠١٩ ٧:٢٧ م
await Jiffy.locale('zh-cn');
await Jiffy.locale('zh_cn');
Jiffy().yMMMMEEEEdjm; // 2019年10月19日星期六 下午7:28
await Jiffy.locale('nb');
Jiffy().yMMMMEEEEdjm;

return 0;
}
1 change: 1 addition & 0 deletions lib/src/enums/startOfWeek.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum StartOfWeek { SATURDAY, SUNDAY, MONDAY }
143 changes: 89 additions & 54 deletions lib/src/jiffy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import 'dart:math';

import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:jiffy/src/enums/startOfWeek.dart';
import 'package:jiffy/src/enums/units.dart';
import 'package:jiffy/src/locale/availableLocales.dart';
import 'package:jiffy/src/locale/locale.dart';
import 'package:jiffy/src/utils/exception.dart';
import 'package:jiffy/src/relative_time/relative_time.dart' as relative;
import 'package:jiffy/src/utils/normalize_units.dart';
import 'package:jiffy/src/utils/ordinalLocale.dart';
import 'package:jiffy/src/utils/regex.dart';
import 'package:jiffy/src/utils/replace.dart';

class Jiffy {
late DateTime _dateTime;
static late Locale _defaultLocale;

DateTime get dateTime => _dateTime;

Jiffy([var input, String? pattern]) {
_dateTime = _parse(input, pattern);
_initializeDateTime(input, pattern);
_initializeLocale();
}

Jiffy.unix(int timestamp) {
Expand All @@ -33,6 +37,10 @@ class Jiffy {
return Jiffy(this);
}

void _initializeDateTime(var input, [String? pattern]) {
_dateTime = _parse(input, pattern);
}

DateTime _parse(var input, [String? pattern]) {
var dateTime;
if (input == null && pattern == null) {
Expand Down Expand Up @@ -117,66 +125,71 @@ class Jiffy {
return dateTime;
}

static const _sundayStartOfWeek = [
'en',
'id',
'enca',
'enil',
'esus',
'zhhk',
'zhtw',
'ja',
'frca',
'ko',
'hi',
'ardz',
'arkw',
'arsa',
'ptbr',
'sv',
'nb',
];

static const _saturdayStartOfWeek = [
'ar',
'arly',
'arma',
'fa',
];
static void _initializeLocale() {
var currentLocale = Intl.getCurrentLocale();
_defaultLocale = getLocale(currentLocale);
_defaultLocale.code = currentLocale.toLowerCase();
}

static String _defaultLocale = 'en';
static Future<String> locale([String? locale]) async {
static Future<Locale> locale([String? locale]) async {
_initializeLocale();
if (locale != null) {
if (isLocalAvailable(locale)) {
throw JiffyException(
'The locale "$locale" does not exist in Jiffy, run Jiffy.getAllAvailableLocales() for more locales')
.cause;
}
await initializeDateFormatting();
Intl.defaultLocale = locale;
_defaultLocale = locale;
_defaultLocale = getLocale(locale);
_defaultLocale.code = locale.toLowerCase();
}
return Future.value(_defaultLocale);
}

static List<String> getAllAvailableLocales() {
return getAllLocales();
}

// GET
int get millisecond => _dateTime.millisecond;

int get second => _dateTime.second;

int get minute => _dateTime.minute;

int get hour => _dateTime.hour;

int get date => _dateTime.day;

int get day {
var weekDays = [1, 2, 3, 4, 5, 6, 7, 1, 2];
var weekDayIndex = _dateTime.weekday - 1;
var _locale = replaceLocaleHyphen(_defaultLocale);
if (_sundayStartOfWeek.contains(_locale)) {
weekDayIndex += 1;
} else if (_saturdayStartOfWeek.contains(_locale)) {
weekDayIndex += 2;

switch (_defaultLocale.startOfWeek()) {
case StartOfWeek.MONDAY:
weekDayIndex += 0;
break;
case StartOfWeek.SUNDAY:
weekDayIndex += 1;
break;
case StartOfWeek.SATURDAY:
weekDayIndex += 2;
break;
}
return weekDays[weekDayIndex];
}

int get daysInMonth => _daysInMonth(_dateTime.year, _dateTime.month);

int get dayOfYear => int.parse(DateFormat('D').format(_dateTime));

int get week => ((dayOfYear - day + 10) / 7).floor();

int get month => _dateTime.month;

int get quarter => int.parse(DateFormat('Q').format(_dateTime));

int get year => _dateTime.year;

// MANIPULATE
Expand Down Expand Up @@ -369,65 +382,87 @@ class Jiffy {
// DISPLAY
String format([String? pattern]) {
if (pattern == null) return _dateTime.toIso8601String();
final suffix = _getOrdinalDates(_dateTime.day);
final escaped = replaceEscapePattern(pattern);
final _pattern = replaceOrdinalDatePattern(escaped, suffix);
return DateFormat(_pattern).format(_dateTime);
}

String _getOrdinalDates(int day) {
var ordinals = getOrdinalLocaleDates(replaceLocaleHyphen(_defaultLocale));
if (ordinals == null) return '';
var suffix = ordinals[0];
final digit = day % 10;
if ((digit > 0 && digit < 4) && (day < 11 || day > 13)) {
suffix = ordinals[digit];
}
return suffix;
var ordinal = _defaultLocale.ordinal(_dateTime.day);
var escaped = replaceEscapePattern(pattern);
var newPattern = replaceOrdinalDatePattern(escaped, ordinal);
return DateFormat(newPattern).format(_dateTime);
}

String get E => DateFormat.E().format(_dateTime);

String get EEEE => DateFormat.EEEE().format(_dateTime);

String get LLL => DateFormat.LLL().format(_dateTime);

String get LLLL => DateFormat.LLLL().format(_dateTime);

String get Md => DateFormat.Md().format(_dateTime);

String get MEd => DateFormat.MEd().format(_dateTime);

String get MMM => DateFormat.MMM().format(_dateTime);

String get MMMd => DateFormat.MMMd().format(_dateTime);

String get MMMEd => DateFormat.MMMEd().format(_dateTime);

String get MMMM => DateFormat.MMMM().format(_dateTime);

String get MMMMd => DateFormat.MMMMd().format(_dateTime);

String get MMMMEEEEd => DateFormat.MMMMEEEEd().format(_dateTime);

String get QQQ => DateFormat.QQQ().format(_dateTime);

String get QQQQ => DateFormat.QQQQ().format(_dateTime);

String get yM => DateFormat.yM().format(_dateTime);

String get yMd => DateFormat.yMd().format(_dateTime);

String get yMEd => DateFormat.yMEd().format(_dateTime);

String get yMMM => DateFormat.yMMM().format(_dateTime);

String get yMMMd => DateFormat.yMMMd().format(_dateTime);

String get yMMMdjm => DateFormat.yMMMd().add_jm().format(_dateTime);

String get yMMMEd => DateFormat.yMMMEd().format(_dateTime);

String get yMMMEdjm => DateFormat.yMMMEd().add_jm().format(_dateTime);

String get yMMMM => DateFormat.yMMMM().format(_dateTime);

String get yMMMMd => DateFormat.yMMMMd().format(_dateTime);

String get yMMMMdjm => DateFormat.yMMMMd().add_jm().format(_dateTime);

String get yMMMMEEEEd => DateFormat.yMMMMEEEEd().format(_dateTime);

String get yMMMMEEEEdjm => DateFormat.yMMMMEEEEd().add_jm().format(_dateTime);

String get yQQQ => DateFormat.yQQQ().format(_dateTime);

String get yQQQQ => DateFormat.yQQQQ().format(_dateTime);

String get Hm => DateFormat.Hm().format(_dateTime);

String get Hms => DateFormat.Hms().format(_dateTime);

String get j => DateFormat.j().format(_dateTime);

String get jm => DateFormat.jm().format(_dateTime);

String get jms => DateFormat.jms().format(_dateTime);

String fromNow() {
return relative.format(_defaultLocale, _dateTime);
return _defaultLocale.getRelativeTime(_dateTime);
}

String from(var input) {
var dateTime = _parse(input);
return relative.format(_defaultLocale, _dateTime, dateTime);
return _defaultLocale.getRelativeTime(_dateTime, dateTime);
}

num diff(var input, [Units units = Units.MILLISECOND, bool asFloat = false]) {
Expand Down
Loading

0 comments on commit a5dee97

Please sign in to comment.