Skip to content
Merged
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
8 changes: 4 additions & 4 deletions lib/src/messages/languages/ar_msg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ArabicMessages extends Messages {

/// Message for when the elapsed time is less than a minute.
@override
String secsAgo(int seconds) => '${convertToUrduNumbers(seconds)} ثوان';
String secsAgo(int seconds) => '${convertToArabicNumbers(seconds)} ثوان';

/// Message for when the elapsed time is about a minute.
@override
Expand All @@ -31,7 +31,7 @@ class ArabicMessages extends Messages {
if (minutes == 2) {
return 'دقيقتين';
}
return '${convertToUrduNumbers(minutes)} دقائق';
return '${convertToArabicNumbers(minutes)} دقائق';
}

/// Message for when the elapsed time is about an hour.
Expand All @@ -44,7 +44,7 @@ class ArabicMessages extends Messages {
if (hours == 2) {
return 'ساعتين';
}
return '${convertToUrduNumbers(hours)} ساعات';
return '${convertToArabicNumbers(hours)} ساعات';
}

/// Message for when the elapsed time is about a day.
Expand All @@ -57,7 +57,7 @@ class ArabicMessages extends Messages {
if (days == 2) {
return 'يومين';
}
return '${convertToUrduNumbers(days)} أيام';
return '${convertToArabicNumbers(days)} أيام';
}

/// Word separator to be used when joining the parts of the message.
Expand Down
25 changes: 25 additions & 0 deletions lib/src/utils/utility.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ String convertToUrduNumbers(int input) {
return result;
}

/// Utility function [convertToArabicNumbers] to convert an English number
/// into its Arabic numeral equivalent.
/// The function takes an integer [input] and replaces its English digits
/// (0-9) with corresponding Arabic digits.
/// Example: 123 -> '١٢٣'
String convertToArabicNumbers(int input) {
// List of English digits
const english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

// List of corresponding Arabic digits
const arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];

// Convert the input number to a string
var result = input.toString();

// Replace each English digit with its Arabic counterpart
for (var i = 0; i < english.length; i++) {
result = result.replaceAll(english[i], arabic[i]);
}

// Return the result with Arabic digits
return result;
}

/// Helper method [formatMessage] to format a time message by combining the
/// [prefix], [msg] (main message), and [suffix]. The method concatenates the
/// non-empty parts of the message using the [Messages.wordSeparator()] to join them.
Expand All @@ -40,3 +64,4 @@ String formatMessage(
(part) => part.isNotEmpty) // Ensure only non-empty parts are joined
.join(message.wordSeparator()); // Join parts with the word separator
}

18 changes: 9 additions & 9 deletions test/messages/language/ar_msg_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
});

test('secsAgo should return correct seconds ago format', () {
expect(messages.secsAgo(5), '۵ ثوان');
expect(messages.secsAgo(5), '٥ ثوان');
});

test('minAgo should return "دقيقة واحدة"', () {
Expand All @@ -31,7 +31,7 @@ void main() {

test('minsAgo should return correct plural form for more than 2 minutes',
() {
expect(messages.minsAgo(5), '۵ دقائق');
expect(messages.minsAgo(5), '٥ دقائق');
});

test('hourAgo should return "ساعة واحدة"', () {
Expand All @@ -44,7 +44,7 @@ void main() {

test('hoursAgo should return correct plural form for more than 2 hours',
() {
expect(messages.hoursAgo(5), '۵ ساعات');
expect(messages.hoursAgo(5), '٥ ساعات');
});

test('dayAgo should return "يوم واحد"', () {
Expand All @@ -56,7 +56,7 @@ void main() {
});

test('daysAgo should return correct plural form for more than 2 days', () {
expect(messages.daysAgo(5), '۵ أيام');
expect(messages.daysAgo(5), '٥ أيام');
});

test('wordSeparator should return a space', () {
Expand All @@ -83,7 +83,7 @@ void main() {
_getRelativeDateTime(const Duration(seconds: 30)),
locale: 'ar',
);
expect(result, 'قبل ۳۰ ثوان');
expect(result, 'قبل ٣٠ ثوان');
});

test('should return دقيقة واحدة for 1 minute ago', () {
Expand All @@ -108,7 +108,7 @@ void main() {
_getRelativeDateTime(const Duration(minutes: 5)),
locale: 'ar',
);
expect(result, 'قبل ۵ دقائق');
expect(result, 'قبل ٥ دقائق');
});

test('should return ساعة واحدة for 1 hour ago', () {
Expand All @@ -132,7 +132,7 @@ void main() {
_getRelativeDateTime(const Duration(hours: 5)),
locale: 'ar',
);
expect(result, 'قبل ۵ ساعات');
expect(result, 'قبل ٥ ساعات');
});

test('should return يوم واحد for 1 day ago', () {
Expand All @@ -156,15 +156,15 @@ void main() {
_getRelativeDateTime(const Duration(days: 5)),
locale: 'ar',
);
expect(result, 'قبل ۵ أيام');
expect(result, 'قبل ٥ أيام');
});

test('should return formatted date for dates beyond 7 days', () {
final result = GetTimeAgo.parse(
_getRelativeDateTime(const Duration(days: 10)),
locale: 'ar',
);
expect(result, isNot('قبل ۱۰ أيام'));
expect(result, isNot('قبل ١٠ أيام'));
});
});
}
Loading