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
| 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
Comment #1
djevans commentedIt 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.
Comment #2
djevans commentedComment #3
djevans commentedComment #5
djevans commentedComment #6
djevans commentedRe-rolled patch against 8.0-alpha6.
Comment #7
djevans commentedComment #9
djevans commentedComment #10
djevans commentedUpdated assertion in LanguageCustomLanguageConfigurationTest to match new error message text.
Comment #11
djevans commentedComment #12
badrange commentedI 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).
Comment #13
badrange commentedTried the patch against Drupal 8.0-alpha8 on simplytest.me and it (perhaps not too surprisingly) worked there too.
Comment #14
jhedstromNeeds a reroll, and probably tests too.
Comment #15
djevans commentedRerolled for 8.0-beta3, with an updated error message to match the help text for the 'Language code' field.
Comment #16
jhedstrom@djevans would it be possible to add a new test assertion that shows the current bug, but passes with the fix in place?
Eg, a test that breaks without this change. This should be fairly straightforward by copying this code:
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 :)
Comment #17
djevans commentedOK, as requested:
A patch containing only an updated test (which should fail), and a combined patch with the updated code.
Comment #19
jhedstromPerfect, thanks @djevans!
I've updated the issue summary with a beta phase evaluation.
Comment #20
alexpottI'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?
Comment #21
gábor hojtsySo 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.
Comment #22
gábor hojtsyComment #23
alexpottLet'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.
Comment #24
jhedstromI added the beta phase evaluation already.
Comment #25
gábor hojtsy@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.
Comment #26
jhedstromI'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.
Comment #27
gábor hojtsyThanks, edited the change record draft slightly for completeness. I think this looks good to go again.
Comment #28
gábor hojtsyComment #29
alexpottCommitted 7d01643 and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation.
Comment #31
gábor hojtsyThanks all!
Comment #32
garphyBackport for D7
Comment #33
xjmComment #40
quietone commentedThis was committed
Comment #42
xjmRestoring credits.