settings = $settings; } /** * WordPress 6.5+ resolves translations through WP_Translation_Controller, * a singleton with one SHARED "current locale" used by every domain — * `WP_Translations::translate()` never remembers the locale a domain was * loaded under, it always re-reads the controller's current locale at * lookup time. So loading our domain via the normal `load_textdomain()` * under `en_US` doesn't stick: the very next `load_textdomain( 'default', ... )` * call for the site's own locale flips the controller's shared locale * back to the site's real one, and our earlier "en_US" entry becomes * unreachable. * * The fix: bypass the controller entirely for our domain. `$l10n[$domain]` * just needs *some* object with a `translate( $text, $context )` method — * the classic `MO` class (kept for backwards compatibility) does its own * self-contained lookups with no shared/global locale state, so it isn't * affected by anything loaded afterwards. */ public function register(): void { if ( '' === $this->forced_locale() ) { return; } $this->force_load_textdomain(); add_filter( 'load_script_translation_file', array( $this, 'filter_script_translation_file' ), 10, 3 ); } public function force_load_textdomain(): void { global $l10n; $forced = $this->forced_locale(); if ( 'en_US' !== $forced ) { foreach ( self::mo_candidates( $forced ) as $mofile ) { if ( ! file_exists( $mofile ) ) { continue; } $mo = new \MO(); if ( $mo->import_from_file( $mofile ) ) { $l10n['acez-seo'] = $mo; return; } } } // en_US, or a translation .mo that failed to load: block the JIT loader // so __() falls back to the original (already-English, since M70) // source string instead of picking up the site's real locale. // Deliberately NOT NOOP_Translations here — load_plugin_textdomain() // (called right after this, in Plugin::init()) specifically detects a // NOOP_Translations entry as "not really loaded yet" and unsets it, // which would let the JIT loader run fresh afterwards and undo this. // An empty MO instance behaves the same (translate() returns the // original string when it has no entries) without triggering that. $l10n['acez-seo'] = new \MO(); } /** * @param string|false $file * * @return string|false */ public function filter_script_translation_file( $file, string $handle, string $domain ) { if ( 'acez-seo' !== $domain ) { return $file; } $forced = $this->forced_locale(); if ( '' === $forced ) { return $file; } if ( 'en_US' === $forced ) { // Source JS strings are already English (M70) — no file needed. return false; } $name = 'acez-seo-' . $forced . '-' . md5( 'assets/js/sidebar.js' ) . '.json'; foreach ( array( WP_LANG_DIR . '/plugins/' . $name, ACEZ_SEO_DIR . 'languages/' . $name ) as $target ) { if ( file_exists( $target ) ) { return $target; } } return $file; } /** * Whether forcing $locale can actually change the panel's language on * this site. en_US needs no file (source strings are English); any other * locale needs its .mo, which a WordPress.org install only gets once WP * downloads the language pack — and WP does that solely for languages * installed on the site (`get_available_languages()`). * * @param string $locale Locale code, e.g. `pl_PL`. */ public static function has_translation( string $locale ): bool { if ( 'en_US' === $locale ) { return true; } foreach ( self::mo_candidates( $locale ) as $mofile ) { if ( file_exists( $mofile ) ) { return true; } } return false; } /** * Where to look for a forced locale's .mo, in priority order. * * WP_LANG_DIR first: that's where translate.wordpress.org drops community * translations, and since M70 (English source strings) the WordPress.org * package no longer bundles .mo files at all — that locale directory is * the ONLY place they exist for a plugin installed from the directory. * The plugin's own languages/ dir stays as a second candidate so a * privately distributed build (or a dev checkout) that does bundle them * keeps working unchanged. * * @return array */ private static function mo_candidates( string $locale ): array { $name = 'acez-seo-' . $locale . '.mo'; return array( WP_LANG_DIR . '/plugins/' . $name, ACEZ_SEO_DIR . 'languages/' . $name ); } private function forced_locale(): string { $value = (string) ( $this->settings->get()[ self::OPTION_KEY ] ?? '' ); return in_array( $value, self::ALLOWED_LOCALES, true ) ? $value : ''; } }