-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix locale inconsistencies between intl and moment
The month names used by moment are different than the ones in intl, leading to validation errors. Get the month and weekday names from intl, and patch the moment locale to use those.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace nkovacs\datetimepicker; | ||
|
||
use Yii; | ||
use IntlDateFormatter; | ||
|
||
class LocaleConverter | ||
{ | ||
protected static $cache = []; | ||
|
||
public static function getMonths($format, $locale = null) { | ||
if ($locale === null) { | ||
$locale = Yii::$app->language; | ||
} | ||
|
||
if (!isset(static::$cache['months'][$locale][$format])) { | ||
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, Yii::$app->timeZone, null, $format); | ||
$localized = []; | ||
for ($month = 0; $month < 12; $month++) { | ||
$localized[] = $formatter->format([ | ||
'tm_sec' => 0, | ||
'tm_min' => 0, | ||
'tm_hour' => 12, | ||
'tm_mday' => 15, | ||
'tm_mon' => $month, | ||
'tm_year' => 2016, | ||
]); | ||
} | ||
static::$cache['months'][$locale][$format] = $localized; | ||
} | ||
return static::$cache['months'][$locale][$format]; | ||
} | ||
|
||
public static function getWeekdays($format, $locale = null) { | ||
if ($locale === null) { | ||
$locale = Yii::$app->language; | ||
} | ||
if (!isset(static::$cache['weekdays'][$locale][$format])) { | ||
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, Yii::$app->timeZone, null, $format); | ||
$localized = []; | ||
for ($day = 0; $day < 7; $day++) { | ||
$localized[] = $formatter->format([ | ||
'tm_sec' => 0, | ||
'tm_min' => 0, | ||
'tm_hour' => 12, | ||
'tm_mon' => 6, | ||
'tm_year' => 2016, | ||
'tm_mday' => 2 + $day, // 2016-06-02 is a sunday | ||
]); | ||
} | ||
static::$cache['weekdays'][$locale][$format] = $localized; | ||
} | ||
return static::$cache['weekdays'][$locale][$format]; | ||
} | ||
} |