Skip to content

Commit

Permalink
Fix locale inconsistencies between intl and moment
Browse files Browse the repository at this point in the history
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
nkovacs committed Feb 10, 2016
1 parent a0b03e4 commit 06e1139
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,29 @@ protected function registerLocale($view)
if ($file === false) {
return;
}

$view->registerJsFile(Yii::$app->assetManager->publish($file)[1], [
'depends' => '\nkovacs\datetimepicker\MomentAsset',
]);

// use the same strings intl uses, to avoid validation errors.
if (extension_loaded('intl')) {
$weekdays = LocaleConverter::getWeekdays('EEEE', $this->locale); // dddd
$weekdaysShort = LocaleConverter::getWeekdays('EEE', $this->locale); // ddd
$weekdaysMin = LocaleConverter::getWeekdays('EEEEEE', $this->locale); // dd
$months = LocaleConverter::getMonths('MMMM', $this->locale);
$monthsShort = LocaleConverter::getMonths('MMM', $this->locale);
$override = Json::encode([
'months' => $months,
'monthsShort' => $monthsShort,
'weekdays' => $weekdays,
'weekdaysShort' => $weekdaysShort,
'weekdaysMin' => $weekdaysMin,
]);
// POS_END is the default for js files.
$view->registerJs("moment.locale('$locale', $override)", \yii\web\View::POS_END);
}

$this->clientOptions['locale'] = $locale;
}

Expand Down
56 changes: 56 additions & 0 deletions LocaleConverter.php
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];
}
}

0 comments on commit 06e1139

Please # to comment.