Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor datefield to use proper fallbacks for HTML5 #8362

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/Forms/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ protected function getInternalFormatter()
public function getAttributes()
{
$attributes = parent::getAttributes();

$attributes['lang'] = i18n::convert_rfc1766($this->getLocale());
$lang = $this->getLocale();
$attributes['lang'] = i18n::convert_rfc1766($lang);

if ($this->getHTML5()) {
$attributes['min'] = $this->getMinDate();
Expand All @@ -284,6 +284,7 @@ public function getSchemaDataDefaults()
$defaults = parent::getSchemaDataDefaults();
return array_merge($defaults, [
'lang' => i18n::convert_rfc1766($this->getLocale()),
'isoLang' => i18n::convert_rfc1766(DBDate::ISO_LOCALE),
'data' => array_merge($defaults['data'], [
'html5' => $this->getHTML5(),
'min' => $this->getMinDate(),
Expand Down Expand Up @@ -445,10 +446,6 @@ public function validate($validator)
*/
public function getLocale()
{
// Use iso locale for html5
if ($this->getHTML5()) {
return DBDate::ISO_LOCALE;
}
return $this->locale ?: i18n::get_locale();
}

Expand Down
4 changes: 3 additions & 1 deletion src/Forms/DatetimeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public function getAttributes()
{
$attributes = parent::getAttributes();

$attributes['lang'] = i18n::convert_rfc1766($this->getLocale());
$lang = $this->getLocale();
$attributes['lang'] = i18n::convert_rfc1766($lang);

if ($this->getHTML5()) {
$attributes['min'] = $this->internalToFrontend($this->getMinDatetime());
Expand All @@ -114,6 +115,7 @@ public function getSchemaDataDefaults()
$defaults = parent::getSchemaDataDefaults();
return array_merge($defaults, [
'lang' => i18n::convert_rfc1766($this->getLocale()),
'isoLang' => i18n::convert_rfc1766(DBDate::ISO_LOCALE),
'data' => array_merge($defaults['data'], [
'html5' => $this->getHTML5(),
'min' => $this->internalToFrontend($this->getMinDatetime()),
Expand Down
9 changes: 5 additions & 4 deletions src/View/Requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,18 @@ public static function include_in_response(HTTPResponse $response)
* directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js,
* etc.
*
* @param string $langDir The JavaScript lang directory, relative to the site root, e.g.,
* @param string $langDir The JavaScript lang directory, relative to the site root, e.g.,
* 'framework/javascript/lang'
* @param bool $return Return all relative file paths rather than including them in
* @param bool $return Return all relative file paths rather than including them in
* requirements
* @param bool $langOnly @deprecated 4.1.0:5.0.0 as i18n.js should be included manually in your project
*
* @param bool $convertRFC1766 converts file candidates to be RFC1766 compatible
* @return array
*/
public static function add_i18n_javascript($langDir, $return = false, $langOnly = false)
public static function add_i18n_javascript($langDir, $return = false, $langOnly = false, $convertRFC1766 = false)
{
return self::backend()->add_i18n_javascript($langDir, $return);
return self::backend()->add_i18n_javascript($langDir, $return, $convertRFC1766);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/View/Requirements_Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,10 @@ public function includeInResponse(HTTPResponse $response)
* @param bool $return Return all relative file paths rather than including them in
* requirements
*
* @param bool $convertRFC1766 converts file candidates to be RFC1766 compatible
* @return array|null All relative files if $return is true, or null otherwise
*/
public function add_i18n_javascript($langDir, $return = false)
public function add_i18n_javascript($langDir, $return = false, $convertRFC1766 = false)
{
$langDir = ModuleResourceLoader::singleton()->resolvePath($langDir);

Expand All @@ -1012,6 +1013,9 @@ public function add_i18n_javascript($langDir, $return = false)
i18n::get_locale() . '.js',
);
foreach ($candidates as $candidate) {
if ($convertRFC1766) {
$candidate = i18n::convert_rfc1766($candidate);
}
$relativePath = Path::join($langDir, $candidate);
$absolutePath = Director::getAbsFile($relativePath);
if (file_exists($absolutePath)) {
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public static function get_closest_translation($locale)
*/
public static function convert_rfc1766($locale)
{
return str_replace('_', '-', $locale);
return strtolower(str_replace('_', '-', $locale));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/php/Forms/FormSchemaTest/testSchemaValidation.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"min": null,
"max": null,
"maxlength": null
}
},
"isoLang": "en-US"
},
{
"name": "Number",
Expand Down