This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relative_date_format.dart
195 lines (179 loc) · 6.54 KB
/
relative_date_format.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import 'package:flutter/cupertino.dart';
import 'package:lit_relative_date_time/lit_relative_date_time.dart';
/// [RelativeDateFormat] localizes relative time stamps using the provided [Locale].
///
/// Following languages are supported by default at the moment:
/// * English
/// * German
/// * Russian
///
/// If the passed [Locale] can not be applied to the supported localized strings, the
/// English localization will be returned instead.
class RelativeDateFormat {
/// The device [Locale] on which the localized string will be based on.
final Locale locale;
/// The localizations available for formatting.
final List<RelativeDateLocalization> localizations;
final bool debug;
/// Creates a [RelativeDateFormat].
///
/// Provide a [Locale] for localizing a [RelativeDateTime] and formatting it into a
/// human-readable string.
const RelativeDateFormat(
this.locale, {
this.localizations = formatLocalizations,
this.debug = false,
});
/// Gets the localization to be applied to the formatted string by searching the for it
/// in the [localizations] list.
RelativeDateLocalization _getLocalization() {
try {
return localizations.firstWhere((RelativeDateLocalization localization) {
return localization.languageCode == locale.languageCode;
});
// If the provided list does not contain the current locale's localization,
// return the default localization.
} catch (e) {
if (debug) {
print("Error: $e");
}
return defaultLocalization;
}
}
/// States whether to localized the corresponding [TimeDifference]'s time unit in singular form.
bool _isSingular(RelativeDateTime relativeDateTime) {
return relativeDateTime.timeDifference.isSingular;
}
/// Returns the absolute time value.
String _getTimeValue(
RelativeDateTime relativeDateTime,
RelativeDateLocalization relativeDateLocalization,
) {
return "${relativeDateTime.timeDifference.value.abs()}";
}
/// Returns the localized time unit based on the [Locale] parameter value.
String _getLocalizedTimeUnit(
Locale locale,
RelativeDateTime relativeDateTime,
RelativeDateLocalization relativeDateLocalization,
) {
List<String> _timeUnitsSingular =
relativeDateLocalization.timeUnitsSingular;
List<String> _timeUnitsPlural = relativeDateLocalization.timeUnitsPlural;
/// Return the corresponding localized unit depending on its time value.
switch (relativeDateTime.timeDifference.unit) {
case LitTimeUnit.second:
return _isSingular(relativeDateTime)
? _timeUnitsSingular[0]
: _timeUnitsPlural[0];
case LitTimeUnit.minute:
return _isSingular(relativeDateTime)
? _timeUnitsSingular[1]
: _timeUnitsPlural[1];
case LitTimeUnit.hour:
return _isSingular(relativeDateTime)
? _timeUnitsSingular[2]
: _timeUnitsPlural[2];
case LitTimeUnit.day:
return _isSingular(relativeDateTime)
? _timeUnitsSingular[3]
: _timeUnitsPlural[3];
default:
return _isSingular(relativeDateTime)
? _timeUnitsSingular[4]
: _timeUnitsPlural[4];
}
}
/// Returns a localized [String] formatting the [RelativeDateTime] in human-readable
/// form.
///
/// Providing a [RelativeDateTime] whose time unit is 'second' and its value is 3 might
/// return **3 seconds ago** on English Locale.
///
/// Following units can be display for formatting:
/// * Seconds
/// * Minutes
/// * Hours
/// * Years
String format(RelativeDateTime relativeDateTime) {
RelativeDateLocalization _localization = _getLocalization();
//String timeValue = _getLocalizedTimeValue(locale, relativeDateTime);
String timeValue = _getTimeValue(
relativeDateTime,
_localization,
);
String timeUnit = _getLocalizedTimeUnit(
locale,
relativeDateTime,
_localization,
);
const String packageName = "LitRelativeDateTime";
// If the difference in time is less than one second, return the localized string for
// 'at the moment/now' due to the dates being too close to each other.
if (relativeDateTime.isNow) {
return "${_localization.atTheMoment}";
} else {
List<String> _words = [];
// If the relative date time does describe a date in the past, return the formatting
// for past dates.
if (relativeDateTime.isPast) {
for (int i = 0; i < _localization.formatOrderPast.length; i++) {
try {
if (_localization.formatOrderPast[i] ==
FormatComponent.preposition) {
_words.add(_localization.prepositionPast);
}
if (_localization.formatOrderPast[i] == FormatComponent.value) {
_words.add(timeValue);
}
if (_localization.formatOrderPast[i] == FormatComponent.unit) {
_words.add(timeUnit);
}
} catch (e) {
if (debug) {
const String errorMessage =
"ERROR on formatting prepositionPast array. Please check your localization configuration.";
print("$packageName: $errorMessage");
_words.add(errorMessage);
}
}
}
// Else return the formatting for future dates.
} else {
try {
for (int i = 0; i < _localization.formatOrderFuture.length; i++) {
if (_localization.formatOrderFuture[i] ==
FormatComponent.preposition) {
_words.add(_localization.prepositionPast);
}
if (_localization.formatOrderFuture[i] == FormatComponent.value) {
_words.add(timeValue);
}
if (_localization.formatOrderFuture[i] == FormatComponent.unit) {
_words.add(timeUnit);
}
}
} catch (e) {
const String errorMessage =
"Error on formatting prepositionFuture array. Please check your localization configuration.";
if (debug) {
print("$packageName: $errorMessage");
}
_words.add(errorMessage);
}
}
// Try to return the compound words.
try {
return "${_words[0]} ${_words[1]} ${_words[2]}";
// If it does fail, return an error message.
} catch (e) {
const String errorMessage =
"Error on formatting date: Please check your localization config";
if (debug) {
print("$packageName: $errorMessage");
}
return errorMessage;
}
}
}
}