Merge pull request #154 from woocommerce/PCP-138-paypal-payments-doesnt-work-with

Filter PayPal-supported language codes
This commit is contained in:
Emili Castells 2021-05-20 11:42:54 +02:00 committed by GitHub
commit cc1cd30bb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 2 deletions

View file

@ -46,7 +46,7 @@ class ApplicationContextRepository {
): ApplicationContext {
$brand_name = $this->settings->has( 'brand_name' ) ? $this->settings->get( 'brand_name' ) : '';
$locale = str_replace( '_', '-', get_user_locale() );
$locale = $this->valid_bcp47_code();
$landingpage = $this->settings->has( 'landing_page' ) ?
$this->settings->get( 'landing_page' ) : ApplicationContext::LANDING_PAGE_NO_PREFERENCE;
$context = new ApplicationContext(
@ -59,4 +59,24 @@ class ApplicationContextRepository {
);
return $context;
}
/**
* Returns a PayPal-supported BCP-47 code, for example de-DE-formal becomes de-DE.
*
* @return string
*/
protected function valid_bcp47_code() {
$locale = str_replace( '_', '-', get_user_locale() );
if ( preg_match( '/^[a-z]{2}(?:-[A-Z][a-z]{3})?(?:-(?:[A-Z]{2}))?$/', $locale ) ) {
return $locale;
}
$parts = explode( '-', $locale );
if ( count( $parts ) === 3 ) {
return substr( $locale, 0, strrpos( $locale, '-' ) );
}
return 'en';
}
}