I have a need to create a Spanish site targeting a Latin American audience. It is my understanding that valid language code would be es-419 for this, but Drupal doesn't allow me to create this, instead it throws an error message:

Notice: Undefined index: langcode in language_admin_edit_form_validate() (line 365 of core/modules/language/language.admin.inc).
 may only contain characters a-z, underscores, or hyphens.

The W3C page that is linked on the very same page where you can define a custom language even lists this language code as one of it's examples:
"es-419 Spanish as used in Latin America"
http://www.w3.org/International/articles/language-tags/

Other use cases for this is if you wish to create an English language site targeting a Middle Eastern audience, where (I think) it would make sense to use en-145 as language code:

http://schneegans.de/lv/?tags=en-145&format=text

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because valid languages are not allowed
Unfrozen changes Unfrozen because it is a bug in both Drupal 7 and Drupal 8

Comments

djevans’s picture

It looks like this needs a change to LanguageFormBase::validateCommon().

The following regex pattern (taken from the W3C's RDF 1.1 Concepts and Abstract Syntax, and based on BCP47, seems suitable:

@^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$@

I'd welcome suggestions on how to improve the UX on this.

djevans’s picture

Issue summary: View changes
StatusFileSize
new1.37 KB
djevans’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: language-codes-bcp-47-validate-2023091-2.patch, failed testing.

djevans’s picture

djevans’s picture

Re-rolled patch against 8.0-alpha6.

djevans’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: language-codes-bcp-47-validate-2023091-6.patch, failed testing.

djevans’s picture

djevans’s picture

Updated assertion in LanguageCustomLanguageConfigurationTest to match new error message text.

djevans’s picture

Status: Needs work » Needs review
badrange’s picture

I wanted to test the patch, but it didn't apply to the most recent version of Drupal core. Attached is a re-roll against 8.x as checked out today.

I created a test d8 site, applied the patch, and was now able to add es-419 as langauge code for Spanish (Latin America).

badrange’s picture

Tried the patch against Drupal 8.0-alpha8 on simplytest.me and it (perhaps not too surprisingly) worked there too.

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll, +Needs tests

Needs a reroll, and probably tests too.

djevans’s picture

Status: Needs work » Needs review
StatusFileSize
new2.55 KB

Rerolled for 8.0-beta3, with an updated error message to match the help text for the 'Language code' field.

jhedstrom’s picture

@djevans would it be possible to add a new test assertion that shows the current bug, but passes with the fix in place?

+++ b/core/modules/language/src/Form/LanguageFormBase.php
@@ -100,8 +100,11 @@ public function commonForm(array &$form) {
-    if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state->getValue('langcode'))) {
...
+    if (!isset($form['langcode_view']) && !preg_match('@^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$@', $form_state->getValue('langcode'))) {

Eg, a test that breaks without this change. This should be fairly straightforward by copying this code:

  // Test validation of invalid values.
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => 'white space',
      'label' => '<strong>evil markup</strong>',
      'direction' => LanguageInterface::DIRECTION_LTR,
    );
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));

and posting the valid langcode.

If you upload just the test patch (which will fail) and then the combined patch including the fix, this shows a) the fix works and b) the current way is broken :)

djevans’s picture

OK, as requested:
A patch containing only an updated test (which should fail), and a combined patch with the updated code.

jhedstrom’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Perfect, thanks @djevans!

I've updated the issue summary with a beta phase evaluation.

alexpott’s picture

Assigned: Unassigned » gábor hojtsy
Status: Reviewed & tested by the community » Needs review

I'd like @GaborHojtsy to have a quick look at this before commit. Also, removing the ability to have an underscore? How is migrate going to cope with that?

gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs reroll, -Needs tests +D8MI, +sprint, +language-base

So the UI already refers to W3C language codes (note not language tags, we refrained from referring to language *tags* so far). It is true that according to the W3C definitions, the language code would be part of the language tag, not the whole...

So to align the validation with that definition makes sense. As for removing the possibility of underscores, we certainly have not been using it on localize.drupal.org. Drupal 7 and 6 are both referring to 'RFC 4646 compliant language identifier. Language codes typically use a country code, and optionally, a script or regional variant name. Examples: "en", "en-US" and "zh-Hant".' with a link to http://www.ietf.org/rfc/rfc4646.txt, which does not define _ as a valid separator either. So I don't know when did we use to tell people to use an underscore there.

As for those migrating sites, the validation only happens when they save the language again, it is on the form. So if they used a language code that they were not supposed to in Drupal 6 or 7 they will need to work around that in Drupal 8 with a form alter or something... I think it makes sense to stop that legacy underscore support sometime.

gábor hojtsy’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record, +Needs issue summary update

Let's get a small change record to inform developers that support for _ is being removed.

Also, this issue is a normal bug so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary.

jhedstrom’s picture

I added the beta phase evaluation already.

gábor hojtsy’s picture

@jhedstrrm: looking at which changes are unfrozen (https://www.drupal.org/core/beta-changes) this does not seem to be any of them; however it looks like this would be a prioritized change given that Drupal 7 should allow for numbers the same way (even if underscores will not be removed there), so the summary needs an update.

jhedstrom’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record, -Needs issue summary update

I've updated the beta phase evaluation to mention that this is unfrozen due to needing a backport. I also took a stab at adding a change record.

gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, edited the change record draft slightly for completeness. I think this looks good to go again.

gábor hojtsy’s picture

Assigned: gábor hojtsy » Unassigned
alexpott’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed 7d01643 and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation.

  • alexpott committed 7d01643 on 8.0.x
    Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...
gábor hojtsy’s picture

Issue tags: -sprint

Thanks all!

garphy’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new2.72 KB

Backport for D7

xjm’s picture

  • alexpott committed 7d01643 on 8.1.x
    Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...

  • alexpott committed 7d01643 on 8.3.x
    Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...

  • alexpott committed 7d01643 on 8.3.x
    Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...

  • alexpott committed 7d01643 on 8.4.x
    Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...

  • alexpott committed 7d01643 on 8.4.x
    Issue #2023091 by djevans, badrange: Drupal does not allow W3C compliant...

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.

quietone’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Closed (outdated) » Fixed

This was committed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

xjm’s picture

Restoring credits.