Skip to content

Commit 99897da

Browse files
Platform: Add translations to flatpickr - refs BT#20928
1 parent eecd38f commit 99897da

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

public/main/inc/lib/formvalidator/Element/DateTimePicker.php

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,64 @@ public function setValue($value)
6969
*/
7070
private function getElementJS()
7171
{
72-
$js = null;
72+
$localeCode = $this->getLocaleCode();
7373
$id = $this->getAttribute('id');
74-
//timeFormat: 'hh:mm'
75-
$js .= "<script>
76-
$(function() {
77-
var config = {
78-
altInput: true,
79-
altFormat: '".get_lang('F d, Y')." ".get_lang('at')." H:i',
80-
enableTime: true,
81-
dateFormat: 'Y-m-d H:i',
82-
time_24hr: true,
83-
wrap: false,
84-
locale: {
85-
firstDayOfWeek: 1
86-
}
87-
};
88-
$('#{$id}').flatpickr(config);
74+
75+
$localeScript = '';
76+
if ($localeCode !== 'en') {
77+
$localeScript = '<script async="false" src="/build/flatpickr/l10n/' . $localeCode . '.js"></script>';
78+
}
79+
80+
$js = $localeScript . "<script>
81+
document.addEventListener('DOMContentLoaded', function () {
82+
flatpickr('#{$id}', {
83+
locale: '{$localeCode}',
84+
altInput: true,
85+
altFormat: '".get_lang('F d, Y')." ".get_lang('at')." H:i',
86+
enableTime: true,
87+
dateFormat: 'Y-m-d H:i',
88+
time_24hr: true,
89+
wrap: false
8990
});
91+
});
9092
</script>";
9193

9294
return $js;
9395
}
96+
97+
/**
98+
* Retrieves the locale code based on user and course settings.
99+
* Extracts the ISO language code from user or course settings and checks
100+
* its availability in the list of supported locales. Returns 'en' if the language
101+
* is not available.
102+
*
103+
* @return string Locale code (e.g., 'es', 'en', 'fr').
104+
*/
105+
private function getLocaleCode()
106+
{
107+
$locale = api_get_language_isocode();
108+
$userInfo = api_get_user_info();
109+
if (is_array($userInfo) && !empty($userInfo['language'])) {
110+
$locale = $userInfo['language'];
111+
}
112+
113+
$courseInfo = api_get_course_info();
114+
if (isset($courseInfo)) {
115+
$locale = $courseInfo['language'];
116+
}
117+
118+
$localeCode = explode('_', $locale)[0];
119+
$availableLocales = [
120+
'ar', 'ar-dz', 'at', 'az', 'be', 'bg', 'bn', 'bs', 'cat', 'ckb', 'cs', 'cy', 'da', 'de',
121+
'eo', 'es', 'et', 'fa', 'fi', 'fo', 'fr', 'ga', 'gr', 'he', 'hi', 'hr', 'hu', 'hy',
122+
'id', 'is', 'it', 'ja', 'ka', 'km', 'ko', 'kz', 'lt', 'lv', 'mk', 'mn', 'ms', 'my',
123+
'nl', 'nn', 'no', 'pa', 'pl', 'pt', 'ro', 'ru', 'si', 'sk', 'sl', 'sq', 'sr', 'sr-cyr',
124+
'sv', 'th', 'tr', 'uk', 'uz', 'uz_latn', 'vn', 'zh', 'zh-tw'
125+
];
126+
if (!in_array($localeCode, $availableLocales)) {
127+
$localeCode = 'en';
128+
}
129+
130+
return $localeCode;
131+
}
94132
}

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ Encore
9898
pattern: /(jquery.qtip.css)$/,
9999
to: 'libs/qtip2/dist/jquery.qtip.css'
100100
},
101+
{
102+
from: './node_modules/flatpickr/dist/l10n',
103+
to: 'flatpickr/l10n/[name].[ext]'
104+
},
101105
//{from: './node_modules/ckeditor4/', to: 'libs/ckeditor/[path][name].[ext]', pattern: /\.(js|css)$/, includeSubdirectories: false},
102106
//{from: './node_modules/ckeditor4/adapters', to: 'libs/ckeditor/adapters/[path][name].[ext]'},
103107
//{from: './node_modules/ckeditor4/lang', to: 'libs/ckeditor/lang/[path][name].[ext]'},

0 commit comments

Comments
 (0)