Problem/Motivation

The locales form build, validation and submission callbacks to account for languages with hyphenated langcodes.

Steps to reproduce

With a site configured with multiple languages, including custom languages with hyphenated lang codes.

  • Go to /admin/config/services/bazaarvoice
  • Configure locales
  • Save form
  • Review configured locales
  • Observe languages with hyphenated

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jasonawant created an issue. See original summary.

jasonawant’s picture

Notes

  • when rendering the form and setting default locale codes for each langauge
  • BazaarvoiceService::getBazaarvoiceLocaleCode transforms a langcode of fr-ca to fr_ca here
  • BazaarvoiceService::isValidLocaleCode() returns false when validating fr_ca
santoine’s picture

santoine’s picture

Status: Active » Needs review
FileSize
12.67 KB

Save locales hyphenated langcodes correctly.

santoine’s picture

FileSize
12.67 KB

Save locales hyphenated langcodes correctly.

jasonawant’s picture

Status: Needs review » Needs work

patch does not apply when using composer; only include relevant changes and I'll try again.

santoine’s picture

FileSize
1.84 KB

Save locales hyphenated langcodes correctly. This change should allow user's to apply patch via composer.

jasonawant’s picture

Thanks for the patch santoine!

I took some time to review methods getBazaarvoiceLocaleCode(), and its related methods: isValidLocaleCode, getLocaleCode, findDefaultLocale and defaultLocaleCodes().

I've also reviewed the BV documentation on locales and supported locales.

The Bazaarvoice platform support multiple locales which are identified using the language_COUNTRY format.

BV supported locales. are not 1:1 with the list of the modules default codes seen here, e.g af_NA (first default code) is not a supported BV code.

I don't think it makes much sense to manage default locales in code as BV may change that. I created #3143372: Replace default codes with help text and link to supported BV locales.

Regarding the original reported issue and getBazaarvoiceLocaleCode(), I don't see a need to transform or validate the langcode at all in order to fetch the mapped locale code, do you?

I think the $locale_code = $langcode; confuses things greatly b/c while it makes sense to validate BV locale codes for its expected language_COUNTRY format. However, when getBazaarvoiceLocaleCode validates it's validating the langcode, not BV locale code.

Currently,

  public function getBazaarvoiceLocaleCode($langcode) {
    $locale_code = $langcode;
    // Do some basic transformation.
    $locale_code = preg_replace('/[^a-zA-Z]+/', '_', trim($locale_code));

    if (!$this->isValidLocaleCode($locale_code)) {
      if ($code = $this->getLocaleCode($locale_code)) {
        $locale_code = $code;
      } else {
        $locale_code = $this->findDefaultLocale($locale_code);
      }
    }
    return $locale_code;
  }

Recommended change to preserve the possibility of this return FALSE as previously observed.

  /**
   * {@inheritdoc}
   */
  public function getBazaarvoiceLocaleCode($langcode) {
    $locale_code = FALSE;

    if ($code = $this->getLocaleCode($locale_code)) {
      $locale_code = $code;
    }

    return $locale_code;
  }

However, now looking at this, getBazaarvoiceLocaleCode() and getLocaleCode() can be the same and thus getBazaarvoiceLocaleCode() and remove getLocaleCode(); it was only being used by getBazaarvoiceLocaleCode().

  /**
   * {@inheritdoc}
   */
  public function getBazaarvoiceLocaleCode($langcode) {
    $locale_code = FALSE;
    if ($locale_codes = $this->getLocaleCodes()) {
      if (isset($locale_codes[$langcode])) {
        $locale_code = $locale_codes[$langcode];
      }
    }
    return $locale_code;
  }
santoine’s picture

FileSize
4.97 KB

This patch significantly cleans up the bazaarvoice service removing unnecessary code.

jasonawant’s picture

Looks good!

Looks like setLocaleCodes() is used in AdminSettingsForm::submitForm(), so we'll need to keep this method, which sets the mapped langcode to locale to bazaarvoice.locales.

+++ b/src/Service/BazaarvoiceService.php
@@ -28,35 +28,19 @@ class BazaarvoiceService implements BazaarvoiceServiceInterface {
-  public function setLocaleCodes($locales) {
santoine’s picture

FileSize
5.67 KB

Add code style changes. Now compatible with other patches.

kamkejj’s picture

Looks like this issue included this patch. Check what's in patch #11 and the code changes are applied on the dev branch.

I think this can be closed.

kamkejj’s picture

Status: Needs work » Needs review