add translation

This commit is contained in:
nasser committed 2021-06-06 11:45:19 +04:30
1 parent d4e87f00e4
commit dc614c2545
20 files changed
+703 -563

No files matched your search

+1 -1
View File
@@ -36,7 +36,7 @@
"extra": {
"laravel": {
"providers": [
"Hekmatinasser\\Verta\\VertaServiceProvider"
"Hekmatinasser\\Verta\\Laravel\\VertaServiceProvider"
],
"aliases": {
"Verta": "Hekmatinasser\\Verta\\Verta"
+12 -12
View File
@@ -415,19 +415,19 @@ return $v1->isFriday(); // false
<p>شما باید پیام های خطا برای اعتبار سنجی خود در در مسیر فایل های ترجمه خود اضافه کنیدبرای نمونه کد هایی که درون پکیج برای پیام خطا وجود دارد در فایل ترجمه خود قرار دهیدبرای مثال در فایل ترجمه پیام خطا به آرایه عبارات زیر را اضافه کنید</p>
<pre>
'jdate' => ':attribute تاریخ شمسی معتبر نمی باشد.',
'jdate_equal' => ':attribute تاریخ شمسی برابر :fa-date نمی باشد.',
'jdate_not_equal' => ':attribute تاریخ شمسی نامساوی :fa-date نمی باشد.',
'jdate_equal' => ':attribute تاریخ شمسی برابر :date نمی باشد.',
'jdate_not_equal' => ':attribute تاریخ شمسی نامساوی :date نمی باشد.',
'jdatetime' => ':attribute تاریخ و زمان شمسی معتبر نمی باشد.',
'jdatetime_equal' => ':attribute تاریخ و زمان شمسی مساوی :fa-date نمی باشد.',
'jdatetime_not_equal' => ':attribute تاریخ و زمان شمسی نامساوی :fa-date نمی باشد.',
'jdate_after' => ':attribute تاریخ شمسی باید بعد از :fa-date باشد.',
'jdate_after_equal' => ':attribute تاریخ شمسی باید بعد یا برابر از :fa-date باشد.',
'jdatetime_after' => ':attribute تاریخ و زمان شمسی باید بعد از :fa-date باشد.',
'jdatetime_after_equal' => ':attribute تاریخ و زمان شمسی باید بعد یا برابر از :fa-date باشد.',
'jdate_before' => ':attribute تاریخ شمسی باید قبل از :fa-date باشد.',
'jdate_before_equal' => ':attribute تاریخ شمسی باید قبل یا برابر از :fa-date باشد.',
'jdatetime_before' => ':attribute تاریخ و زمان شمسی باید قبل از :fa-date باشد.',
'jdatetime_before_equal' => ':attribute تاریخ و زمان شمسی باید قبل یا برابر از :fa-date باشد.',
'jdatetime_equal' => ':attribute تاریخ و زمان شمسی مساوی :date نمی باشد.',
'jdatetime_not_equal' => ':attribute تاریخ و زمان شمسی نامساوی :date نمی باشد.',
'jdate_after' => ':attribute تاریخ شمسی باید بعد از :date باشد.',
'jdate_after_equal' => ':attribute تاریخ شمسی باید بعد یا برابر از :date باشد.',
'jdatetime_after' => ':attribute تاریخ و زمان شمسی باید بعد از :date باشد.',
'jdatetime_after_equal' => ':attribute تاریخ و زمان شمسی باید بعد یا برابر از :date باشد.',
'jdate_before' => ':attribute تاریخ شمسی باید قبل از :date باشد.',
'jdate_before_equal' => ':attribute تاریخ شمسی باید قبل یا برابر از :date باشد.',
'jdatetime_before' => ':attribute تاریخ و زمان شمسی باید قبل از :date باشد.',
'jdatetime_before_equal' => ':attribute تاریخ و زمان شمسی باید قبل یا برابر از :date باشد.',
'attributes' => [
'start_date' => 'تاریخ شروع',
+77
View File
@@ -0,0 +1,77 @@
<?php
return [
'numbers' => [
'0' => '۰',
'1' => '۱',
'2' => '۲',
'3' => '۳',
'4' => '٤',
'5' => '٥',
'6' => '٦',
'7' => '۷',
'8' => '۸',
'9' => '۹',
],
'year_months' => [
'1' => 'حَمَل',
'2' => 'ثَور',
'3' => 'جَوزا',
'4' => 'سَرَطان',
'5' => 'اَسَد',
'6' => 'سُنبُله',
'7' => 'میزان',
'8' => 'عَقرَب',
'9' => 'قَوس',
'10' => 'جَدْی',
'11' => 'دَلو',
'12' => 'حوت',
],
'weekdays' => [
'1' => 'شنبه',
'2' => 'یکشنبه',
'3' => 'دوشنبه',
'4' => 'سه شنبه',
'5' => 'چهارشنبه',
'6' => 'پنج شنبه',
'7' => 'جمعه',
],
'date_units' => [
'second' => 'ثواني',
'mintue' => 'الدقائق',
'hour' => 'الساعة',
'day' => 'يوم',
'week' => 'أسبوع',
'month' => 'شهر',
'yaer' => 'سنة',
'decade' => 'عشر سنوات',
'century' => 'مئة عام'
],
'pharse' => [
'am' => 'ق ظ',
'pm' => 'ب ظ',
'ante_meridiem' => 'صباحي',
'post_meridiem' => 'بعد الظهر',
'th' => '',
'ago' => 'قبل',
'now' => 'الآن',
'later' => 'التالي',
],
'validation' => [
'jdate' => ':attribute x ليس تاريخ جلالي صالحًا',
'jdate_equal' => ':attribute لا يساوي تاريخ جلالي :date',
'jdate_not_equal' => 'ال :attribute لا يساوي تاريخ جلالي :date',
'jdatetime' => ':attribute غير صالح لتاريخ جلالي',
'jdatetime_equal' => ':attribute ليس مساويًا لـ تاريخ جلالي :date',
'jdatetime_not_equal' => ':attribute لا يساوي تاريخ جلالي :date',
'jdate_after' => 'يجب أن يكون :attribute تاريخ جلالي بعد :date.',
'jdate_after_equal' => 'يجب أن يكون :attribute هو تاريخ جلالي بعد :date أو يساوي.',
'jdatetime_after' => 'يجب أن يكون :attribute عبارة عن تاريخ جلالي بعد :date.',
'jdatetime_after_equal' => 'يجب أن يكون :attribute عبارة عن تاريخ جلالي بعد :date أو يساوي.',
'jdate_before' => 'يجب أن يكون :attribute تاريخ جلالي قبل :date.',
'jdate_before_equal' => 'يجب أن يكون :attribute تاريخ جلالي قبل :date أو يساوي.',
'jdatetime_before' => 'يجب أن يكون :attribute هو تاريخ جلالي قبل :date.',
'jdatetime_before_equal' => 'يجب أن يكون :attribute هو تاريخ جلالي قبل أو يساوي :date.',
]
];
+77
View File
@@ -0,0 +1,77 @@
<?php
return [
'numbers' => [
'0' => '0',
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
],
'year_months' => [
'1' => 'farvardin',
'2' => 'ordibehesht',
'3' => 'khordad',
'4' => 'tir',
'5' => 'mordad',
'6' => 'shahrivar',
'7' => 'mehr',
'8' => 'aban',
'9' => 'azar',
'10' => 'dey',
'11' => 'bahman',
'12' => 'esfand',
],
'weekdays' => [
'1' => 'saturday',
'2' => 'sunday',
'3' => 'monday',
'4' => 'tuesday',
'5' => 'wednesday',
'6' => 'thursday',
'7' => 'friday',
],
'date_units' => [
'second' => 'second',
'mintue' => 'mintue',
'hour' => 'hour',
'day' => 'day',
'week' => 'week',
'month' => 'month',
'yaer' => 'yaer',
'decade' => 'decade',
'century' => 'century'
],
'pharse' => [
'am' => 'am',
'pm' => 'pm',
'ante_meridiem' => 'ante meridiem',
'post_meridiem' => 'post meridiem',
'th' => 'th',
'ago' => 'ago',
'now' => 'now',
'later' => 'later',
],
'validation' => [
'jdate' => 'The :attribute is not valid Jalali date',
'jdate_equal' => 'The :attribute is not equal Jalali date :date',
'jdate_not_equal' => 'The :attribute is\'nt not equal Jalali date :date',
'jdatetime' => 'The :attribute is not valid Jalali datetime',
'jdatetime_equal' => 'The :attribute is not equal Jalali datetime :date',
'jdatetime_not_equal' => 'The :attribute is\'nt not equal Jalali datetime :date',
'jdate_after' => 'The :attribute must be a Jalali date after :date.',
'jdate_after_equal' => 'The :attribute must be a Jalali date after or equal :date.',
'jdatetime_after' => 'The :attribute must be a Jalali datetime after :date.',
'jdatetime_after_equal' => 'The :attribute must be a Jalali datetime after or equal :date.',
'jdate_before' => 'The :attribute must be a Jalali date before :date.',
'jdate_before_equal' => 'The :attribute must be a Jalali date before or equal :date.',
'jdatetime_before' => 'The :attribute must be a Jalali datetime before :date.',
'jdatetime_before_equal' => 'The :attribute must be a Jalali datetime before or equal :date.',
]
];
+77
View File
@@ -0,0 +1,77 @@
<?php
return [
'numbers' => [
'0' => '۰',
'1' => '۱',
'2' => '۲',
'3' => '۳',
'4' => '۴',
'5' => '۵',
'6' => '۶',
'7' => '۷',
'8' => '۸',
'9' => '۹',
],
'year_months' => [
'1' => 'فروردین',
'2' => 'اردیبهشت',
'3' => 'خرداد',
'4' => 'تیر',
'5' => 'مرداد',
'6' => 'شهریور',
'7' => 'مهر',
'8' => 'آبان',
'9' => 'آذر',
'10' => 'دی',
'11' => 'بهمن',
'12' => 'اسفند',
],
'weekdays' => [
'1' => 'شنبه',
'2' => 'یکشنبه',
'3' => 'دوشنبه',
'4' => 'سه شنبه',
'5' => 'چهارشنبه',
'6' => 'پنج شنبه',
'7' => 'جمعه',
],
'date_units' => [
'second' => 'ثانیه',
'mintue' => 'دقیقه',
'hour' => 'ساعت',
'day' => 'روز',
'week' => 'هفته',
'month' => 'ماه',
'yaer' => 'سال',
'decade' => 'دهه',
'century' => 'قرن'
],
'pharse' => [
'am' => 'ق ظ',
'pm' => 'ب ظ',
'ante_meridiem' => 'قبل از ظهر',
'post_meridiem' => 'بعد از ظهر',
'th' => 'ام',
'ago' => 'قبل',
'now' => 'الان',
'later' => 'بعد',
],
'validation' => [
'jdate' => ':attribute' . ' تاریخ شمسی معتبر نمی باشد.',
'jdate_equal' => ':attribute' . ' تاریخ شمسی برابر ' . ':date' . ' نمی باشد.',
'jdate_not_equal' => ':attribute' . ' تاریخ شمسی نامساوی ' . ':date' . ' نمی باشد.',
'jdatetime' => ':attribute' . ' تاریخ و زمان شمسی معتبر نمی باشد.',
'jdatetime_equal' => ':attribute' . ' تاریخ و زمان شمسی مساوی ' . ':date' . ' نمی باشد.',
'jdatetime_not_equal' => ':attribute' . ' تاریخ و زمان شمسی نامساوی ' . ':date' . ' نمی باشد.',
'jdate_after' => ':attribute' . ' تاریخ شمسی باید بعد از ' . ':date' . ' باشد.',
'jdate_after_equal' => ':attribute' . ' تاریخ شمسی باید بعد یا برابر از ' . ':date' . ' باشد.',
'jdatetime_after' => ':attribute' . ' تاریخ و زمان شمسی باید بعد از ' . ':date' . ' باشد.',
'jdatetime_after_equal' => ':attribute' . ' تاریخ و زمان شمسی باید بعد یا برابر از ' . ':date' . ' باشد.',
'jdate_before' => ':attribute' . ' تاریخ شمسی باید قبل از ' . ':date' . ' باشد.',
'jdate_before_equal' => ':attribute' . ' تاریخ شمسی باید قبل یا برابر از ' . ':date' . ' باشد.',
'jdatetime_before' => ':attribute' . ' تاریخ و زمان شمسی باید قبل از ' . ':date' . ' باشد.',
'jdatetime_before_equal' => ':attribute' . ' تاریخ و زمان شمسی باید قبل یا برابر از ' . ':date' . ' باشد.',
]
];
+76
View File
@@ -0,0 +1,76 @@
<?php
return [
'numbers' => [
'0' => '۰',
'1' => '۱',
'2' => '۲',
'3' => '۳',
'4' => '۴',
'5' => '۵',
'6' => '۶',
'7' => '۷',
'8' => '۸',
'9' => '۹',
],
'year_months' => [
'1' => 'وری',
'2' => 'غویی',
'3' => 'غبرګولی',
'4' => 'چنګاښ',
'5' => 'زمری',
'6' => 'وږی',
'7' => 'تله',
'8' => 'لړم',
'9' => 'لیندۍ',
'10' => 'مرغومی',
'11' => 'سلواغه',
'12' => 'کب',
],
'weekdays' => [
'1' => 'شنبه',
'2' => 'یکشنبه',
'3' => 'دوشنبه',
'4' => 'سه شنبه',
'5' => 'چهارشنبه',
'6' => 'پنج شنبه',
'7' => 'جمعه',
],
'date_units' => [
'second' => 'ثانیه',
'mintue' => 'دقیقه',
'hour' => 'ساعت',
'day' => 'روز',
'week' => 'هفته',
'month' => 'ماه',
'yaer' => 'سال',
'century' => 'قرن'
],
'pharse' => [
'am' => 'ق ظ',
'pm' => 'ب ظ',
'ante_meridiem' => 'قبل از ظهر',
'post_meridiem' => 'بعد از ظهر',
'th' => 'ام',
'ago' => 'قبل',
'now' => 'الان',
'later' => 'بعد',
],
'validation' => [
'jdate' => ':attribute' . ' تاریخ شمسی معتبر نمی باشد.',
'jdate_equal' => ':attribute' . ' تاریخ شمسی برابر ' . ':date' . ' نمی باشد.',
'jdate_not_equal' => ':attribute' . ' تاریخ شمسی نامساوی ' . ':date' . ' نمی باشد.',
'jdatetime' => ':attribute' . ' تاریخ و زمان شمسی معتبر نمی باشد.',
'jdatetime_equal' => ':attribute' . ' تاریخ و زمان شمسی مساوی ' . ':date' . ' نمی باشد.',
'jdatetime_not_equal' => ':attribute' . ' تاریخ و زمان شمسی نامساوی ' . ':date' . ' نمی باشد.',
'jdate_after' => ':attribute' . ' تاریخ شمسی باید بعد از ' . ':date' . ' باشد.',
'jdate_after_equal' => ':attribute' . ' تاریخ شمسی باید بعد یا برابر از ' . ':date' . ' باشد.',
'jdatetime_after' => ':attribute' . ' تاریخ و زمان شمسی باید بعد از ' . ':date' . ' باشد.',
'jdatetime_after_equal' => ':attribute' . ' تاریخ و زمان شمسی باید بعد یا برابر از ' . ':date' . ' باشد.',
'jdate_before' => ':attribute' . ' تاریخ شمسی باید قبل از ' . ':date' . ' باشد.',
'jdate_before_equal' => ':attribute' . ' تاریخ شمسی باید قبل یا برابر از ' . ':date' . ' باشد.',
'jdatetime_before' => ':attribute' . ' تاریخ و زمان شمسی باید قبل از ' . ':date' . ' باشد.',
'jdatetime_before_equal' => ':attribute' . ' تاریخ و زمان شمسی باید قبل یا برابر از ' . ':date' . ' باشد.',
]
];
@@ -1,8 +1,10 @@
<?php
namespace Hekmatinasser\Verta\Validation;
namespace Hekmatinasser\Verta\Laravel;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Arr;
use phpDocumentor\Reflection\Types\Static_;
class JalaliValidator
{
@@ -205,22 +207,27 @@ class JalaliValidator
public function replaceDateOrDatetime($message, $attribute, $rule, $parameters)
{
return $message;
$message = Verta::loadMessages();
return Arr::get(static::$messages, $message, $message);
}
public function replaceDateAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters)
{
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
$date = count($parameters) ? $parameters[0] : Verta::instance()->format($format);
$faDate = Verta::enToFaNumbers($date);
return str_replace([':date', ':fa-date'], [$date, $faDate], $message);
if(Verta::getLocale() != 'en') {
$en = Verta::getMessages('en');
$date = str_replace(array_values($en['numbers']), array_values(Verta::$messages['numbers']), $date);
}
return str_replace(':date', $date, $message);
}
public function replaceDateTimeAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters)
{
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
$date = count($parameters) ? $parameters[0] : Verta::instance()->format($format);
$faDate = Verta::enToFaNumbers($date);
return str_replace([':date', ':fa-date'], [$date, $faDate], $message);
$date = str_replace(array_values($en['numbers']), array_values(Verta::$messages['numbers']), $date);
return str_replace(':date', $date, $message);
}
}
+118
View File
@@ -0,0 +1,118 @@
<?php
namespace Hekmatinasser\Verta\Laravel;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
class VertaServiceProvider extends ServiceProvider
{
private $rules = [
'jdate' => [
'extend' => 'validateDate',
'replacer' => 'replaceDateOrDatetime'
],
'jdate_equal' => [
'extend' => 'validateDateEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
],
'jdate_not_equal' => [
'extend' => 'validateDateNotEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
],
'jdatetime' => [
'extend' => 'validateDateTime',
'replacer' => 'replaceDateOrDatetime'
],
'jdatetime_equal' => [
'extend' => 'validateDateTimeEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
],
'jdatetime_not_equal' => [
'extend' => 'validateDateTimeNotEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
],
'jdate_after' => [
'extend' => 'validateDateAfter',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
],
'jdate_after_equal' => [
'extend' => 'validateDateAfterEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
],
'jdatetime_after' => [
'extend' => 'validateDateTimeAfter',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
],
'jdatetime_after_equal' => [
'extend' => 'validateDateTimeAfterEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
],
'jdate_before' => [
'extend' => 'validateDateBefore',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
],
'jdate_before_equal' => [
'extend' => 'validateDateBeforeEqual',
'replacer' => 'replaceDateAfterOrBeforeOrEqual'
],
'jdatetime_before' => [
'extend' => 'validateDateTimeBefore',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
],
'jdatetime_before_equal' => [
'extend' => 'validateDateTimeBeforeEqual',
'replacer' => 'replaceDateTimeAfterOrBeforeOrEqual'
],
];
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->loadValidators();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('verta', function ($app) {
return new Verta;
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('verta');
}
public function loadValidators()
{
$className = JalaliValidator::class . '@';
foreach ($this->rules as $name => $methods) {
if (array_key_exists('extend', $methods) || array_key_exists('replacer', $methods)) {
Validator::extend($name, $className . $methods['extend']);
Validator::replacer($name, $className . $methods['replacer']);
}
}
}
}
+1 -2
View File
@@ -6,7 +6,6 @@ namespace Hekmatinasser\Verta\Traits;
use DateTimeZone;
use Hekmatinasser\Verta\Verta;
use Hekmatinasser\Verta\VertaInterface;
use InvalidArgumentException;
trait Accessor
@@ -43,7 +42,7 @@ trait Accessor
return (int) $this->format($formats[$name]);
case $name === 'quarter':
return (int) ceil($this->month / VertaInterface::MONTHS_PER_QUARTER);
return (int) ceil($this->month / static::MONTHS_PER_QUARTER);
case $name === 'timezone':
return $this->getTimezone()->getName();
+1 -1
View File
@@ -334,7 +334,7 @@ trait Comparison
*/
public function isWeekend()
{
return in_array($this->dayOfWeek, static::$weekendDays);
return in_array($this->dayOfWeek, static::$weekEndsAt);
}
/**
+7 -9
View File
@@ -36,6 +36,7 @@ trait Creator
try {
parent::__construct($datetime, static::createTimeZone($timezone));
parent::setTimezone(static::createTimeZone($timezone));
self::loadMessages();
} catch (Exception $exception) {
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
}
@@ -138,16 +139,13 @@ trait Creator
*
* @return static
*/
public static function parse($datetime , $timezone = null) {
$names = array_map(function ($value) {
return " $value ";
}, self::$monthYear);
public static function parse($datetime , $timezone = null)
{
$names = array_map(function ($value) { return " $value "; }, array_values(self::$messages['year_months']));
$values = array_map(function ($value) { return "-$value-"; }, range(1,12));
$values = array_map(function ($value) {
return "-$value-";
}, range(1,12));
$formatted = str_replace($names, $values, $datetime);
$formatted = str_replace(self::$monthYear, range(1,12), $formatted);
$formatted = str_replace(array_values(self::$messages['year_months']), range(1,12), $formatted);
$parse = date_parse($formatted);
if($parse['error_count'] > 0 || !self::isValidDate($parse['year'], $parse['month'], $parse['day']) || !self::isValidTime($parse['hour'], $parse['minute'], $parse['second'])) {
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
@@ -169,7 +167,7 @@ trait Creator
* @return static
*/
public static function parseFormat($format, $datetime, $timezone = null) {
$formatted = str_replace(self::$monthYear, range(1,12), $datetime);
$formatted = str_replace(array_values(self::$messages['year_months']), range(1,12), $datetime);
$parse = date_parse_from_format($format, $formatted);
if($parse['error_count'] > 0 || !self::isValidDate($parse['year'], $parse['month'], $parse['day']) || !self::isValidTime($parse['hour'], $parse['minute'], $parse['second'])) {
+2 -91
View File
@@ -14,6 +14,7 @@ trait Date
use Formatting;
use Modification;
use Transformation;
use Translator;
use Boundaries;
use Difference;
@@ -43,10 +44,7 @@ trait Date
*
* @var array
*/
protected static $weekendDays = [
VertaInterface::THURSDAY,
VertaInterface::FRIDAY,
];
protected static $weekendDays = [VertaInterface::THURSDAY, VertaInterface::FRIDAY];
/**
* number day in months gregorian
@@ -62,91 +60,4 @@ trait Date
*/
protected static $daysMonthJalali = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29];
/**
* month name jalali
*
* @var array
*/
protected static $monthYear = [
'فروردین',
'اردیبهشت',
'خرداد',
'تیر',
'مرداد',
'شهریور',
'مهر',
'آبان',
'آذر',
'دی',
'بهمن',
'اسفند',
];
/**
* Names of days of the week.
*
* @var array
*/
protected static $dayWeek = [
'شنبه',
'یکشنبه',
'دوشنبه',
'سه شنبه',
'چهارشنبه',
'پنج شنبه',
'جمعه',
];
/**
* unit date name.
*
* @var array
*/
protected static $unitName = [
'ثانیه',
'دقیقه',
'ساعت',
'روز',
'هفته',
'ماه',
'سال',
'قرن'
];
/**
* unit date number.
*
* @var array
*/
protected static $unitNumber = [
VertaInterface::SECONDS_PER_MINUTE,
VertaInterface::MINUTES_PER_HOUR,
VertaInterface::HOURS_PER_DAY,
VertaInterface::DAYS_PER_WEEK,
VertaInterface::WEEKS_PER_MONTH,
VertaInterface::MONTHS_PER_YEAR,
VertaInterface::YEARS_PER_DECADE,
];
/**
* arabic number.
*
* @var array
*/
protected static $arabicNumber = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
/**
* persian number.
*
* @var array
*/
protected static $persianNumber = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
/**
* english number.
*
* @var array
*/
protected static $englishNumber = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
}
+137 -285
View File
@@ -44,116 +44,114 @@ trait Formatting
* @param string $format for example 'Y-m-d H:i:s'
* @return string
*/
protected function date($format){
protected function date($format)
{
$timestamp = $this->getTimestamp();
list( $gYear, $gMonth, $gDay, $gWeek ) = explode( '-', parent::format('Y-m-d-w'));
list( $pYear, $pMonth, $pDay ) = static::getJalali( $gYear, $gMonth, $gDay );
$pWeek = ( $gWeek + 1 );
$pWeek = $gWeek + 1;
if ($pWeek >= 7) {
$pWeek = 0;
}
if ($format == '\\') {
$format = '//';
}
$lenghFormat = strlen( $format );
$i = 0;
$lengh = strlen($format);
$index = 0;
$result = '';
while ($i < $lenghFormat) {
$par = $format[$i];
if ($par == '\\') {
$result .= $format[++$i];
$i++;
while ($index < $lengh) {
$char = $format[$index];
if ($char == '\\') {
$result .= $format[++$index];
$index++;
continue;
}
switch ($par) {
$result .= $this->characterFormat($char, $timestamp, $pYear, $pMonth, $pDay, $pWeek);
$index++;
}
return $result;
}
/**
* The format of the outputted date charactre (jalali equivalent of php date() function)
*
* @param string $char
* @param integer $timestamp
* @param integer $pYear
* @param integer $pMonth
* @param integer $pDay
* @param integer $pWeek
* @return string
*/
private function characterFormat($char, $timestamp, $pYear, $pMonth, $pDay, $pWeek)
{
switch ($char) {
# Day
case 'd':
$result .= sprintf( '%02s', $pDay );
break;
return sprintf( '%02s', $pDay );
case 'D':
$result .= substr( static::$dayWeek[ $pWeek ], 0, 2 );
break;
return substr( self::$messages['weekdays'][$pWeek+1], 0, 2 );
case 'j':
$result .= $pDay;
break;
return $pDay;
case 'l':
$result .= static::$dayWeek[ $pWeek ];
break;
return self::$messages['weekdays'][$pWeek+1];
case 'N':
$result .= $pWeek + 1;
break;
return $pWeek+1;
case 'w':
$result .= $pWeek;
break;
return $pWeek;
case 'z':
$result .= $this->daysYear( $pMonth, $pDay );
break;
return $this->daysYear( $pMonth, $pDay );
case 'S':
$result .= self::NUMBER_TH;
break;
return static::$messages['phrase']['th'];
# Week
case 'W':
$result .= ceil( $this->daysYear( $pMonth, $pDay ) / 7 );
break;
return ceil( $this->daysYear( $pMonth, $pDay ) / 7 );
# Month
case 'F':
$result .= static::$monthYear[ $pMonth - 1 ];
break;
return self::$messages['year_months'][$pMonth];
case 'm':
$result .= sprintf( '%02s', $pMonth );
break;
return sprintf( '%02s', $pMonth );
case 'M':
$result .= substr( static::$monthYear[ $pMonth - 1 ], 0, 6 );
break;
return substr(self::$messages['year_months'][$pMonth], 0, 6 );
case 'n':
$result .= $pMonth;
break;
return $pMonth;
case 't':
$result .= static::isLeapYear( $pYear ) && ( $pMonth == 12 ) ? 30 : static::$daysMonthJalali[ intval( $pMonth ) - 1 ];
break;
return static::isLeapYear( $pYear ) && ( $pMonth == 12 ) ? 30 : static::$daysMonthJalali[ intval( $pMonth ) - 1 ];
# Years
case 'L':
$result .= intval( $this->isLeapYear( $pYear ) );
break;
return intval( $this->isLeapYear( $pYear ) );
case 'Y':
case 'o':
$result .= $pYear;
break;
return $pYear;
case 'y':
$result .= substr( $pYear, 2 );
break;
return substr( $pYear, 2 );
# Time
case 'a':
return static::$messages['phrase'][parent::format('a')];
case 'A':
if (parent::format( 'a' ) == 'am') {
$result .= ( ( $par == 'a' ) ? self::AM : self::ANTE_MERIDIEM );
} else {
$result .= ( ( $par == 'a' ) ? self::PM : self::POST_MERIDIEM );
}
break;
return static::$messages['pharse'][parent::format('a') == 'am' ? 'ante_meridiem' : 'post_meridiem'];
case 'B':
case 'g':
@@ -170,29 +168,21 @@ trait Formatting
case 'P':
case 'T':
case 'Z':
$result .= parent::format( $par );
break;
return parent::format( $char );
# Full Date/Time
case 'c':
$result .= ( $pYear . '-' . $pMonth . '-' . $pDay . ' ' . parent::format( 'H:i:s P' ) );
break;
return ( $pYear . '-' . $pMonth . '-' . $pDay . ' ' . parent::format( 'H:i:s P' ) );
case 'r':
$result .= ( substr( static::$dayWeek[ $pWeek ], 0, 2 ) . '، ' . $pDay . ' ' . substr( static::$monthYear[ $pMonth ], 0, 6 ) . ' ' . $pYear . ' ' . parent::format( 'H:i:s P' ) );
break;
return ( substr(self::$messages['weekdays'][$gWeek], 0, 2 ) . '، ' . $pDay . ' ' . substr(self::$messages['year_months'][$pMonth], 0, 6 ) . ' ' . $pYear . ' ' . parent::format( 'H:i:s P' ) );
case 'U':
$result .= $timestamp;
break;
return $timestamp;
default:
$result .= $par;
return $char;
}
$i++;
}
return $result;
}
/**
@@ -207,141 +197,29 @@ trait Formatting
list( $gYear, $gMonth, $gDay, $gWeek ) = explode( '-', parent::format('Y-m-d-w'));
list( $pYear, $pMonth, $pDay ) = static::getJalali( $gYear, $gMonth, $gDay );
$pWeek = ($gWeek + 1);
$pWeek = $gWeek + 1;
if ($pWeek >= 7) {
$pWeek = 0;
}
if ($format == '\\') {
$format = '//';
}
$lenghFormat = strlen($format);
$i = 0;
$lengh = strlen($format);
$index = 0;
$result = '';
$word = new Notowo(0, 'fa');
while ($i < $lenghFormat) {
$par = $format[$i];
if ($par == '\\') {
$result .= $format[++$i];
$i ++;
while ($index < $lengh) {
$char = $format[$index];
if ($char == '\\') {
$result .= $format[++$index];
$index++;
continue;
}
switch ($par) {
# Day
case 'd':
case 'j':
$result .= $word->getWord(strval($pDay));
break;
$output = $this->characterFormat($char, $timestamp, $pYear, $pMonth, $pDay, $pWeek);
$result .= (string) new Notowo($output, static::getLocale() == 'en' ? 'en' : 'fa' );
case 'D':
$result .= substr(static::$dayWeek[$pWeek], 0, 2);
break;
case 'l':
$result .= static::$dayWeek[$pWeek];
break;
case 'N':
$result .= $word->getWord(strval($pWeek + 1));
break;
case 'w':
$result .= $word->getWord(strval($pWeek));
break;
case 'z':
$result .= $word->getWord(strval($this->daysYear($pMonth, $pDay)));
break;
case 'S':
$result .= self::NUMBER_TH;
break;
# Week
case 'W':
$result .= $word->getWord(strval(ceil($this->daysYear($pMonth, $pDay) / 7)));
break;
# Month
case 'F':
$result .= static::$monthYear[$pMonth-1];
break;
case 'm':
case 'n':
$result .= $word->getWord(strval($pMonth));
break;
case 'M':
$result .= substr(static::$monthYear[$pMonth-1], 0, 6);
break;
case 't':
$result .= $word->getWord(strval(static::isLeapYear($pYear) && ($pMonth == 12) ? 30 : static::$daysMonthJalali[intval($pMonth)-1]));
break;
# Years
case 'L':
$result .= intval($this->isLeapYear($pYear));
break;
case 'Y':
case 'o':
$result .= $word->getWord(strval($pYear));
break;
case 'y':
$result .= $word->getWord(strval(substr($pYear, 2)));
break;
# Time
case 'a':
$result .= (parent::format('a') == 'am' ? self::AM : self::PM);
case 'A':
$result .= (parent::format('a') == 'am' ? self::ANTE_MERIDIEM : self::POST_MERIDIEM);
break;
case 'B':
case 'g':
case 'G':
case 'h':
case 'H':
case 's':
case 'u':
case 'i':
$result .= $word->getWord(strval(parent::format($par)));
break;
case 'e':
case 'I':
case 'O':
case 'P':
case 'T':
case 'Z':
$result .= parent::format($par);
break;
# Full Date/Time
case 'c':
$result .= $this->dateWord('Y, m, d, H:i:s P');
break;
case 'r':
$result .= $this->dateWord('l Y, m, d, H:i:s P');
break;
case 'U':
$result .= $word->getWord(strval($timestamp));
break;
default:
$result .= $par;
$index++;
}
$i ++;
}
return $result;
}
@@ -369,93 +247,50 @@ trait Formatting
*/
protected function strftime($format)
{
$str_format_code = array(
"%a",
"%A",
"%d",
"%e",
"%j",
"%u",
"%w",
"%U",
"%V",
"%W",
"%b",
"%B",
"%h",
"%m",
"%C",
"%g",
"%G",
"%y",
"%Y",
"%H",
"%I",
"%l",
"%M",
"%p",
"%P",
"%r",
"%R",
"%S",
"%T",
"%X",
"%z",
"%Z",
"%c",
"%D",
"%F",
"%s",
"%x",
"%n",
"%t",
"%%",
$strftime_date = array(
"%a" => "D",
"%A" => "l",
"%d" => "d",
"%e" => "j",
"%j" => "z",
"%u" => "N",
"%w" => "w",
"%U" => "W",
"%V" => "W",
"%W" => "W",
"%b" => "M",
"%B" => "F",
"%h" => "M",
"%m" => "m",
"%C" => "y",
"%g" => "y",
"%G" => "y",
"%y" => "y",
"%Y" => "Y",
"%H" => "H",
"%I" => "h",
"%l" => "g",
"%M" => "i",
"%p" => "A",
"%P" => "a",
"%r" => "h:i:s A",
"%R" => "H:i",
"%S" => "s",
"%T" => "H:i:s",
"%X" => "h:i:s",
"%z" => "H",
"%Z" => "H",
"%c" => "D j M H:i:s",
"%D" => "d/m/y",
"%F" => "Y-m-d",
"%s" => "U",
"%x" => "d/m/y",
"%n" => "\n",
"%t" => "\t",
"%%" => "%",
);
$date_format_code = array(
"D",
"l",
"d",
"j",
"z",
"N",
"w",
"W",
"W",
"W",
"M",
"F",
"M",
"m",
"y",
"y",
"y",
"y",
"Y",
"H",
"h",
"g",
"i",
"A",
"a",
"h:i:s A",
"H:i",
"s",
"H:i:s",
"h:i:s",
"H",
"H",
"D j M H:i:s",
"d/m/y",
"Y-m-d",
"U",
"d/m/y",
"\n",
"\t",
"%",
);
return str_replace($str_format_code, $date_format_code, $format);
return str_replace(array_keys($strftime_date), array_values($strftime_date), $format);
}
/**
@@ -539,20 +374,31 @@ trait Formatting
*/
public function formatDifference(Verta $v = null)
{
$units = [
static::SECONDS_PER_MINUTE,
static::MINUTES_PER_HOUR,
static::HOURS_PER_DAY,
static::DAYS_PER_WEEK,
static::WEEKS_PER_MONTH,
static::MONTHS_PER_YEAR,
static::YEARS_PER_DECADE,
static::DECADE_PER_CENTURY,
];
$difference = $this->diffSeconds($v);
$absolute = $difference < 0 ? self::POST : self::PRE;
$result .= static::$messages['phrase'][$difference < 0 ? 'later' : 'ago'];
$difference = abs($difference);
for ($j = 0; $difference >= static::$unitNumber[$j] and $j < count(static::$unitNumber) - 1; $j++) {
$difference /= static::$unitNumber[$j];
for ($unit = 0; $difference >= $units[$unit] and $unit < count($units) - 1; $unit++) {
$difference /= $units[$unit];
}
$difference = intval(round($difference));
if($difference === 0) {
return self::NOW;
return static::$messages['phrase']['now'];
}
return sprintf('%s %s %s', $difference, static::$unitName[$j], $absolute);
return sprintf('%s %s %s', $difference, array_values(static::$messages['date_units'])[$unit], $absolute);
}
public function formatWord($format)
@@ -568,7 +414,9 @@ trait Formatting
*/
public static function enToFaNumbers($string)
{
return str_replace(self::$englishNumber, self::$persianNumber, $string);
$en = static::getMessages('en');
$fa = static::getMessages('fa');
return str_replace(array_values($en['numbers']), array_values($fa['numbers']), $string);
}
/**
@@ -579,7 +427,9 @@ trait Formatting
*/
public static function faToEnNumbers($string)
{
return str_replace(self::$persianNumber, self::$englishNumber, $string);
$fa = static::getMessages('fa');
$en = static::getMessages('en');
return str_replace(array_values($fa['numbers']), array_values($en['numbers']), $string);
}
/**
@@ -590,7 +440,9 @@ trait Formatting
*/
public static function arToEnNumbers($string)
{
return str_replace(self::$arabicNumber, self::$englishNumber, $string);
$ar = static::getMessages('ar');
$en = static::getMessages('en');
return str_replace(array_values($ar['numbers']), array_values($en['numbers']), $string);
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
namespace Hekmatinasser\Verta\Traits;
trait Translator
{
/**
* List of custom localized messages.
*
* @var array
*/
public static $messages = array();
/**
* List of custom localized messages.
*
* @var array
*/
public static $locale;
/**
* Reset the format used to the default when type juggling a Verta instance to a string
*/
public static function resetLocale()
{
static::setStringFormat(static::DEFAULT_LOCALE);
}
/**
* Set the default locale
*
* @param string $locale
*/
public static function setLocale($locale)
{
static::$locale = $locale;
static::loadMessages($locale);
}
/**
* Get the default locale
*
* @param string $locale
*/
public static function getLocale()
{
return static::$locale ?: static::DEFAULT_LOCALE;
}
/**
* Return a singleton instance of Translator.
*
* @param string|null $locale optional initial locale ("fa" - by default)
*
* @return bool
*/
public static function loadMessages($locale = null)
{
if ($messages = static::getMessages($locale)) {
static::$messages = $messages;
return true;
}
return false;
}
/**
* Set messages of a locale and take file first if present.
*
* @param string $locale
*
* @return mixed
*/
public static function getMessages($locale = null)
{
if (file_exists($filename = dirname(__DIR__).'/Lang/'.($locale ?: static::getLocale()).'.php')) {
return require $filename;
}
return false;
}
/**
* Set messages of a locale and take file first if present.
*
* @param string $locale
* @param array $messages
*
* @return $this
*/
public function setMessages($locale, $messages)
{
if (static::loadMessages($locale)) {
static::$messages = array_merge(static::$messages, $messages);
}
static::$messages = $messages;
}
}
-49
View File
@@ -1,49 +0,0 @@
<?php
namespace Hekmatinasser\Verta\Validation;
use Illuminate\Support\Facades\Validator;
use Hekmatinasser\Verta\Validation\JalaliValidator;
class ValidatorLoader
{
const EXTEND_INDEX = 0;
const REPLACER_INDEX = 1;
const DATE_VALIDATORS = [
'jdate' => ['validateDate', 'replaceDateOrDatetime'],
'jdate_equal' => ['validateDateEqual', 'replaceDateAfterOrBeforeOrEqual'],
'jdate_not_equal' => ['validateDateNotEqual', 'replaceDateAfterOrBeforeOrEqual'],
'jdatetime' => ['validateDateTime', 'replaceDateOrDatetime'],
'jdatetime_equal' => ['validateDateTimeEqual', 'replaceDateTimeAfterOrBeforeOrEqual'],
'jdatetime_not_equal' => ['validateDateTimeNotEqual', 'replaceDateTimeAfterOrBeforeOrEqual'],
'jdate_after' => ['validateDateAfter', 'replaceDateAfterOrBeforeOrEqual'],
'jdate_after_equal' => ['validateDateAfterEqual', 'replaceDateAfterOrBeforeOrEqual'],
'jdatetime_after' => ['validateDateTimeAfter', 'replaceDateTimeAfterOrBeforeOrEqual'],
'jdatetime_after_equal' => ['validateDateTimeAfterEqual', 'replaceDateTimeAfterOrBeforeOrEqual'],
'jdate_before' => ['validateDateBefore', 'replaceDateAfterOrBeforeOrEqual'],
'jdate_before_equal' => ['validateDateBeforeEqual', 'replaceDateAfterOrBeforeOrEqual'],
'jdatetime_before' => ['validateDateTimeBefore', 'replaceDateTimeAfterOrBeforeOrEqual'],
'jdatetime_before_equal' => ['validateDateTimeBeforeEqual', 'replaceDateTimeAfterOrBeforeOrEqual'],
];
public static function loadValidators()
{
$validatorClass = JalaliValidator::class . '@';
foreach (self::DATE_VALIDATORS as $name => $methods) {
if (!is_array($methods) || count($methods) < 1) {
continue;
}
Validator::extend($name, $validatorClass . $methods[self::EXTEND_INDEX]);
if (array_key_exists(self::REPLACER_INDEX, $methods)) {
Validator::replacer($name, $validatorClass . $methods[self::REPLACER_INDEX]);
}
}
}
}
+3 -10
View File
@@ -20,7 +20,7 @@ interface VertaInterface
/**
* Number unit in date
*/
const YEARS_PER_CENTURY = 100;
const DECADE_PER_CENTURY = 10;
const YEARS_PER_DECADE = 10;
const MONTHS_PER_YEAR = 12;
const MONTHS_PER_QUARTER = 3;
@@ -37,14 +37,7 @@ interface VertaInterface
const DEFAULT_STRING_FORMAT = 'Y-m-d H:i:s';
/**
* Default format to use for __toString method when type juggling occurs.
* Word use in format datetime.
*/
const AM = 'ق.ظ';
const PM = 'ب.ظ';
const ANTE_MERIDIEM = 'قبل از ظهر';
const POST_MERIDIEM = 'بعد از ظهر';
const NUMBER_TH = ' ام';
const PRE = 'قبل';
const NOW = 'الان';
const POST = 'بعد';
const DEFAULT_LOCALE = 'fa';
}
-44
View File
@@ -1,44 +0,0 @@
<?php
namespace Hekmatinasser\Verta;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\ServiceProvider;
use Hekmatinasser\Verta\Validation\ValidatorLoader;
class VertaServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
ValidatorLoader::loadValidators();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind('verta', function ($app) {
return new Verta;
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('verta');
}
}
-17
View File
@@ -1,17 +0,0 @@
<?php
return [
'jdate' => ':attribute x ليس تاريخ جلالي صالحًا',
'jdate_equal' => ':attribute لا يساوي تاريخ جلالي :date',
'jdate_not_equal' => 'ال :attribute لا يساوي تاريخ جلالي :date',
'jdatetime' => ':attribute غير صالح لتاريخ جلالي',
'jdatetime_equal' => ':attribute ليس مساويًا لـ تاريخ جلالي :date',
'jdatetime_not_equal' => ':attribute لا يساوي تاريخ جلالي :date',
'jdate_after' => 'يجب أن يكون :attribute تاريخ جلالي بعد :date.',
'jdate_after_equal' => 'يجب أن يكون :attribute هو تاريخ جلالي بعد :date أو يساوي.',
'jdatetime_after' => 'يجب أن يكون :attribute عبارة عن تاريخ جلالي بعد :date.',
'jdatetime_after_equal' => 'يجب أن يكون :attribute عبارة عن تاريخ جلالي بعد :date أو يساوي.',
'jdate_before' => 'يجب أن يكون :attribute تاريخ جلالي قبل :date.',
'jdate_before_equal' => 'يجب أن يكون :attribute تاريخ جلالي قبل :date أو يساوي.',
'jdatetime_before' => 'يجب أن يكون :attribute هو تاريخ جلالي قبل :date.',
'jdatetime_before_equal' => 'يجب أن يكون :attribute هو تاريخ جلالي قبل أو يساوي :date.',
];
-18
View File
@@ -1,18 +0,0 @@
<?php
return [
'jdate' => 'The :attribute is not valid Jalali date',
'jdate_equal' => 'The :attribute is not equal Jalali date :date',
'jdate_not_equal' => 'The :attribute is\'nt not equal Jalali date :date',
'jdatetime' => 'The :attribute is not valid Jalali datetime',
'jdatetime_equal' => 'The :attribute is not equal Jalali datetime :date',
'jdatetime_not_equal' => 'The :attribute is\'nt not equal Jalali datetime :date',
'jdate_after' => 'The :attribute must be a Jalali date after :date.',
'jdate_after_equal' => 'The :attribute must be a Jalali date after or equal :date.',
'jdatetime_after' => 'The :attribute must be a Jalali datetime after :date.',
'jdatetime_after_equal' => 'The :attribute must be a Jalali datetime after or equal :date.',
'jdate_before' => 'The :attribute must be a Jalali date before :date.',
'jdate_before_equal' => 'The :attribute must be a Jalali date before or equal :date.',
'jdatetime_before' => 'The :attribute must be a Jalali datetime before :date.',
'jdatetime_before_equal' => 'The :attribute must be a Jalali datetime before or equal :date.',
];
-18
View File
@@ -1,18 +0,0 @@
<?php
return [
'jdate' => ':attribute' . ' تاریخ شمسی معتبر نمی باشد.',
'jdate_equal' => ':attribute' . ' تاریخ شمسی برابر ' . ':fa-date' . ' نمی باشد.',
'jdate_not_equal' => ':attribute' . ' تاریخ شمسی نامساوی ' . ':fa-date' . ' نمی باشد.',
'jdatetime' => ':attribute' . ' تاریخ و زمان شمسی معتبر نمی باشد.',
'jdatetime_equal' => ':attribute' . ' تاریخ و زمان شمسی مساوی ' . ':fa-date' . ' نمی باشد.',
'jdatetime_not_equal' => ':attribute' . ' تاریخ و زمان شمسی نامساوی ' . ':fa-date' . ' نمی باشد.',
'jdate_after' => ':attribute' . ' تاریخ شمسی باید بعد از ' . ':fa-date' . ' باشد.',
'jdate_after_equal' => ':attribute' . ' تاریخ شمسی باید بعد یا برابر از ' . ':fa-date' . ' باشد.',
'jdatetime_after' => ':attribute' . ' تاریخ و زمان شمسی باید بعد از ' . ':fa-date' . ' باشد.',
'jdatetime_after_equal' => ':attribute' . ' تاریخ و زمان شمسی باید بعد یا برابر از ' . ':fa-date' . ' باشد.',
'jdate_before' => ':attribute' . ' تاریخ شمسی باید قبل از ' . ':fa-date' . ' باشد.',
'jdate_before_equal' => ':attribute' . ' تاریخ شمسی باید قبل یا برابر از ' . ':fa-date' . ' باشد.',
'jdatetime_before' => ':attribute' . ' تاریخ و زمان شمسی باید قبل از ' . ':fa-date' . ' باشد.',
'jdatetime_before_equal' => ':attribute' . ' تاریخ و زمان شمسی باید قبل یا برابر از ' . ':fa-date' . ' باشد.',
];