Page MenuHomePhabricator

"de-formal" is no valid HTML language code
Closed, DuplicatePublic

Description

de-formal ("formal" German) is one of MediaWiki's supported language codes. When using that code, pages created by MediaWiki start this way:

<!DOCTYPE html>
<html lang="de-formal" dir="ltr" class="client-nojs">
<head>

However, HTML validators complain that de-formal is no valid HTML language code:

line 2 column 53 - Error: Bad value “de-formal” for attribute “lang” on element “html”: Bad variant subtag “formal”.

The closest valid HTML language codes are de or de-x-formal.

I think there are many ways to fix this. Complicated ones like renaming the MediaWiki language code de-formal to de-x-formal and quick and dirty ones like tweaking the function getHtmlCode() in Language.php or wfBCP47 in GlobalFunctions.php.

To start, I suggest to add a test:

diff --git a/tests/phpunit/includes/GlobalFunctions/wfBCP47Test.php b/tests/phpunit/includes/GlobalFunctions/wfBCP47Test.php
index 082fe22..5a3a0ce 100644
--- a/tests/phpunit/includes/GlobalFunctions/wfBCP47Test.php
+++ b/tests/phpunit/includes/GlobalFunctions/wfBCP47Test.php
@@ -112,6 +112,9 @@ class WfBCP47Test extends MediaWikiTestCase {
                        [ 'zh-cn-a-myext-x-private', 'zh-CN-a-myext-x-private' ],
                        [ 'en-a-myext-b-another', 'en-a-myext-b-another' ],
 
+                       # Non-BCP47 compliant codes used in MediaWiki should translate to compliant codes
+                       [ 'de-formal', 'de' ],
+
                        # Invalid:
                        // de-419-DE
                        // a-DE

Thank you! :-)

Event Timeline

For me, the following fix works (although I admit it's not a very general solution):

index 86bca98..a12faa2 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -4212,7 +4212,8 @@ class Language {
         */
        public function getHtmlCode() {
                if ( is_null( $this->mHtmlCode ) ) {
-                       $this->mHtmlCode = wfBCP47( $this->getCode() );
+                       if ($this->getCode() == 'de-formal') $this->mHtmlCode = 'de';
+                       else $this->mHtmlCode = wfBCP47( $this->getCode() );
                }
                return $this->mHtmlCode;
        }