$part ) { $bits = explode( ';', trim( $part ) ); $tag = trim( $bits[0] ); $q = 1.0; if ( isset( $bits[1] ) && preg_match( '/q\s*=\s*([0-9.]+)/', $bits[1], $m ) ) { $q = (float) $m[1]; } if ( '' !== $tag && $q > 0 ) { $prefs[] = array( $tag, $q, $i ); } } usort( $prefs, function ( $a, $b ) { return $b[1] === $a[1] ? $a[2] - $b[2] : ( $b[1] > $a[1] ? 1 : -1 ); } ); foreach ( $prefs as $p ) { $primary = substr( $p[0], 0, 2 ); if ( 'es' === $primary || 'en' === $primary ) { return $primary; } } return ''; } /** * Language to remember on a new code (so later emails use it), or ''. */ public static function lang_for_new_code() { return 'visitor' === self::setting() ? self::visitor_lang() : ''; } /** * Is this request something a shopper sees? */ protected static function shopper_context() { if ( wp_doing_cron() ) { return true; } return ! is_admin() || ( wp_doing_ajax() && ! current_user_can( 'manage_woocommerce' ) ); } /** * Locale shoppers see. */ public static function store_locale( $lang = '' ) { $s = self::setting(); if ( 'visitor' === $s ) { $lang = in_array( $lang, array( 'es', 'en' ), true ) ? $lang : self::visitor_lang(); $s = $lang ? $lang : 'auto'; // Unknown: the site language. } if ( 'en' === $s ) { return 'en_US'; } if ( 'es' === $s ) { return self::is_spanish( get_locale() ) ? get_locale() : 'es_ES'; } return get_locale(); } protected static function is_spanish( $locale ) { return 0 === strpos( (string) $locale, 'es' ); } /** * Is the text currently showing in Spanish while WordPress itself isn't? * (Then month and day names need translating too.) */ public static function spanish_override() { return self::is_spanish( self::$loaded ) && ! self::is_spanish( determine_locale() ); } public static function load() { self::switch_to( self::shopper_context() ? self::store_locale() : determine_locale() ); } /** * Text domains that follow the store language: domain => folder with its * bundled translations. Add-ons register theirs with this filter. * * @return array */ public static function domains() { return (array) apply_filters( 'hyperdrop_text_domains', array( 'hyperdrop' => HYPERDROP_DIR . 'languages/' ) ); } /** * Load HyperDrop's text domains in a given locale. */ public static function switch_to( $locale ) { if ( $locale === self::$loaded ) { return; } self::$loaded = $locale; $domains = self::domains(); foreach ( array_keys( $domains ) as $domain ) { unload_textdomain( $domain, true ); } if ( 'en_US' === $locale ) { add_filter( 'override_load_textdomain', array( __CLASS__, 'block_english' ), 10, 2 ); return; } remove_filter( 'override_load_textdomain', array( __CLASS__, 'block_english' ), 10 ); // Register under the locale WordPress is using right now — that is where __() looks. $current = determine_locale(); foreach ( $domains as $domain => $dir ) { $official = WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo'; $bundled = trailingslashit( $dir ) . $domain . '-es_ES.mo'; if ( is_readable( $official ) ) { load_textdomain( $domain, $official, $current ); } elseif ( self::is_spanish( $locale ) && is_readable( $bundled ) ) { load_textdomain( $domain, $bundled, $current ); } } } /** * English chosen: keep WordPress from loading another language for HyperDrop. */ public static function block_english( $override, $domain ) { return array_key_exists( $domain, self::domains() ) ? true : $override; } /** * Run something (e.g. send an email) in the store language, then switch back. * * @param callable $fn * @param string $lang 'es' / 'en' remembered for the recipient, when known. */ public static function as_shopper( $fn, $lang = '' ) { $before = self::$loaded ? self::$loaded : determine_locale(); self::switch_to( self::store_locale( $lang ) ); try { return call_user_func( $fn ); } finally { self::switch_to( $before ); } } /** * Spanish month/day names when the store shows Spanish on an English WordPress. */ public static function date( $ts ) { if ( ! self::spanish_override() ) { return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $ts ); } $months = array( 'enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre' ); $days = array( 'domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado' ); $dt = ( new DateTimeImmutable( '@' . (int) $ts ) )->setTimezone( wp_timezone() ); return sprintf( '%s %d de %s de %d, %s', $days[ (int) $dt->format( 'w' ) ], (int) $dt->format( 'j' ), $months[ (int) $dt->format( 'n' ) - 1 ], (int) $dt->format( 'Y' ), $dt->format( 'g:i a' ) ); } }