Does not support langcode url prefixes more complex than 2 letters (e.g. zh-hans, zh-hant, haw)

Created on 7 November 2024, 15 days ago

Problem/Motivation

I want to configure this module to work with langcode prefixes such as zh-hans (Chinese, Simplified), zh-hant (Chinese, Traditional), haw (Hawaiian), or even custom languages used for testing (e.g. test).

Steps to reproduce

  1. Add the language "Chinese, Simplified" to the site
  2. Under Languages > Detection and selection, configure ensure its URL prefix is configured as the standard "zh-hans"
  3. Add a country named, e.g. "Singapore" with the code "sg"
  4. Assign Chinese, Simplified to Singapore
  5. Create a node (e.g. node 1) source language English, with a zh-hans translation
  6. Go to a URL like "/zh-hans-sg/node/1"

Expected results: Stay on the page, see the node in its zh-hans translation

Actual results: Page not found

Proposed resolution

This module makes the assumption (in PathUtility) that langcodes are exactly 2 letters long. It does not correctly detect the language when this is not the case. Suggest making path-prefix-detection configuration-aware, so it checks not for something that "looks like" /langcode-countrycode/, but actually tests against the enabled languages/countries.

Remaining tasks

Replace PathUtility with a helper that detects the prefix by considering the actual configuration on the site, not just substr() etc

User interface changes

None

API changes

When "Request fallback" is enabled, a path with an unrecognised /langcode-countrycode/ prefix that nonetheless "looks right" was redirected to remove that prefix. Now this scenario results in a page not found. An example (from the tests) would be /xx-yy/test

Data model changes

None

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom jamsilver West Midlands, UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @jamsilver
  • Pipeline finished with Success
    15 days ago
    #332188
  • Pipeline finished with Success
    15 days ago
    #332191
  • 🇬🇧United Kingdom jamsilver West Midlands, UK

    I've raised a MR that replaces PathUtility with a new PathPrefixHelper service that does the stronger check for configured languages/countries rather than a general regexp.

    It incorporates the work in Compatibility Drupal 10.3^ Active , mainly so the pipeline passes.

  • 🇬🇧United Kingdom jamsilver West Midlands, UK
  • 🇬🇧United Kingdom jamsilver West Midlands, UK
  • 🇬🇧United Kingdom jamsilver West Midlands, UK
  • 🇩🇪Germany simonbaese Berlin

    This feature is on the roadmap. See Development Plan on the module page. In general, I thought it would be good to use plugins for the prefix negotiation. I can imagine scenarios where it would make sense to chain the negotiation.

  • 🇬🇧United Kingdom jamsilver West Midlands, UK

    Interesting. I find the module's current approach (i.e. /LANGCODE-COUNTRYCODE/where LANGCODE is configured per language and COUNTRYCODE is configured per country) to be good and sufficient.

    (Well, I suppose I found it disorienting that the langcode url prefix used is the one on configured on /admin/config/regional/language/detection/url which doesn't feel like it should be related, but "good enough for the moment", I thought).

    I'd be interested to hear more of your thoughts about the scenarios where prefix-mapping would get more complex than that?

    Regarding making it pluggable, my first thought is: if someone needed to do prefix negotiation differently, then surely the natural step would be for them to write an alternative Language Negotiation Plugin? Is there value in language_country_negotiation adding an extra plugin-layer further to this? Put another way: what code would be left in LanguageNegotiationLanguageCountryUrl if it deferred logic off to a sub-plugin?

    And (still regarding making it pluggable), my second thought is: If the scenarios you imagine are rare, then perhaps the use of a service ("PathPrefixHelper" in the current MR) is a good enough solution? After all, if another module wishes to adjust how prefixes are parsed (but in a way that is pretty-close-to-standard), then they have the option of replacing or decorating language_country_negotiation.path_prefix_helper. (If we code with the expectation that it may be decorated, perhaps the class should be renamed, final modifier taken off, and its methods restructured to make it more useful to do this kind of thing?)

    My third thought: The MR removes length/format assumptions of both LANGCODE and COUNTRYCODE which makes them potentially quite flexible. The only other thing I would add is something on the language-country-url configuration form that lists every country/language permutation and allows a custom prefix to be provided for each. This would be fairly simple to implement, and would surely afford maximum necessary flexibility?

  • Pipeline finished with Success
    11 days ago
    Total: 154s
    #335108
Production build 0.71.5 2024