diff --git a/src/pendulum/locales/ar/__init__.py b/src/pendulum/locales/ar/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/pendulum/locales/ar/custom.py b/src/pendulum/locales/ar/custom.py new file mode 100644 index 00000000..7aefe390 --- /dev/null +++ b/src/pendulum/locales/ar/custom.py @@ -0,0 +1,24 @@ +""" +ar custom locale file. +""" + +from __future__ import annotations + + +translations = { + "units": {"few_second": "بضع ثوانٍ"}, + # Relative time + "ago": "منذ {0}", + "from_now": "خلال {0}", + "after": "بعد {0}", + "before": "قبل {0}", + # Date formats + "date_formats": { + "LTS": "h:mm:ss A", + "LT": "h:mm A", + "L": "DD/MM/YYYY", + "LL": "D MMMM YYYY", + "LLL": "D MMMM YYYY، h:mm A", + "LLLL": "dddd، D MMMM YYYY، h:mm A", + }, +} diff --git a/src/pendulum/locales/ar/locale.py b/src/pendulum/locales/ar/locale.py new file mode 100644 index 00000000..c48e8301 --- /dev/null +++ b/src/pendulum/locales/ar/locale.py @@ -0,0 +1,329 @@ +from __future__ import annotations + +from pendulum.locales.ar.custom import translations as custom_translations + + +""" +ar locale file. + +Arabic (Modern Standard Arabic). Month and weekday names and the day periods +follow CLDR data; the duration and relative phrases are written in MSA. +""" + + +def _plural(n: int) -> str: + """ + CLDR plural rules for Arabic (integers): zero, one, two, few, many, other. + """ + if n == 0: + return "zero" + if n == 1: + return "one" + if n == 2: + return "two" + if 3 <= n % 100 <= 10: + return "few" + if 11 <= n % 100 <= 99: + return "many" + return "other" + + +locale = { + "plural": _plural, + "ordinal": lambda n: "other", + "translations": { + "days": { + "abbreviated": { + 0: "الاثنين", + 1: "الثلاثاء", + 2: "الأربعاء", + 3: "الخميس", + 4: "الجمعة", + 5: "السبت", + 6: "الأحد", + }, + "narrow": { + 0: "ن", + 1: "ث", + 2: "ر", + 3: "خ", + 4: "ج", + 5: "س", + 6: "ح", + }, + "short": { + 0: "إثنين", + 1: "ثلاثاء", + 2: "أربعاء", + 3: "خميس", + 4: "جمعة", + 5: "سبت", + 6: "أحد", + }, + "wide": { + 0: "الاثنين", + 1: "الثلاثاء", + 2: "الأربعاء", + 3: "الخميس", + 4: "الجمعة", + 5: "السبت", + 6: "الأحد", + }, + }, + "months": { + "abbreviated": { + 1: "يناير", + 2: "فبراير", + 3: "مارس", + 4: "أبريل", + 5: "مايو", + 6: "يونيو", + 7: "يوليو", + 8: "أغسطس", + 9: "سبتمبر", + 10: "أكتوبر", + 11: "نوفمبر", + 12: "ديسمبر", + }, + "narrow": { + 1: "ي", + 2: "ف", + 3: "م", + 4: "أ", + 5: "م", + 6: "ي", + 7: "ي", + 8: "أ", + 9: "س", + 10: "أ", + 11: "ن", + 12: "د", + }, + "wide": { + 1: "يناير", + 2: "فبراير", + 3: "مارس", + 4: "أبريل", + 5: "مايو", + 6: "يونيو", + 7: "يوليو", + 8: "أغسطس", + 9: "سبتمبر", + 10: "أكتوبر", + 11: "نوفمبر", + 12: "ديسمبر", + }, + }, + "units": { + "year": { + "zero": "{0} سنة", + "one": "سنة", + "two": "سنتان", + "few": "{0} سنوات", + "many": "{0} سنة", + "other": "{0} سنة", + }, + "month": { + "zero": "{0} شهر", + "one": "شهر", + "two": "شهران", + "few": "{0} أشهر", + "many": "{0} شهرًا", + "other": "{0} شهر", + }, + "week": { + "zero": "{0} أسبوع", + "one": "أسبوع", + "two": "أسبوعان", + "few": "{0} أسابيع", + "many": "{0} أسبوعًا", + "other": "{0} أسبوع", + }, + "day": { + "zero": "{0} يوم", + "one": "يوم", + "two": "يومان", + "few": "{0} أيام", + "many": "{0} يومًا", + "other": "{0} يوم", + }, + "hour": { + "zero": "{0} ساعة", + "one": "ساعة", + "two": "ساعتان", + "few": "{0} ساعات", + "many": "{0} ساعة", + "other": "{0} ساعة", + }, + "minute": { + "zero": "{0} دقيقة", + "one": "دقيقة", + "two": "دقيقتان", + "few": "{0} دقائق", + "many": "{0} دقيقة", + "other": "{0} دقيقة", + }, + "second": { + "zero": "{0} ثانية", + "one": "ثانية", + "two": "ثانيتان", + "few": "{0} ثوانٍ", + "many": "{0} ثانية", + "other": "{0} ثانية", + }, + "microsecond": { + "zero": "{0} ميكروثانية", + "one": "ميكروثانية", + "two": "ميكروثانيتان", + "few": "{0} ميكروثانيات", + "many": "{0} ميكروثانية", + "other": "{0} ميكروثانية", + }, + }, + "relative": { + "year": { + "future": { + "zero": "خلال {0} سنة", + "one": "خلال سنة", + "two": "خلال سنتين", + "few": "خلال {0} سنوات", + "many": "خلال {0} سنة", + "other": "خلال {0} سنة", + }, + "past": { + "zero": "منذ {0} سنة", + "one": "منذ سنة", + "two": "منذ سنتين", + "few": "منذ {0} سنوات", + "many": "منذ {0} سنة", + "other": "منذ {0} سنة", + }, + }, + "month": { + "future": { + "zero": "خلال {0} شهر", + "one": "خلال شهر", + "two": "خلال شهرين", + "few": "خلال {0} أشهر", + "many": "خلال {0} شهرًا", + "other": "خلال {0} شهر", + }, + "past": { + "zero": "منذ {0} شهر", + "one": "منذ شهر", + "two": "منذ شهرين", + "few": "منذ {0} أشهر", + "many": "منذ {0} شهرًا", + "other": "منذ {0} شهر", + }, + }, + "week": { + "future": { + "zero": "خلال {0} أسبوع", + "one": "خلال أسبوع", + "two": "خلال أسبوعين", + "few": "خلال {0} أسابيع", + "many": "خلال {0} أسبوعًا", + "other": "خلال {0} أسبوع", + }, + "past": { + "zero": "منذ {0} أسبوع", + "one": "منذ أسبوع", + "two": "منذ أسبوعين", + "few": "منذ {0} أسابيع", + "many": "منذ {0} أسبوعًا", + "other": "منذ {0} أسبوع", + }, + }, + "day": { + "future": { + "zero": "خلال {0} يوم", + "one": "خلال يوم", + "two": "خلال يومين", + "few": "خلال {0} أيام", + "many": "خلال {0} يومًا", + "other": "خلال {0} يوم", + }, + "past": { + "zero": "منذ {0} يوم", + "one": "منذ يوم", + "two": "منذ يومين", + "few": "منذ {0} أيام", + "many": "منذ {0} يومًا", + "other": "منذ {0} يوم", + }, + }, + "hour": { + "future": { + "zero": "خلال {0} ساعة", + "one": "خلال ساعة", + "two": "خلال ساعتين", + "few": "خلال {0} ساعات", + "many": "خلال {0} ساعة", + "other": "خلال {0} ساعة", + }, + "past": { + "zero": "منذ {0} ساعة", + "one": "منذ ساعة", + "two": "منذ ساعتين", + "few": "منذ {0} ساعات", + "many": "منذ {0} ساعة", + "other": "منذ {0} ساعة", + }, + }, + "minute": { + "future": { + "zero": "خلال {0} دقيقة", + "one": "خلال دقيقة", + "two": "خلال دقيقتين", + "few": "خلال {0} دقائق", + "many": "خلال {0} دقيقة", + "other": "خلال {0} دقيقة", + }, + "past": { + "zero": "منذ {0} دقيقة", + "one": "منذ دقيقة", + "two": "منذ دقيقتين", + "few": "منذ {0} دقائق", + "many": "منذ {0} دقيقة", + "other": "منذ {0} دقيقة", + }, + }, + "second": { + "future": { + "zero": "خلال {0} ثانية", + "one": "خلال ثانية", + "two": "خلال ثانيتين", + "few": "خلال {0} ثوانٍ", + "many": "خلال {0} ثانية", + "other": "خلال {0} ثانية", + }, + "past": { + "zero": "منذ {0} ثانية", + "one": "منذ ثانية", + "two": "منذ ثانيتين", + "few": "منذ {0} ثوانٍ", + "many": "منذ {0} ثانية", + "other": "منذ {0} ثانية", + }, + }, + }, + "day_periods": { + "midnight": "منتصف الليل", + "am": "ص", + "noon": "الظهر", + "pm": "م", + "morning1": "صباحًا", + "afternoon1": "ظهرًا", + "evening1": "مساءً", + "night1": "ليلاً", + }, + "week_data": { + "min_days": 1, + "first_day": 5, + "weekend_start": 4, + "weekend_end": 5, + }, + }, + "custom": custom_translations, +} diff --git a/tests/localization/test_ar.py b/tests/localization/test_ar.py new file mode 100644 index 00000000..ee7c1cf4 --- /dev/null +++ b/tests/localization/test_ar.py @@ -0,0 +1,142 @@ +from __future__ import annotations + +import pendulum + +from pendulum.locales.locale import Locale + + +locale = "ar" + + +def test_diff_for_humans(): + with pendulum.travel_to(pendulum.datetime(2016, 8, 29), freeze=True): + diff_for_humans() + + +def diff_for_humans(): + d = pendulum.now().subtract(seconds=1) + assert d.diff_for_humans(locale=locale) == "منذ بضع ثوانٍ" + + d = pendulum.now().subtract(seconds=30) + assert d.diff_for_humans(locale=locale) == "منذ 30 ثانية" + + d = pendulum.now().subtract(minutes=1) + assert d.diff_for_humans(locale=locale) == "منذ دقيقة" + + d = pendulum.now().subtract(minutes=2) + assert d.diff_for_humans(locale=locale) == "منذ دقيقتين" + + d = pendulum.now().subtract(minutes=3) + assert d.diff_for_humans(locale=locale) == "منذ 3 دقائق" + + d = pendulum.now().subtract(hours=1) + assert d.diff_for_humans(locale=locale) == "منذ ساعة" + + d = pendulum.now().subtract(hours=2) + assert d.diff_for_humans(locale=locale) == "منذ ساعتين" + + d = pendulum.now().subtract(hours=5) + assert d.diff_for_humans(locale=locale) == "منذ 5 ساعات" + + d = pendulum.now().subtract(days=1) + assert d.diff_for_humans(locale=locale) == "منذ يوم" + + d = pendulum.now().subtract(days=2) + assert d.diff_for_humans(locale=locale) == "منذ يومين" + + d = pendulum.now().subtract(days=3) + assert d.diff_for_humans(locale=locale) == "منذ 3 أيام" + + d = pendulum.now().subtract(weeks=1) + assert d.diff_for_humans(locale=locale) == "منذ أسبوع" + + d = pendulum.now().subtract(weeks=2) + assert d.diff_for_humans(locale=locale) == "منذ أسبوعين" + + d = pendulum.now().subtract(months=1) + assert d.diff_for_humans(locale=locale) == "منذ شهر" + + d = pendulum.now().subtract(months=2) + assert d.diff_for_humans(locale=locale) == "منذ شهرين" + + d = pendulum.now().subtract(months=3) + assert d.diff_for_humans(locale=locale) == "منذ 3 أشهر" + + d = pendulum.now().subtract(years=1) + assert d.diff_for_humans(locale=locale) == "منذ سنة" + + d = pendulum.now().subtract(years=2) + assert d.diff_for_humans(locale=locale) == "منذ سنتين" + + d = pendulum.now().subtract(years=3) + assert d.diff_for_humans(locale=locale) == "منذ 3 سنوات" + + d = pendulum.now().subtract(years=11) + assert d.diff_for_humans(locale=locale) == "منذ 11 سنة" + + d = pendulum.now().add(seconds=1) + assert d.diff_for_humans(locale=locale) == "خلال بضع ثوانٍ" + + d = pendulum.now().add(hours=3) + assert d.diff_for_humans(locale=locale) == "خلال 3 ساعات" + + d = pendulum.now().add(seconds=1) + d2 = pendulum.now() + assert d.diff_for_humans(d2, locale=locale) == "بعد بضع ثوانٍ" + assert d2.diff_for_humans(d, locale=locale) == "قبل بضع ثوانٍ" + + assert d.diff_for_humans(d2, True, locale=locale) == "بضع ثوانٍ" + assert d2.diff_for_humans(d.add(seconds=1), True, locale=locale) == "بضع ثوانٍ" + + +def test_plural_forms(): + # zero, one, two, few (3-10), many (11-99), other (100+ and the rest) + expected = { + 0: "zero", + 1: "one", + 2: "two", + 3: "few", + 10: "few", + 11: "many", + 99: "many", + 100: "other", + 101: "other", + 102: "other", + 103: "few", + 111: "many", + } + ar_locale = Locale.load(locale) + for count, form in expected.items(): + assert ar_locale.plural(count) == form, count + + +def test_in_words(): + assert pendulum.duration(years=1).in_words(locale=locale) == "سنة" + assert pendulum.duration(years=2).in_words(locale=locale) == "سنتان" + assert pendulum.duration(years=3).in_words(locale=locale) == "3 سنوات" + assert pendulum.duration(years=11).in_words(locale=locale) == "11 سنة" + assert pendulum.duration(years=100).in_words(locale=locale) == "100 سنة" + assert pendulum.duration(days=2).in_words(locale=locale) == "يومان" + assert pendulum.duration(days=3).in_words(locale=locale) == "3 أيام" + assert pendulum.duration(hours=11).in_words(locale=locale) == "11 ساعة" + assert pendulum.duration(minutes=3).in_words(locale=locale) == "3 دقائق" + assert pendulum.duration(seconds=10).in_words(locale=locale) == "10 ثوانٍ" + + +def test_format(): + d = pendulum.datetime(2016, 8, 29, 15, 3, 6, 123456) + assert d.format("dddd", locale=locale) == "الاثنين" + assert d.format("ddd", locale=locale) == "الاثنين" + assert d.format("dd", locale=locale) == "إثنين" + assert d.format("MMMM", locale=locale) == "أغسطس" + assert d.format("MMM", locale=locale) == "أغسطس" + assert d.format("L", locale=locale) == "29/08/2016" + assert d.format("LL", locale=locale) == "29 أغسطس 2016" + assert d.format("LLL", locale=locale) == "29 أغسطس 2016، 3:03 م" + assert d.format("LLLL", locale=locale) == "الاثنين، 29 أغسطس 2016، 3:03 م" + assert d.format("LT", locale=locale) == "3:03 م" + assert d.format("LTS", locale=locale) == "3:03:06 م" + assert d.format("A", locale=locale) == "م" + + d = pendulum.datetime(2016, 8, 29, 9, 3, 6) + assert d.format("LT", locale=locale) == "9:03 ص"